home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / nsfont.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  20.1 KB  |  712 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. // implement CNetscapeFontModule
  20. #include "stdafx.h"
  21.  
  22. #ifndef XP_WIN
  23. #define XP_WIN
  24. #endif
  25.  
  26. #ifdef Displayer_SDK
  27.     #define CASTINT(a) ((int)(a))
  28. #endif
  29.  
  30. #include "nsfont.h"
  31.  
  32. #include "nf.h"         // for nfSpacingProportional
  33. #include "Mnff.h"       // for nff_GetRenderableFont
  34. #include "Mnfrf.h"      // for     struct nfrf     *pRenderableFont;
  35. #include "Mnffmi.h"
  36. #include "Mnfrc.h"
  37.  
  38.  
  39. #include "Mnffbp.h"
  40. #include "Mnffbc.h"
  41. #include "Mnffbu.h"
  42. #include "Mnfdoer.h"
  43.  
  44. #include "prefapi.h"        // for PREF_GetIntPref
  45.  
  46. extern "C" char *FE_GetProgramDirectory(char *buffer, int length);
  47.  
  48. // XXX Arthur todo
  49. // These directory names should be relative to preferance.
  50. #define WF_FONT_CATALOG            "dynfonts\\fonts.cat"
  51. #define WF_FONT_DISPLAYER_PATH    "dynfonts"
  52.  
  53. char *CNetscapeFontModule::converGenericFont(char *pExtracted)
  54. {
  55.     if( stricmp( pExtracted, "serif" ) == 0) 
  56.         return( DEF_PROPORTIONAL_FONT );   //  "Times New Roman" defined in defaults.h
  57.  
  58.     if( stricmp( pExtracted, "sans-serif" ) == 0)  return( "Arial" );
  59.     if( stricmp( pExtracted, "cursive" ) == 0) return( "Script" );
  60.     if( stricmp( pExtracted, "fantasy" ) == 0) return( "Arial" );
  61.     if( stricmp( pExtracted, "monospace" ) == 0) return( DEF_FIXED_FONT ); // "Courier New"
  62.  
  63.     return( pExtracted );  // return the origenal.
  64. }
  65.  
  66. char * CNetscapeFontModule::convertToFmiCharset( BYTE charSetID )
  67. {
  68.     switch( charSetID )
  69.     {
  70.         case ANSI_CHARSET:            return ("iso-8859-1");
  71.         case DEFAULT_CHARSET:        return ("DEFAULT_CHARSET");
  72.         case SYMBOL_CHARSET:        return ("adobe-symbol-encoding");
  73.         case SHIFTJIS_CHARSET:        return ("Shift_JIS");
  74.         case HANGEUL_CHARSET:        return ("EUC-KR");
  75.         case CHINESEBIG5_CHARSET:    return ("big5");
  76.         case OEM_CHARSET:            return ("OEM_CHARSET");
  77.         //    We cannot use xxx_CHARSET for the rest since they are not 
  78.         //    defined for Win16 
  79.         case 130: return ("JOHAB_CHARSET");
  80.         case 134: return ("gb2312");    // not defined for 16 bit
  81.         case 177: return ("HEBREW_CHARSET");
  82.         case 178: return ("ARABIC_CHARSET");
  83.         case 161: return ("windows-1253");
  84.         case 162: return ("iso-8859-9");
  85.         case 222: return ("x-tis620");
  86.         case 238: return ("windows-1250");
  87.         case 204: return ("windows-1251");
  88.         default:  return ("DEFAULT_CHARSET");
  89.     }
  90.  
  91. }
  92.  
  93. int CNetscapeFontModule::convertToFmiPitch( LOGFONT *pLogFont )
  94. {
  95.     if( pLogFont->lfPitchAndFamily == FIXED_PITCH )
  96.         return( nfSpacingMonospaced );
  97.  
  98.     if( pLogFont->lfPitchAndFamily == VARIABLE_PITCH )
  99.         return( nfSpacingProportional );
  100.  
  101.     return( nfSpacingDontCare );
  102. }
  103.  
  104. int CNetscapeFontModule::convertToFmiStyle( LOGFONT *pLogFont )
  105. {
  106.     //TODO
  107.     if( pLogFont->lfItalic )
  108.         return( nfStyleItalic );
  109.  
  110.     // todo map:    lfUnderline, lfStrikeOut
  111.     //      to      nfStyleNormal, nfStyleOblique
  112.     return( nfStyleDontCare );
  113. }
  114.  
  115. struct nffmi*
  116. CNetscapeFontModule::createFMIfromLOGFONTWithAttr( LOGFONT *pLogFont, int YPixelPerInch, int extraAttributes  )
  117. {
  118.  
  119.     nffmi*  pFmi;
  120.     char    *FmiName;
  121.     char    *FmiCharset;
  122.     char    *FmiEncoding;
  123.     int     FmiWeight;
  124.     int     FmiPitch;
  125.     int     FmiStyle;
  126.     int        FmiUnderline;
  127.     int     FmiOverstrike;
  128.     int     FmiPixelPerInchX;
  129.  
  130.     FmiName         = pLogFont->lfFaceName;
  131.     FmiCharset      = convertToFmiCharset( pLogFont->lfCharSet );
  132.     FmiEncoding     = extraAttributes ? "Unicode": FmiCharset;
  133.     FmiWeight       = pLogFont->lfWeight;
  134.     FmiPitch        = convertToFmiPitch( pLogFont );
  135.     FmiStyle        = convertToFmiStyle( pLogFont ); 
  136.     FmiUnderline    = pLogFont->lfUnderline? nfUnderlineYes : nfUnderlineDontCare;
  137.     FmiOverstrike    = pLogFont->lfStrikeOut? nfStrikeOutYes : nfStrikeOutDontCare;
  138.     FmiPixelPerInchX  = 0;
  139.     
  140.  
  141.     pFmi = nffbu_CreateFontMatchInfo(
  142.         m_pFontBrokerUtilityInterface, 
  143.         FmiName,
  144.         FmiCharset,
  145.         FmiEncoding,
  146.         FmiWeight,
  147.         FmiPitch,
  148.         FmiStyle,
  149.         FmiUnderline,
  150.         FmiOverstrike,
  151.         FmiPixelPerInchX,                          
  152.         YPixelPerInch,                          
  153.         NULL /* exception */
  154.         );
  155.  
  156.     return( pFmi );        
  157. }
  158.  
  159.  
  160. //////////////////////////////////////////////////////////////////////////////////
  161. //  Implement class CNetscapeFontModule
  162.  
  163. CNetscapeFontModule::CNetscapeFontModule()
  164. {
  165.     m_pFontBrokerDisplayerInterface  = NULL; /* The FontBroker Displayer interface */
  166.     m_FontBrokerConsumerInterface   = NULL; /* The FontBroker Consumer interface */
  167.     m_pFontBrokerUtilityInterface   = NULL; /* The FontBroker Utility interface */
  168.  
  169.     m_pFontDisplayerInterface        = NULL; /* The FontDisplayer interface */
  170.  
  171.     m_fontModuleStatus                = FMSTATUS_NULL;
  172.  
  173. }
  174.  
  175. CNetscapeFontModule::~CNetscapeFontModule()
  176. {
  177.     if( m_workingRC )
  178.         nfrc_release( m_workingRC, NULL);
  179.     m_workingRC = NULL;
  180.  
  181.     // Save the catalog
  182.     if (m_pFontBrokerUtilityInterface) {
  183.         char     exeDir_buffer[1024];
  184.         FE_GetProgramDirectory( exeDir_buffer, 512 );
  185.     
  186.         if( *exeDir_buffer ) {
  187.             strcat( exeDir_buffer, WF_FONT_CATALOG );
  188.             nffbu_SaveCatalog(m_pFontBrokerUtilityInterface, exeDir_buffer, NULL);  
  189.         }
  190.     }
  191.     
  192.     releaseDisplayer();
  193. }
  194.  
  195. int CNetscapeFontModule::InitFontModule( )
  196. {
  197.     if( m_fontModuleStatus != FMSTATUS_NULL )
  198.         return( FONTERR_OK );            // already initialized
  199.  
  200.     if( createObjects() ) 
  201.         return( FONTERR_CreateObjectFail );
  202.  
  203.     if ( getInterfaces() )
  204.         return( FONTERR_GetInterfacesFail );
  205.  
  206.     if ( registerFontDisplayer() )
  207.         return( FONTERR_RegisterFontDisplayerFail );
  208.     
  209.     m_fontModuleStatus = FMSTATUS_IntialOK;
  210.  
  211.        // create the RC object
  212.     m_workingRC            = convertDC(NULL);
  213.  
  214.     return( FONTERR_OK );
  215. }
  216.  
  217. int CNetscapeFontModule::createObjects()
  218. {
  219.     int ret = 0;
  220.  
  221.     /* Create the FontBroker Object */
  222.  
  223.     m_FontBrokerConsumerInterface = NF_FontBrokerInitialize();
  224.     if (!m_FontBrokerConsumerInterface)
  225.         ret = -1;
  226.  
  227.     return (ret);
  228.  
  229. }   // int CNetscapeFontModule::createObjects()
  230.  
  231. int CNetscapeFontModule::getInterfaces()
  232. {
  233.     int ret = 0;
  234.  
  235.     /* Get all the FontBroker interfaces */
  236.     m_pFontBrokerDisplayerInterface = (struct nffbp *) nffbc_getInterface(m_FontBrokerConsumerInterface, &nffbp_ID, NULL);
  237.     if (!m_pFontBrokerDisplayerInterface )
  238.       {
  239.         ret--;
  240.       }
  241.  
  242.     m_pFontBrokerUtilityInterface = (struct nffbu *) nffbc_getInterface(m_FontBrokerConsumerInterface, &nffbu_ID, NULL);
  243.     if (!m_pFontBrokerUtilityInterface)
  244.       {
  245.         ret--;
  246.       }
  247.  
  248.        /* Create the FontDisplayer Object, passing the broker interface to Displayer */
  249.     m_pFontDisplayerInterface = (struct nffp *) winfpFactory_Create(NULL, m_pFontBrokerDisplayerInterface );
  250.     if (!m_pFontDisplayerInterface)
  251.       {
  252.         ret--;
  253.       }
  254.     
  255.     return (ret);
  256.  
  257. }   // int CNetscapeFontModule::getInterfaces()
  258.  
  259. /* Register the Displayer with the broker */
  260. int CNetscapeFontModule::registerFontDisplayer()
  261. {
  262.     int        reCode ;
  263.     int        ndisplayer = 0;
  264.  
  265.     char     exeDir_buffer[513], dir_buffer[1024];
  266.     FE_GetProgramDirectory( exeDir_buffer, 512 );
  267.  
  268. #ifdef WIN32
  269.     // not working on win16
  270.     if( *exeDir_buffer ) {
  271.         strcat( strcpy(dir_buffer,exeDir_buffer), WF_FONT_CATALOG );
  272.         // Load the font catalogs
  273.         nffbu_LoadCatalog(m_pFontBrokerUtilityInterface, dir_buffer, NULL);
  274.     }
  275. #endif
  276.     
  277.     // Register native windows displayer
  278.     reCode = (int) nffbp_RegisterFontDisplayer(m_pFontBrokerDisplayerInterface, m_pFontDisplayerInterface, NULL);
  279.     if (reCode >= 0)
  280.     {
  281.         ndisplayer = 1;
  282.     }
  283.  
  284.     /* 
  285.     0    : always ignore document fonts 
  286.     1    : use document fonts but ignore webfonts 
  287.     2    : use document fonts and webfonts (default) 
  288.     */
  289.     if(*exeDir_buffer ) {
  290.         // Scan other displayers               
  291.         strcat( strcpy(dir_buffer,exeDir_buffer), WF_FONT_DISPLAYER_PATH );
  292.         reCode = (int) nffbp_ScanForFontDisplayers(m_pFontBrokerDisplayerInterface, dir_buffer, NULL);
  293.         if (reCode > 0)
  294.         {
  295.             ndisplayer += reCode;
  296.         }
  297.     }
  298.  
  299.     if (ndisplayer <= 0)
  300.     {
  301.         ASSERT( ndisplayer > 0 ); // XXX Aaagh. NO displayer. Alert user and exit.
  302.     }
  303.  
  304.     return( 0);        // arthur 2/19 reCode );
  305. }
  306.  
  307. int CNetscapeFontModule::releaseAllFontResource(MWContext *pContext)
  308. {
  309.     return(0);
  310. }
  311.  
  312. int CNetscapeFontModule::LookupFailed(MWContext *pContext, struct nfrc *prc, struct nffmi * pfmi)
  313. {
  314.     if( m_pFontBrokerUtilityInterface == NULL )
  315.         return(-1);
  316.  
  317.     nffbu_LookupFailed(m_pFontBrokerUtilityInterface, pContext,prc, pfmi, NULL);
  318.     return(0);
  319. }
  320.  
  321. // this function mallocate space. When done with the returned pointer
  322. // call nfrc_release( pRenderingContext, NULL);
  323. struct nfrc    * CNetscapeFontModule::convertDC( HDC hDC )
  324. {
  325.     struct nfrc        *tempRenderingContext;
  326.        void            *rcbuf[4]; 
  327.  
  328.     if( m_fontModuleStatus == FMSTATUS_NULL ) {
  329.         m_lastError = FONTERR_NotInitiated;
  330.         return(NULL );
  331.     }
  332.  
  333.     rcbuf[0] = (void *)hDC;         // windows specific rendering context
  334.     rcbuf[1] = 0;           // terminator
  335.     tempRenderingContext = nffbu_CreateRenderingContext(m_pFontBrokerUtilityInterface, NF_RC_DIRECT, 0, rcbuf, 1,
  336.                                       NULL /* exception */);
  337.  
  338.     if (tempRenderingContext == NULL) {
  339.         m_lastError = FONTERR_ConvertDCFailed;
  340.         return( NULL );
  341.     }
  342.     
  343.     return( tempRenderingContext );
  344. }
  345.  
  346. int    CNetscapeFontModule::WebfontsNeedReload( MWContext *pContext )
  347. {
  348.     int nn;
  349.     nn = (int) nffbu_WebfontsNeedReload(m_pFontBrokerUtilityInterface, pContext, NULL);
  350.     return(nn);
  351. }
  352.  
  353. int    CNetscapeFontModule::UpdateWorkingRC( HDC hDC )
  354. {
  355.     struct rc_data  RenderingContextData;
  356.     RenderingContextData = nfrc_GetPlatformData( m_workingRC, NULL/*exception*/ ); 
  357.     RenderingContextData.t.directRc.dc = (void *)hDC;
  358.     nfrc_SetPlatformData( m_workingRC, &RenderingContextData, NULL /*exception*/ ); 
  359.     return( FONTERR_OK );
  360. }
  361.  
  362. int
  363. CNetscapeFontModule::FreeFMIList( struct nffmi ** fmiList)
  364. {
  365.     nffbu_free( getBrokerUtilityInterface(), fmiList, NULL );
  366.     return( FONTERR_OK );
  367. }
  368.  
  369. // this function malloc space. After done with the list,
  370. // call CNetscapeFontModule::FreeFMIList()
  371. struct nffmi **CNetscapeFontModule::CreateFMIList(HDC hDC )
  372. {
  373.     struct nffmi **pTempFmiList;
  374.        struct nffmi    *pWantedFmi;
  375.  
  376.     if( m_fontModuleStatus == FMSTATUS_NULL ) {
  377.         m_lastError = FONTERR_NotInitiated;
  378.         return( NULL );
  379.     }
  380.  
  381.     if( UpdateWorkingRC( hDC ) ) 
  382.         return(NULL);            // error
  383.  
  384.     // Create a FMI for the font of interest 
  385.     // Not really used by Displayer, because it has hardcoded the font and don't do any matching.
  386.     pWantedFmi = nffbu_CreateFontMatchInfo(
  387.                 m_pFontBrokerUtilityInterface, 
  388.                 "Arial",                  // "nfFmiName"
  389.                 "iso8859",                  // "nfFmiCharset"
  390.                 "1",                        // "nfFmiEncoding"
  391.                 400,                        // "nfFmiWeight"
  392.                 nfSpacingDontCare,          // "nfFmiPitch"     nfSpacingProportional,
  393.                 nfStyleDontCare,            // "nfFmiStyle"     nfStyleDontCare, nfStyleItalic
  394.                 0,                            // FmiUnderline,
  395.                 0,                            // FmiOverstrike,
  396.                 0,                          // "nfFmiPixelPerInchX"
  397.                 0,                          // "nfFmiPixelPerInchY"
  398.                 NULL /* exception */);
  399.     if (pWantedFmi == NULL) { 
  400.         m_lastError = FONTERR_CreateFontMatchInfoFailed;
  401.         return( NULL );
  402.       }
  403.  
  404.  
  405.     pTempFmiList = (struct nffmi **)nffbc_ListFonts(m_FontBrokerConsumerInterface, m_workingRC, pWantedFmi, NULL /* exception */);
  406.  
  407.     // clean up 
  408.     nffmi_release( pWantedFmi, NULL);
  409.  
  410.     return( pTempFmiList );
  411.  
  412. }
  413.  
  414. int CNetscapeFontModule::releaseDisplayer()
  415. {
  416.  
  417.     if( m_pFontDisplayerInterface != NULL )
  418.         nffp_release( m_pFontDisplayerInterface, NULL);
  419.     m_pFontDisplayerInterface = NULL;
  420.     
  421.     return( FONTERR_OK );
  422. }
  423.  
  424.  
  425. ///////////////////////////////////////////////////////////////////////////////////
  426. // class Cyafont : yet another font class
  427.  
  428. extern CNetscapeFontModule    theGlobalNSFont;
  429.  
  430. CyaFont::CyaFont()
  431. {
  432.     m_pRenderableFont    = NULL;
  433.     m_iMeanWidth        = 0;
  434.  
  435. #ifdef DEBUG_aliu0
  436.     selectFlag            = 0;
  437. #endif
  438.  
  439.     // create the RC object
  440.     m_displayRC = NULL;
  441.     m_displayRC = theGlobalNSFont.convertDC(NULL);
  442.     ASSERT( m_displayRC != NULL );
  443. #ifndef NO_PERFORMANCE_HACK
  444.     m_displayRCData = NULL;
  445.     if (m_displayRC != NULL)
  446.     {
  447.         m_displayRCData = NF_GetRCNativeData(m_displayRC);
  448.     }
  449. #endif
  450. }
  451.  
  452. CyaFont::~CyaFont()
  453. {
  454.     if( m_pRenderableFont != NULL )
  455.         nfrf_release( m_pRenderableFont, NULL);
  456.     m_pRenderableFont = NULL;
  457.  
  458.     if( m_displayRC )
  459.         nfrc_release( m_displayRC, NULL);
  460.     m_displayRC = NULL;
  461. #ifndef NO_PERFORMANCE_HACK
  462.     m_displayRCData = NULL;
  463. #endif /* NO_PERFORMANCE_HACK */
  464. }
  465.  
  466. // this interface is for a quick integrating with Navigator
  467. // back-end xp code should use low level interface.
  468. //
  469. // this function allocate space, the destructor for m_pRenderableFont,
  470. // need to free the space.
  471. int    CyaFont::CreateNetscapeFontWithLOGFONT(
  472.        MWContext *pContext,
  473.        HDC hDC, LOGFONT *pLogFont, int extraAttributes /* =0 */  )
  474. {
  475.        struct nffmi    *pWantedFmi;
  476.     int                YPixelPerInch = ::GetDeviceCaps(hDC, LOGPIXELSY);
  477.  
  478.     pWantedFmi = theGlobalNSFont.createFMIfromLOGFONTWithAttr( pLogFont, YPixelPerInch, extraAttributes) ;
  479.  
  480.     if (pWantedFmi == NULL)
  481.         return( FONTERR_CreateFontMatchInfoFailed );
  482.  
  483.     // remember the lfPitchAndFamily, cannot get it from FMI easily
  484.     m_pitch = theGlobalNSFont.convertToFmiPitch( pLogFont );
  485.  
  486.     int reCode = CreateNetscapeFontWithFMI(
  487.         pContext,
  488.         hDC, pWantedFmi, pLogFont->lfHeight / YPixelPerInch ) ;
  489.     CalculateMeanWidth( hDC, extraAttributes );
  490.     return( reCode );
  491. }
  492.  
  493. // this function allocate space, the destructor for m_pRenderableFont,
  494. // need to free the space.
  495. int    CyaFont::CreateNetscapeFont(
  496.     MWContext *pContext,
  497.     HDC        hDC, 
  498.     const char    *FmiName,
  499.     const char    *FmiCharset,
  500.     const char    *FmiEncoding,
  501.     int     FmiWeight,
  502.     int     FmiPitch,
  503.     int     FmiStyle,
  504.     int        FmiUnderline,
  505.     int     FmiOverstrike,
  506.     int     FmiPixelPerInchX,
  507.     int     FmiPixelPerInchY,
  508.     double    fontHeight                // not a fmi field
  509.     )
  510. {
  511.        struct nffmi    *pWantedFmi;
  512.     int                reCode;
  513.  
  514.     // remember the lfPitchAndFamily, cannot get it from FMI easily
  515.     m_pitch = FmiPitch;
  516.  
  517.     pWantedFmi = nffbu_CreateFontMatchInfo( 
  518.         theGlobalNSFont.getBrokerUtilityInterface(),
  519.         FmiName,
  520.         FmiCharset,
  521.         FmiEncoding,
  522.         FmiWeight,
  523.         FmiPitch,
  524.         FmiStyle,
  525.         FmiUnderline,
  526.         FmiOverstrike,
  527.         FmiPixelPerInchX,                          
  528.         FmiPixelPerInchY,                          
  529.         NULL                /* exception */
  530.         );
  531.  
  532.     if (pWantedFmi == NULL)
  533.         return( FONTERR_CreateFontMatchInfoFailed );
  534.     
  535.     reCode = CreateNetscapeFontWithFMI(pContext, hDC, pWantedFmi, fontHeight );
  536.     nffmi_release( pWantedFmi, NULL);
  537.  
  538.     return( reCode); 
  539.  
  540. }
  541.  
  542. // this function allocate space, the destructor for m_pRenderableFont,
  543. // need to free the space.
  544. int    CyaFont::CreateNetscapeFontWithFMI(
  545.     MWContext *pContext,
  546.     HDC hDC, struct nffmi *pWantedFmi, double fontHeight )
  547. {
  548.     struct nff      *genericFontHandle;
  549.  
  550.     if( m_pRenderableFont != NULL )
  551.         nfrf_release( m_pRenderableFont, NULL);
  552.     m_pRenderableFont = NULL;
  553.     
  554.     if( UpdateDisplayRC( hDC ) ) 
  555.         return(1);            // error
  556.  
  557.       // Create a FMI for the font of interest 
  558.     // Not really used by Displayer, because it has hardcoded the font and don't do any matching.
  559.     
  560.     if (pWantedFmi == NULL)
  561.         return( FONTERR_CreateFontMatchInfoFailed );
  562.     
  563.     /* Find the url that is loading this font */
  564.     const char *accessing_url_str = NULL;
  565.     if( pContext ) {
  566.         History_entry *he = SHIST_GetCurrent(&(pContext->hist));
  567.         if( he )
  568.             accessing_url_str = he->address;
  569.     }
  570.  
  571.     // Look for a font 
  572.     genericFontHandle = nffbc_LookupFont(
  573.            theGlobalNSFont.getFontBrokerConsumerInterface(), 
  574.            m_displayRC,
  575.            pWantedFmi,
  576.            accessing_url_str, /* XXX GetContext()->url */
  577.            NULL /* exception */);
  578.  
  579.     if (genericFontHandle == NULL) {
  580.         // failed
  581.         theGlobalNSFont.LookupFailed( pContext, m_displayRC, pWantedFmi);
  582.         return( FONTERR_nffbc_LookupFontFailed );
  583.  
  584.     }
  585.  
  586.     if( fontHeight < 0 )
  587.         fontHeight = - fontHeight;        // webfont use positive size, see winfp.cpp
  588.  
  589.     /* Create a rendering font */
  590.     m_pRenderableFont = nff_GetRenderableFont( genericFontHandle, m_displayRC, fontHeight, NULL /* exception */);
  591.     
  592.     // clean up 
  593.     nff_release( genericFontHandle , NULL );
  594.  
  595.  
  596.     return( FONTERR_OK );
  597. }   // int    CyaFont::CreateNetscapeFontWithFMI( HDC hDC )
  598.  
  599. int CyaFont::GetAscent()    { return(CASTINT(NULL==m_pRenderableFont?0:nfrf_GetFontAscent( m_pRenderableFont,  NULL ) )); }
  600. int CyaFont::GetDescent()    { return(CASTINT(NULL==m_pRenderableFont?0:nfrf_GetFontDescent( m_pRenderableFont, NULL ) )); }
  601. int CyaFont::GetMeanWidth() { return( m_iMeanWidth ); }
  602. int CyaFont::GetHeight()    { return( (int) ( GetAscent() + GetDescent() )); }
  603.  
  604. int CyaFont::GetMaxWidth()  
  605.     int wid = CASTINT(NULL==m_pRenderableFont?0:nfrf_GetMaxWidth( m_pRenderableFont, NULL ) );
  606.     return(wid); 
  607. }
  608.  
  609. jint CyaFont::MeasureTextWidth( HDC hDC, jbyte *str, jsize strLen, jint *charLocs, jsize charLoc_len )
  610. {
  611.     jint  len, chrSpaceing;
  612.     if( m_pRenderableFont == NULL )
  613.         return -1;
  614.  
  615.     if( UpdateDisplayRC( hDC ) ) 
  616.             return( -2 );            // error
  617.  
  618.     if( strLen <= 0 )
  619.         return(0);
  620.  
  621.     len = nfrf_MeasureText( m_pRenderableFont, m_displayRC, chrSpaceing, str, strLen, charLocs, charLoc_len, NULL );
  622.     return(len);
  623. }
  624.  
  625. BOOL CyaFont::MeasureTextSize( HDC hDC, jbyte *str, jsize strLen, jint *charLocs, jsize charLoc_len, LPSIZE lpSize)
  626. {
  627.     lpSize->cx = CASTINT(MeasureTextWidth( hDC, str, strLen, charLocs, charLoc_len ));
  628.     lpSize->cy = GetHeight();
  629.     return( lpSize->cx >= 0? TRUE : FALSE );
  630. }
  631. // this is a mimic of Navigator code in CNetscapeFont::Initialize()
  632. // return -1 if failed.
  633. int CyaFont::CalculateMeanWidth(  HDC hDC, BOOL isUnicode )
  634. {
  635.     // calculate the mean width
  636.     m_iMeanWidth    = 0;
  637.     if( isUnicode ) {
  638.         m_iMeanWidth = CASTINT(MeasureTextWidth( hDC, "\x0W\x0w", 4, NULL, 0 ));
  639.     } else {
  640.         m_iMeanWidth = CASTINT(MeasureTextWidth( hDC, "Ww", 2, NULL, 0 ));
  641.     }
  642.     m_iMeanWidth = m_iMeanWidth / 2 ;
  643.  
  644.     return( m_iMeanWidth );
  645. }
  646.  
  647. int    CyaFont::UpdateDisplayRC( HDC hDC )
  648. {
  649. #ifndef NO_PERFORMANCE_HACK
  650.     m_displayRCData->t.directRc.dc = (void *)hDC;
  651. #else
  652.     ASSERT( m_displayRC != NULL );
  653.     struct rc_data  RenderingContextData;
  654.     RenderingContextData = nfrc_GetPlatformData( m_displayRC, NULL/*exception*/ ); 
  655.     RenderingContextData.t.directRc.dc = (void *)hDC;
  656.     nfrc_SetPlatformData( m_displayRC, &RenderingContextData, NULL /*exception*/ ); 
  657. #endif /* NO_PERFORMANCE_HACK */
  658.     return( FONTERR_OK );
  659. }
  660.  
  661. // PrepareDrawText() and EndDrawText() must be called in pair!
  662. int    CyaFont::PrepareDrawText( HDC hDC )
  663. {
  664. #ifdef DEBUG_aliu0
  665.     ASSERT( selectFlag == 0 );
  666.     selectFlag = 1;
  667. #endif
  668.     
  669.     if( NULL == m_pRenderableFont )
  670.         return(1);        // todo
  671.  
  672.     if( UpdateDisplayRC( hDC ) )
  673.         return(1);            // error
  674.  
  675.     nfrf_PrepareDraw( m_pRenderableFont, m_displayRC, NULL /*exception*/ ); 
  676.     return( FONTERR_OK );
  677. }
  678.  
  679. // PrepareDrawText() and EndDrawText() must be called in pair!
  680. int    CyaFont::EndDrawText( HDC hDC )
  681. {
  682. #ifdef DEBUG_aliu0
  683.     ASSERT( selectFlag == 1 );
  684.     selectFlag = 0;
  685. #endif
  686.  
  687.     if( NULL == m_pRenderableFont ) 
  688.         return( FONTERR_OK );
  689.     if( UpdateDisplayRC( hDC ) ) 
  690.         return(1);            // error 
  691.  
  692.     nfrf_EndDraw( m_pRenderableFont, m_displayRC, NULL /*exception*/ ); 
  693.     return( FONTERR_OK );
  694. }
  695.                       
  696. int    CyaFont::drawText( HDC hDC, int xx, int yy, char *str, int strLen)
  697. {
  698.     if( m_pRenderableFont == NULL )
  699.         return(-1);
  700.  
  701.     if( UpdateDisplayRC( hDC ) ) 
  702.         return(1);            // error 
  703.  
  704.     jint    chrSpaceing;
  705.     // draw text through the renderable font
  706.     nfrf_DrawText( m_pRenderableFont, m_displayRC, xx, yy, chrSpaceing, str, strLen, NULL );
  707.  
  708.     return( FONTERR_OK );
  709.  
  710. }
  711.