home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / libfont / producers / win / src / winfp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  32.6 KB  |  1,133 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.  * Implements the FontDisplayer
  20.  */
  21.  
  22. // forward declear for .h files. if not decleared, 16bit compiler
  23. // reports overload nor allowed for _winfp_init() 
  24. struct nffbp;
  25. struct winfp;
  26.  
  27. #include "Pwinfp.h"
  28. #include "Pwinrf.h"
  29. //#include "Pcstrm.h"        /* for cstrmFactory_Create() */
  30.  
  31. #include "nf.h"     // for RenderingContextData.type
  32. #include "Mnfrf.h"      // for nfrf_ID
  33. #include "Mwinrf.h"       // for winrfFactory_Create
  34. #include "Mnffbu.h"
  35. #include "Mnff.h"      // for nfSpacingProportional
  36. #include "Mnffmi.h"
  37.  
  38. #include "Mnffbp.h"
  39. //#include "Mcfb.h"     // in ns\modules\libfont\src\_jmc  
  40.  
  41. #include "coremem.h"
  42.  
  43. /****************************************************************************
  44.  *                 Implementation of string-int mapping functions
  45.  *  Those can be shared by others, may be moved to cross platform XP_
  46.  ****************************************************************************/
  47.  
  48. typedef struct strIntMap_s {
  49.     char * str;
  50.     int    id;
  51. }  strIntMap_t, *pStrIntMap_t;
  52.  
  53. enum strIntMapFlag {
  54.     strIntMapNoCase = 0x01,
  55. };
  56.  
  57. // get (int) id
  58. EXTERN_C
  59. BOOL mapStrToInt( pStrIntMap_t pt, const char *ss, int *nn, int flag )
  60. {
  61.     const char *ps;
  62.     BOOL found;
  63.  
  64.     found = FALSE;
  65.     while( NULL != pt && NULL != (ps = pt->str) ) {
  66.         if( flag & strIntMapNoCase )
  67.             found = (BOOL) (stricmp( ps, ss ) == 0 );
  68.         else
  69.             found = (BOOL) (strcmp( ps, ss ) == 0 ) ;
  70.         
  71.         if( found ) {
  72.             *nn = pt->id;
  73.             return( TRUE );
  74.         }
  75.         // try next 
  76.         pt++;
  77.     }
  78.  
  79.     return(FALSE);
  80. }
  81.  
  82. // get string 
  83. EXTERN_C
  84. BOOL mapIntToStr( pStrIntMap_t pt,  int nn, char **ss)
  85. {
  86.     while( NULL != pt && NULL != (*ss = pt->str) ) {
  87.         if( pt->id == nn ) {
  88.             return( TRUE );
  89.         }
  90.         // try next 
  91.         pt++;
  92.     }
  93.  
  94.     return(FALSE);
  95. }
  96. /****************************************************************************
  97.  *        End of Implementation of string-int mapping functions
  98.  ****************************************************************************/
  99.  
  100. static strIntMap_t charSetIdMap[] = {
  101.     // from msdev\include\windgi.h
  102.     { "iso-8859-1"        , ANSI_CHARSET },
  103.     { "DEFAULT_CHARSET"     , DEFAULT_CHARSET },    
  104.     { "adobe-symbol-encoding"      , SYMBOL_CHARSET },    
  105.     { "Shift_JIS"            , SHIFTJIS_CHARSET },
  106.     { "EUC-KR"                , HANGEUL_CHARSET },
  107.     { "big5"                 ,    CHINESEBIG5_CHARSET },
  108.     { "OEM_CHARSET"         , OEM_CHARSET },
  109.  
  110.     //    We cannot use xx_CHARSET here because Win16 does not define them
  111.     { "JOHAB_CHARSET"       , 130 },
  112.     { "gb2312"                , 134 },        // not defined for 16 bit
  113.     { "HEBREW_CHARSET"      , 177 },
  114.     { "ARABIC_CHARSET"      , 178 },
  115.     { "windows-1253"        , 161 },
  116.     { "iso-8859-9"            , 162 },
  117.     { "x-tis620"            , 222 },
  118.     { "windows-1250"        , 238 },
  119.     { "windows-1251"        , 204 },
  120.     { NULL, 0 },                            // terminator.
  121. #if 0
  122.     // from msdev\include\windgi.h
  123.     { "ANSI_CHARSET"        , ANSI_CHARSET },
  124.     { "DEFAULT_CHARSET"     , DEFAULT_CHARSET },    
  125.     { "SYMBOL_CHARSET"      , SYMBOL_CHARSET },    
  126.     { "SHIFTJIS_CHARSET"    , SHIFTJIS_CHARSET },
  127.     { "HANGEUL_CHARSET"     , HANGEUL_CHARSET },
  128.     { "CHINESEBIG5_CHARSET" , CHINESEBIG5_CHARSET },
  129.     { "OEM_CHARSET"         , OEM_CHARSET },
  130.  
  131.     //    We cannot use xx_CHARSET here because Win16 does not define them
  132.     { "JOHAB_CHARSET"       , 130 },
  133.     { "GB2312_CHARSET"      , 134 },        // not defined for 16 bit
  134.     { "HEBREW_CHARSET"      , 177 },
  135.     { "ARABIC_CHARSET"      , 178 },
  136.     { "GREEK_CHARSET"       , 161 },
  137.     { "TURKISH_CHARSET"     , 162 },
  138.     { "THAI_CHARSET"        , 222 },
  139.     { "EASTEUROPE_CHARSET"  , 238 },
  140.     { "RUSSIAN_CHARSET"     , 204 },
  141.     { NULL, 0 },                            // terminator.
  142. #endif
  143.  
  144.     //    default:  return ("DEFAULT_CHARSET");
  145. };
  146.  
  147.  
  148. // todo: get rid of this global!!
  149. struct nffbp    *global_pBrokerObj;
  150.  
  151. EXTERN_C 
  152. struct nffbu *
  153. getFontBrokerUtilityInterface( struct nffbp    *pBrokerObj )
  154. {
  155.     struct nffbu *pBrokerUtilityInterface;
  156.     pBrokerUtilityInterface = (struct nffbu *) nffbp_getInterface( pBrokerObj, &nffbu_ID, NULL);
  157.  
  158.     return( pBrokerUtilityInterface );
  159. };
  160.  
  161.  
  162.  
  163.  
  164. /****************************************************************************
  165.  *                 Implementation of common interface methods
  166.  ****************************************************************************/
  167.  
  168. #ifdef OVERRIDE_winfp_getInterface
  169. #include "Mnffp.h"
  170. #ifdef __cplusplus
  171. extern "C"
  172. #endif
  173. JMC_PUBLIC_API(void*)
  174. _winfp_getInterface(struct winfp* self, jint op, const JMCInterfaceID* iid, JMCException* *exc)
  175. {
  176.     if (memcmp(iid, &nffp_ID, sizeof(JMCInterfaceID)) == 0)
  177.         return winfpImpl2winfp(winfp2winfpImpl(self));
  178.     return _winfp_getBackwardCompatibleInterface(self, iid, exc);
  179. }
  180. #endif /* OVERRIDE_winfp_getInterface */
  181.  
  182. #ifdef __cplusplus
  183. extern "C"
  184. #endif
  185. JMC_PUBLIC_API(void*)
  186. _winfp_getBackwardCompatibleInterface(struct winfp* self,
  187.                                     const JMCInterfaceID* iid,
  188.                                     struct JMCException* *exceptionThrown)
  189. {
  190.     return (NULL);
  191. }
  192.  
  193. #ifdef __cplusplus
  194. extern "C"
  195. #endif
  196. JMC_PUBLIC_API(void)
  197. _winfp_init(struct winfp* self, struct JMCException* *exceptionThrown,
  198.           struct nffbp *broker)
  199. {
  200.     /* FONTDISPLAYER developers:
  201.      * This is supposed to do initialization that is required for the 
  202.      * font Displayer object.
  203.      */
  204.     // get a reference to my implementation data.
  205.  
  206.     struct winfpImpl *pSampleDisplayerData = winfp2winfpImpl(self);
  207.  
  208.     
  209.     pSampleDisplayerData->m_pBrokerObj   = broker;
  210.     global_pBrokerObj                   = broker;
  211.  
  212.     /*** The following list need not be maintained at all. - dp
  213.     pSampleDisplayerData->m_pPrimeFontList   = NULL;
  214.     ***/
  215.  
  216. }
  217.  
  218. #ifdef OVERRIDE_winfp_finalize
  219. #ifdef __cplusplus
  220. extern "C"
  221. #endif
  222. JMC_PUBLIC_API(void)
  223. _winfp_finalize(struct winfp* self, jint op, JMCException* *exception)
  224. {
  225.     /* FONTDISPLAYER developer:
  226.      * Free any private data for this object here.
  227.      */
  228.  
  229.     struct winfpImpl *oimpl = winfp2winfpImpl(self);
  230.     
  231.     /* Finally, free the memory for the object containter. */
  232.     XP_FREEIF(self);
  233. }
  234. #endif /* OVERRIDE_winfp_finalize */
  235.  
  236. /****************************************************************************
  237.  *                 Implementation of Object specific methods
  238.  *
  239.  * FONTDISPLAYER developers:
  240.  ****************************************************************************/
  241.  
  242. enum  { 
  243.     MatchMask_faceName    = 0x01,
  244.     MatchMask_italic    = 0x02,
  245.     MatchMask_weight    = 0x04,
  246.     MatchMask_charSet   = 0x08,
  247.     MatchMask_underline = 0x10,
  248.     MatchMask_strikeOut = 0x20,
  249. };
  250.  
  251. static BOOL matchItalic( LOGFONT *pWanted,    LOGFONT    *pFound)
  252. {
  253.     //if( pFound->lfPitchAndFamily & TMPF_VECTOR )    // 0x02
  254.     //    return( TRUE );
  255.  
  256.     if( pFound->lfPitchAndFamily & TMPF_TRUETYPE )  // 0x04
  257.         return( TRUE );
  258.  
  259.     if( pWanted->lfItalic && pFound->lfItalic )
  260.         return( TRUE );
  261.  
  262.     if( (! pWanted->lfItalic ) && (! pFound->lfItalic) ) 
  263.         return(TRUE);
  264.     
  265.     return(FALSE);
  266. }
  267.  
  268. typedef struct fontMatch_s {
  269.     int            matchRating;
  270.     int            mask;
  271.     LOGFONT        *pWantedFont;
  272. }  *pFontMatch_t;
  273.  
  274.  
  275. #ifdef not_always_return_a_font
  276. Because we don't have closest-matching now, I, the default font displayer,
  277. always return a closest font.
  278.  
  279. I use Windows font matching mechanism.
  280.  
  281. When the font broker can do closest-matching, remove this comment-out.
  282. #endif // not_always_return_a_font
  283.  
  284. #ifdef __cplusplus
  285. extern "C"
  286. #endif
  287. int CALLBACK 
  288. #ifndef XP_WIN32 
  289. _export 
  290. #endif // no _export in windows 32
  291.  
  292. #ifdef WIN32
  293. sampleEnumFontMatchProc(
  294.     ENUMLOGFONT FAR *lpelf,        // pointer to logical-font data 
  295.     NEWTEXTMETRIC FAR *lpntm,    // pointer to physical-font data 
  296.     int FontType,                // type of font 
  297.     LPARAM lParam                 // address of application-defined data  
  298.     )
  299. #else
  300. sampleEnumFontMatchProc(
  301.     LOGFONT FAR* lpelf,            /* address of structure with logical-font data    */
  302.     TEXTMETRIC FAR* lpntm,        /* address of structure with physical-font data    */
  303.     int FontType,                /* type of font    */
  304.     LPARAM lParam                /* address of application-defined data    */
  305.     )
  306. #endif
  307. {
  308.     pFontMatch_t    pMatchData;
  309.     LOGFONT            *pWanted;
  310.     LOGFONT            *pFound;
  311.     pMatchData = ( pFontMatch_t ) lParam;
  312.  
  313.     if( pMatchData == NULL )
  314.         return(0);          //stop enumerating, todo error handling
  315.  
  316.     pMatchData->matchRating = 0;        // assuming no match 
  317.     pWanted                    = pMatchData->pWantedFont;
  318.  
  319. #ifdef WIN32
  320.     pFound                    = &(lpelf->elfLogFont);
  321. #else
  322.     pFound                     = lpelf;
  323. #endif
  324.     
  325.     // no need to check faceName, if it doesn't match EnumFontFamilies()
  326.     // would not call this.
  327.     // some fonts can draw as italic, but not shown in the LOGFONT 
  328.  
  329. #ifdef match_all_webfont_attributes
  330.     // For Navigator 4.0, 2/28/1997
  331.     // Navigator goes through a font face name list, calls HasFaceName(
  332.     // fontFaceName ) to see the font is avaliable, if not use default
  333.     // font specified in preference.
  334.     // HasFaceName() only asks for the face name. 
  335.     // The face name is there, so the return value of HasFaceName is
  336.     // positive. Navigator stop the font search.
  337.     // Then Navigator asks to create font with all attributes it wants,
  338.     // if is asks for italic, but the given font doesn't has italic,
  339.     // no font is returned to Navigator, and the text will not draw.
  340.  
  341.     //  "Arial blak" font doesn't have italic returned here.
  342.     // see below for more reasons, not checking them.
  343.     if( pMatchData->mask & MatchMask_italic ) {
  344.         if( ! matchItalic(pWanted, pFound ) ) {
  345.             return(1);                            // continue enumerating
  346.         // else, fall through
  347.         }
  348.     }
  349.  
  350.     // When "script" font is the default font in user's  preference,
  351.     // We must return a font for it.
  352.     // "script" font doesn't have underLine returned here,
  353.     // but if use LOGFONT with underLine to create a script font,
  354.     // it will draw with underLine.
  355.     // So, we cannot check underLine here, otherwise, we will
  356.     // miss script-underLine
  357.     // It also happens for script weight.
  358.     // TODO, need to investigate other font, and other attributes.
  359.     if( pMatchData->mask & MatchMask_underline ) {
  360.         if( ! (FontType & TRUETYPE_FONTTYPE ) 
  361.             && pWanted->lfUnderline != pFound->lfUnderline  ) {
  362.             return(1);                            // continue enumerating
  363.         // else, fall through
  364.         }
  365.     }
  366.  
  367.     if( pMatchData->mask & MatchMask_strikeOut ) {
  368.         if( ! ( FontType & TRUETYPE_FONTTYPE ) 
  369.             && pWanted->lfStrikeOut != pFound->lfStrikeOut  ) {
  370.             return(1);                            // continue enumerating
  371.         // else, fall through
  372.         }
  373.     }
  374.  
  375.     if( pMatchData->mask & MatchMask_weight ) {
  376.         if( ! (FontType & TRUETYPE_FONTTYPE ) 
  377.             && ( pWanted->lfWeight != pFound->lfWeight ) ){
  378.             // match failed
  379.             return(1);                            // continue enumerating
  380.         }
  381.     }
  382. #endif    // match_all_webfont_attributes
  383.  
  384.     if( pMatchData->mask & MatchMask_charSet ) {
  385.         if( pWanted->lfCharSet != pFound->lfCharSet ) {
  386.             // match failed
  387.             return(1);                            // continue enumerating
  388.         }
  389.     }
  390.  
  391.     // reaching this point means we passed all required match!
  392.     pMatchData->matchRating    = 1;                // got it
  393.  
  394.     // save the returned font attributs.
  395.     // todo close-match flag
  396.     // memcpy( pWanted, pFound, sizeof(LOGFONT) ); 
  397.  
  398.     return(0);          // no more enumerating
  399. }
  400. // #endif // not_always_return_a_font
  401.  
  402.  
  403. static BYTE
  404. convertCharSetToID( char *pStr)
  405. {
  406.     // refer to:
  407.     //          d:\ns\include\csid.h
  408.     //            d:\ns\lib\libi18n\csnametb.c
  409.     //          d:\ns\cmd\winfe\intlwin.cpp(1185):
  410.     //            ns\lib\libi18n\csnamefv.c, int16 INTL_CharSetNameToID(char    *charset)
  411.     //            ns\cmd\winfe\intlwin.cpp,    charsetlist = "iso-8859-1,x-cp1250,x-cp1251,
  412.     //            ns\lib\libi18n\csnamefn.c   int16 INTL_CharSetNameToID(char    *charset)
  413.  
  414.     // For the round-trip conversion between 
  415.     //    convertCharSetToID() and convertToFmiCharset() 
  416.  
  417.     int    nn;
  418.     if( FALSE == mapStrToInt( charSetIdMap, pStr, &nn, strIntMapNoCase /*flag*/ ) ) {
  419.         return( DEFAULT_CHARSET );
  420.     }
  421.     return( (BYTE)nn );
  422.  
  423. }
  424.  
  425. EXTERN_C
  426. static char *
  427. convertToFmiCharset( LOGFONT *pLogFont )
  428. {
  429.     char * pStr;
  430.     if( FALSE == mapIntToStr(charSetIdMap, pLogFont->lfCharSet, &pStr) ) {
  431.         pStr = "DEFAULT_CHARSET";
  432.     }
  433.  
  434.     return( pStr );
  435. }
  436.  
  437.  
  438. #ifdef __cplusplus
  439. extern "C"
  440. #endif
  441. JMC_PUBLIC_API(void *)
  442. _winfp_LookupFont(struct winfp* self, jint op, struct nfrc* rc,
  443.                 struct nffmi* pFmi, const char *accessor,
  444.                 struct JMCException* *exceptionThrown)
  445. {
  446.     struct winfpImpl        *pSampleDisplayerData;
  447.     struct rc_data        RenderingContextData;
  448.     char                *pFMIfontName;
  449.     char                *pStr;
  450.     struct fontMatch_s    matchData;
  451.     LONG                nTemp;
  452.     pPrimeFont_t        pPrmFont;
  453.     LOGFONT             tempLogFont;
  454.     
  455.     int                    nEncode            = encode_notUnicode;    
  456.     int                    csID            = DEFAULT_CHARSET;
  457.     int                    wanted_Weight    = FW_DONTCARE;
  458.  
  459.     // get a reference to my implementation data.
  460.     pSampleDisplayerData     = winfp2winfpImpl(self);
  461.  
  462.     // Get platform specific RenderingContext data.
  463.     // For Windows, it is a HDC.
  464.     RenderingContextData = nfrc_GetPlatformData( rc, NULL/*exception*/ ); 
  465.  
  466.     if( RenderingContextData.majorType != NF_RC_DIRECT )
  467.         return(NULL);
  468.  
  469.     // prepare the structure for sampleEnumFontMatchProc
  470.     matchData.matchRating   = 0;
  471.     matchData.mask          = 0;
  472.     matchData.pWantedFont   = &tempLogFont;        // pass in the wanted, and pass back the found.
  473.  
  474.     // get asked font attributes from nffmi , and fill in LOGFONT structure.
  475.     // fmi attribute names are defined in nf.h
  476.     memset( &tempLogFont, 0, sizeof( LOGFONT) );
  477.  
  478.     pFMIfontName               = (char *) nffmi_GetValue(pFmi, nfFmiName, NULL );
  479.     if( pFMIfontName )
  480.         strncpy(tempLogFont.lfFaceName, pFMIfontName, LF_FACESIZE );
  481.     // no need to match faceName, it is handled by EnumFontFamilies()
  482.     // matchData.mask          |= MatchMask_faceName;
  483.  
  484.     // encoding is not a LOGFONT attribute on Windows, so it is not part of matching.
  485.     // It is used at draw time to decide using ::DrawText() or ::DrawTextW()
  486.     pStr = (char *)nffmi_GetValue(pFmi, nfFmiEncoding, NULL );
  487.     if( pStr && strcmp(pStr, "Unicode") == 0 ) {
  488.         nEncode     |=  encode_unicode;  //todo 
  489.     }
  490.  
  491.     nTemp = (int) nffmi_GetValue(pFmi, nfFmiStyle, NULL );
  492.     if( nTemp == nfStyleItalic ) {
  493.         tempLogFont.lfItalic  = 1;
  494.         matchData.mask        |= MatchMask_italic ;
  495.     }
  496.  
  497.     // Because win95 unicode cannot draw underline correctly, 
  498.     // the font displayer ignore the attribute, and draw a line under text.
  499.     // For a quick fix, we don't care it is NT or 95.
  500.     // remove this hack when win95 bug is fixed.
  501.  
  502.     // use underline when it is not uncode.
  503.     nTemp = (int) nffmi_GetValue(pFmi, nfFmiUnderline, NULL );
  504.     if( nTemp != nfUnderlineDontCare ) {
  505.         if( nTemp == nfUnderlineYes) {
  506.             if( nEncode & encode_unicode ) {
  507.                 nEncode |= encode_needDrawUnderline;
  508.             } else {
  509.                 tempLogFont.lfUnderline  =  1 ;
  510.                 matchData.mask        |= MatchMask_underline ;
  511.             }
  512.         }
  513.     }
  514.  
  515.  
  516.     nTemp = (int) nffmi_GetValue(pFmi, nfFmiStrikeOut, NULL );
  517.     if( nTemp != nfStrikeOutDontCare ) {
  518.         tempLogFont.lfStrikeOut  = (nTemp == nfStrikeOutYes) ? 1 : 0;
  519.         matchData.mask        |= MatchMask_strikeOut ;
  520.     }
  521.  
  522.     wanted_Weight = (int) nffmi_GetValue(pFmi, nfFmiWeight, NULL );
  523.     if( wanted_Weight > nfWeightDontCare ) {
  524.         tempLogFont.lfWeight  = wanted_Weight;
  525.         matchData.mask        |= MatchMask_weight ;
  526.     }
  527.  
  528.     tempLogFont.lfCharSet  = DEFAULT_CHARSET ;
  529. #ifdef WIN32            // DP_TRYING_WIN16_JAPANESE
  530.     pStr = (char *) nffmi_GetValue(pFmi, nfFmiCharset, NULL );
  531.     if( pStr != NULL ) {
  532.         csID    = convertCharSetToID( pStr);
  533.         tempLogFont.lfCharSet    =  csID;
  534.         //todo matchData.mask                      |= MatchMask_charSet ;
  535.     }
  536. #endif
  537.  
  538. #ifdef not_always_return_a_font
  539. Because we don't have closest-matching now, I, the default font displayer,
  540. always return a closest font.
  541.  
  542. I use Windows font matching mechanism.
  543.  
  544. When the font broker can do closest-matching, remove this comment-out.
  545. I will return null if asked font are not found.
  546. #endif    // not_always_return_a_font
  547.  
  548. #ifdef WIN32
  549.     ::EnumFontFamilies( 
  550.         (HDC) RenderingContextData.t.directRc.dc,
  551.         (LPCTSTR)    pFMIfontName, 
  552.         (FONTENUMPROC) sampleEnumFontMatchProc, 
  553.         (LPARAM) &matchData
  554.         );
  555. #else
  556.     ::EnumFontFamilies( 
  557.         (HDC) RenderingContextData.t.directRc.dc,
  558.         (LPCSTR) pFMIfontName, 
  559.         (FONTENUMPROC) sampleEnumFontMatchProc, 
  560.         (char FAR *)&matchData
  561.         );
  562. #endif
  563.  
  564.     if( matchData.matchRating == 0 ) {    
  565.         // failed to match the asked font.
  566.         return(NULL);   // return NULL means we don't support the font asked in pFmi.
  567.     }
  568. // #endif    // not_always_return_a_font
  569.  
  570.     // we have the font in tempLogFont, matched or not.
  571.     // save it and pass the pointer to caller.
  572.  
  573.     // A font struct is created here, and return the a fontHandle to 
  574.     // Font broker.
  575.     // Font broker doesn't touch the handle, but will pass me(Displayer)
  576.     // the handle to create a renderable font.
  577.     // release font object/struct in _winfp_ReleaseFontHandle
  578.  
  579.     pPrmFont = (pPrimeFont_t) malloc ( sizeof( struct NetscapePrimeFont_s ) );
  580.     
  581.     pPrmFont->csIDInPrimeFont        = csID;
  582.     pPrmFont->encordingInPrimeFont    = nEncode;  
  583.     nTemp                           = (int) nffmi_GetValue(pFmi, nfFmiResolutionY, NULL );
  584.     pPrmFont->YPixelPerInch        = nTemp > 0 ? nTemp : 96 ;    // 96 is the best guest 
  585.  
  586.     // save the font info
  587.     memcpy( &(pPrmFont->logFontInPrimeFont), &tempLogFont, sizeof(LOGFONT) ); 
  588.     
  589.     // todo best-match flag
  590.     // pPrmFont->logFontInPrimeFont.lfWeight = wanted_Weight;
  591.  
  592.     /*** The following list need not be maintained at all. - dp
  593.     // link it (for release memory in _winfp_ReleaseFontHandle )
  594.     pPrmFont->nextFont = pSampleDisplayerData->m_pPrimeFontList;
  595.     pSampleDisplayerData->m_pPrimeFontList = pPrmFont;
  596.     ***/
  597.  
  598.     return( pPrmFont );
  599. }
  600.  
  601. #ifdef __cplusplus
  602. extern "C"
  603. #endif
  604. JMC_PUBLIC_API(struct nfstrm *)
  605. _winfp_CreateFontStreamHandler(struct winfp* self, jint op, struct nfrc *rc,
  606.                                const char *urlOfPage,
  607.                                struct JMCException* *exceptionThrown)
  608. {
  609.     struct winfpImpl *pSampleDisplayerData = winfp2winfpImpl(self);
  610.     /* XXX Need to pass the fontname and url here */
  611.     //struct nfstrm *strm = (struct nfstrm *)cstrmFactory_Create(NULL, rc, NULL, NULL);
  612.     //return strm;
  613.     return NULL;
  614. }
  615.  
  616.  
  617. #ifdef __cplusplus
  618. extern "C"
  619. #endif
  620. JMC_PUBLIC_API(void *)
  621. _winfp_CreateFontFromFile(struct winfp* self, jint op,
  622.                           struct nfrc *rc, const char *mimetype,
  623.                           const char *fontfilename,
  624.                           const char *urlOfPage,
  625.                           struct JMCException* *exceptionThrown)
  626. {
  627.     // This is required only for Displayers that do webfonts
  628.     return (NULL);
  629. }
  630.  
  631.  
  632. #ifdef __cplusplus
  633. extern "C"
  634. #endif
  635. JMC_PUBLIC_API(jint)    
  636. _winfp_ReleaseFontHandle(struct winfp* self, jint op, void *fh,
  637.                        struct JMCException* *exceptionThrown)
  638. {
  639.     struct winfpImpl *pSampleDisplayerData = winfp2winfpImpl(self);
  640.  
  641.     if (fh)
  642.         free(fh);
  643.  
  644.     /*** The following list need not be maintained at all. - dp
  645.     // free the font list
  646.     pPrimeFont_t        pPrmFont, pPrmFont2;
  647.     pPrmFont = pPrmFont2 = pSampleDisplayerData->m_pPrimeFontList;
  648.     while( pPrmFont != NULL ) {
  649.         pPrmFont2 = pPrmFont2->nextFont;
  650.         free( pPrmFont );
  651.         pPrmFont = pPrmFont2;
  652.     }
  653.     pSampleDisplayerData->m_pPrimeFontList = NULL;
  654.     ***/
  655.  
  656.     return (0);
  657. }
  658.  
  659. // assuming a font can have 50 sizes
  660. #define MAXSIZECOUNT    50
  661. typedef struct fontSize_s {
  662.     int  sizeTable[ MAXSIZECOUNT +1 ];   // +1 for terminator
  663.     int     sizeCount;
  664. }  *pFontSize_t;
  665.  
  666. #ifdef __cplusplus
  667. extern "C"
  668. #endif
  669. int CALLBACK 
  670. #ifndef WIN32 
  671. _export 
  672. #endif // no _export in windows 32
  673.  
  674. #ifdef WIN32
  675. sampleEnumFontSizeProc(
  676.     ENUMLOGFONT FAR *lpelf,        // pointer to logical-font data 
  677.     NEWTEXTMETRIC FAR *lpntm,    // pointer to physical-font data 
  678.     int FontType,                // type of font 
  679.     LPARAM lParam                 // address of application-defined data  
  680.     )
  681. #else
  682. sampleEnumFontSizeProc(
  683.     LOGFONT FAR* lpelf,            /* address of structure with logical-font data    */
  684.     TEXTMETRIC FAR* lpntm,        /* address of structure with physical-font data    */
  685.     int FontType,                /* type of font    */
  686.     LPARAM lParam                /* address of application-defined data    */
  687.     )
  688. #endif
  689. {
  690.     pFontSize_t  pfs;
  691.     pfs = ( pFontSize_t ) lParam;
  692.     if( pfs == NULL )
  693.         return(0);          //stop enumerating
  694.  
  695.     LOGFONT            *pFound;
  696. #ifdef WIN32
  697.     pFound                    = &(lpelf->elfLogFont);
  698. #else
  699.     pFound                     = lpelf;
  700. #endif
  701.  
  702.     if( pfs->sizeCount >= MAXSIZECOUNT )
  703.         return(0);          //stop enumerating
  704.  
  705.     // todo, convert to pointsize !!
  706.     // save the size
  707.     pfs->sizeTable[ pfs->sizeCount++ ] = pFound->lfHeight;
  708.  
  709.     return(1);          // continue enumerating
  710. }
  711.  
  712.  
  713. #ifdef __cplusplus
  714. extern "C"
  715. #endif
  716. JMC_PUBLIC_API(void*)
  717. _winfp_EnumerateSizes(struct winfp* self, jint op, struct nfrc* rc, void * fh,
  718.                     struct JMCException* *exceptionThrown)
  719. {
  720.     struct winfpImpl  *pSampleDisplayerData;
  721.     HDC             hDC;
  722.     char            *faceName = NULL;
  723.     pPrimeFont_t    pPrmFont;
  724.     //todo re fefine the struct to use malloc space.
  725.     static struct fontSize_s fontSizeTable;    //todo malloc space?
  726.     jdouble            *newtable = NULL;               // continuing the above hack...
  727.     int                ii;
  728.     struct rc_data    RenderingContextData;
  729.  
  730. #ifdef try_broker
  731.     RenderingContextData = nfrc_GetPlatformData( rc, NULL/*exception*/ ); 
  732.     if( RenderingContextData.majorType != NF_RC_DIRECT )
  733.         return(NULL);
  734.  
  735.     hDC = (HDC) RenderingContextData.t.directRc.dc;
  736.  
  737.     pSampleDisplayerData = winfp2winfpImpl(self);
  738.     pPrmFont             = (pPrimeFont_t) fh;
  739.     if( pPrmFont == NULL )
  740.         return(NULL);
  741.  
  742.     fontSizeTable.sizeCount = 0;
  743.     faceName    = pPrmFont->logFontInPrimeFont.lfFaceName;
  744.  
  745.     if( faceName != NULL ) {
  746.         // get all sizes the font supports
  747.  
  748.  
  749. #ifdef WIN32
  750.     ::EnumFontFamilies( 
  751.         hDC,
  752.         (LPCTSTR)    faceName, 
  753.         (FONTENUMPROC) sampleEnumFontSizeProc, 
  754.         (LPARAM) &fontSizeTable
  755.         );
  756. #else
  757.     ::EnumFontFamilies( 
  758.         hDC,
  759.         (LPCSTR) faceName, 
  760.         (FONTENUMPROC) sampleEnumFontSizeProc, 
  761.         (char FAR *)&fontSizeTable
  762.         );
  763. #endif
  764.     }
  765.     
  766.     fontSizeTable.sizeTable[ fontSizeTable.sizeCount ] = -1; // terminator of the array.
  767.     
  768.     if( fontSizeTable.sizeCount < 1 )
  769.         return(NULL);
  770.         
  771.     // Allocate the sizeTable using nffbu::malloc() and return it to the caller.
  772.     // Caller is expected to free this.
  773.     newtable = (jdouble *)
  774.         nffbu_malloc(getFontBrokerUtilityInterface(global_pBrokerObj),
  775.                     sizeof(*newtable)*(fontSizeTable.sizeCount+1), NULL);
  776.     if (!newtable)
  777.         return( NULL );
  778.     for (ii=0; ii<=fontSizeTable.sizeCount; ii++)
  779.         newtable[ii] = fontSizeTable.sizeTable[ii];
  780.  
  781.     // return non-NULL means we support sizes in sizeTable returned.
  782.     return( newtable );
  783. #endif    // try_broker
  784.  
  785.     /*
  786.     newtable = (jdouble *)
  787.         nffbu_malloc(getFontBrokerUtilityInterface(global_pBrokerObj),
  788.                     sizeof(*newtable)*(2), NULL);
  789.     if (!newtable)
  790.         return( NULL );
  791.  
  792.     newtable[0] = 10.0;
  793.     newtable[1] = -1;
  794.     return(    newtable );        
  795.     */
  796.     return(NULL);
  797.  
  798. }
  799.  
  800.  
  801. #ifdef __cplusplus
  802. extern "C"
  803. #endif
  804. JMC_PUBLIC_API(struct nffmi*)
  805. _winfp_GetMatchInfo(struct winfp* self, jint op, void * fh,
  806.                   struct JMCException* *exceptionThrown)
  807. {
  808.     struct winfpImpl *pSampleDisplayerData = winfp2winfpImpl(self);
  809.     pPrimeFont_t   pPrmFont;
  810.  
  811.     pPrmFont  = ( pPrimeFont_t ) fh;
  812.     if( pPrmFont == NULL )
  813.         return(NULL);
  814.  
  815.     return NULL;
  816. }
  817.  
  818.  
  819. #ifdef __cplusplus
  820. extern "C"
  821. #endif
  822. JMC_PUBLIC_API(struct nfrf*)
  823. _winfp_GetRenderableFont(struct winfp* self, jint op, struct nfrc* rc, void *fh, 
  824.                        jdouble inputPointSize, struct JMCException* *exceptionThrown)
  825. {
  826.     struct winfpImpl      *pSampleDisplayerData;
  827.     struct winrf          *pSampleRenderableFont;
  828.     struct winrfImpl      *pSampleRFdata ;
  829.     struct nfrf         *pSampleRenderableFontInterface;
  830.     pPrimeFont_t        pPrmFont;
  831.     HDC                    hDC;
  832.     struct rc_data        RenderingContextData;
  833.     jdouble                pointSize = inputPointSize;
  834.     int                    pixelSize = 0;
  835.     HFONT           hFont;
  836.     HFONT           hOldFont;
  837.     TEXTMETRIC      textMetric;
  838.     int                reCode;
  839.  
  840.     RenderingContextData = nfrc_GetPlatformData( rc, NULL/*exception*/ ); 
  841.     if( RenderingContextData.majorType != NF_RC_DIRECT )
  842.         return(NULL);
  843.  
  844.     hDC = (HDC) RenderingContextData.t.directRc.dc;
  845.  
  846.     // get a pointor to my implement data of FontDisplayer.
  847.     pSampleDisplayerData = winfp2winfpImpl(self);
  848.  
  849.     pPrmFont             = ( pPrimeFont_t ) fh;
  850.     if( pPrmFont == NULL )
  851.         return(NULL);
  852.  
  853.     //TODO hard code font
  854.     // create a renderable font
  855.     pSampleRenderableFont = winrfFactory_Create(NULL);
  856.  
  857.     if (! pSampleRenderableFont ) {
  858.         return( NULL );
  859.     }
  860.  
  861.     // get a pointor to my implement data in renderable font
  862.     pSampleRFdata = winrf2winrfImpl(pSampleRenderableFont);
  863.  
  864.     // create font, need to call DeleteObject for pSampleRFdata->m_hFontInRF
  865.     // in _winrf_finalize()
  866.  
  867.     // make sure pointsize is positive    
  868.     if( pointSize < 0 ) {
  869.         pointSize = - pointSize;    // webfont use positive size. see winfe\nsfont.cpp
  870.     }
  871.     // pointsize = -MulDiv(pointsize,::GetDeviceCaps(hDC, LOGPIXELSY), 72);
  872.  
  873.     // for passing to Windows, convert pointsize to pixel size and negative.
  874.     if( pointSize > 0 )
  875.         pixelSize = (int) - pointSize * pPrmFont->YPixelPerInch / 72.0;
  876.  
  877.     POINT size;
  878.     size.x = 0;
  879.     size.y = pixelSize;
  880.     ::DPtoLP(hDC, &size, 1);
  881.  
  882.     pPrmFont->logFontInPrimeFont.lfHeight = (int) size.y;
  883.     pPrmFont->logFontInPrimeFont.lfWidth  = 0;
  884.     pSampleRFdata->mRF_hFont = CreateFontIndirect( & pPrmFont->logFontInPrimeFont );    
  885.  
  886.     pSampleRFdata->mRF_csID        = pPrmFont->csIDInPrimeFont;
  887.     pSampleRFdata->mRF_encoding = pPrmFont->encordingInPrimeFont;
  888.  
  889.     // cache attributes now, when consumer asking a sttribute, no rc
  890.     // is passed in.
  891.         // HDC             hDC;
  892.  
  893.         hFont    = pSampleRFdata->mRF_hFont;
  894.  
  895.         hOldFont = SelectObject( hDC, hFont);
  896.  
  897.         reCode = GetTextMetrics( hDC, &textMetric );
  898.  
  899.         // put the old font back after drawtext.
  900.         SelectObject( hDC, hOldFont);
  901.  
  902.         if( reCode )  {   // got it
  903.             pSampleRFdata->mRF_tmDescent        = textMetric.tmDescent;
  904.             pSampleRFdata->mRF_tmMaxCharWidth    = textMetric.tmMaxCharWidth;
  905.             pSampleRFdata->mRF_tmAscent            = textMetric.tmAscent;
  906.             pSampleRFdata->mRF_pointSize        = inputPointSize;
  907.         }
  908.     
  909.     // get the renderable font interace for return value.
  910.     pSampleRenderableFontInterface = (struct nfrf*)winrf_getInterface( pSampleRenderableFont, &nfrf_ID, NULL );
  911.  
  912.     // return the renderable font interface.
  913.     // return NULL if we cannot create the renderable font.
  914.     return( pSampleRenderableFontInterface );     
  915. }
  916.  
  917.  
  918. #ifdef __cplusplus
  919. extern "C"
  920. #endif
  921. JMC_PUBLIC_API(const char*)
  922. _winfp_Name(struct winfp* self, jint op, struct JMCException* *exceptionThrown)
  923. {
  924.     struct winfpImpl *pSampleDisplayerData = winfp2winfpImpl(self);
  925.     return (NF_NATIVE_FONT_DISPLAYER);
  926. }
  927.  
  928.  
  929. #ifdef __cplusplus
  930. extern "C"
  931. #endif
  932. JMC_PUBLIC_API(const char*)
  933. _winfp_Description(struct winfp* self, jint op,
  934.                  struct JMCException* *exceptionThrown)
  935. {
  936.     struct winfpImpl *pSampleDisplayerData = winfp2winfpImpl(self);
  937.     //TODO
  938.     return NULL;
  939. }
  940.  
  941.  
  942. int
  943. convertToFmiPitch( LOGFONT *pLogFont )
  944. {
  945.     if( pLogFont->lfPitchAndFamily == FIXED_PITCH )
  946.         return( nfSpacingMonospaced );
  947.  
  948.     if( pLogFont->lfPitchAndFamily == VARIABLE_PITCH )
  949.         return( nfSpacingProportional );
  950.  
  951.     return( nfSpacingDontCare );
  952. }
  953.  
  954. EXTERN_C struct nffmi*
  955. createFMIfromLOGFONT( LOGFONT *pLogFont )
  956. {
  957.  
  958.     nffmi*  pFmi;
  959.     char    *FmiName;
  960.     char    *FmiCharset;
  961.     char    *FmiEncoding;
  962.     int     FmiWeight;
  963.     int     FmiPitch;
  964.     int     FmiStyle;
  965.     int        FmiUnderline;
  966.     int        FmiStrikeOut;
  967.    //int     *nfFmiPanose"           int[]
  968.     int     FmiPixelPerInchX;
  969.     int     FmiPixelPerInchY;
  970.  
  971.     FmiName         = pLogFont->lfFaceName;
  972.     FmiCharset      = convertToFmiCharset( pLogFont );
  973.     FmiEncoding     = "1";                // only "unicode" is used for select TextOuuW
  974.     FmiWeight       = pLogFont->lfWeight;
  975.     FmiPitch        = convertToFmiPitch( pLogFont );
  976.     FmiStyle        = (pLogFont->lfItalic )? nfStyleItalic : nfStyleDontCare ;
  977.     FmiUnderline    = (pLogFont->lfUnderline) ? nfUnderlineYes : nfUnderlineDontCare ;
  978.     FmiStrikeOut    = (pLogFont->lfStrikeOut) ? nfStrikeOutYes : nfStrikeOutDontCare ;
  979.     FmiPixelPerInchX  = 0;
  980.     FmiPixelPerInchY  = 0;
  981.  
  982.     pFmi = nffbu_CreateFontMatchInfo( 
  983.         getFontBrokerUtilityInterface( global_pBrokerObj ),
  984.         FmiName,
  985.         FmiCharset,
  986.         FmiEncoding,
  987.         FmiWeight,
  988.         FmiPitch,
  989.         FmiStyle,
  990.         FmiUnderline,
  991.         FmiStrikeOut,
  992.         FmiPixelPerInchX,                          
  993.         FmiPixelPerInchY,                          
  994.         NULL /* exception */
  995.         );
  996.  
  997.     return( pFmi );        
  998. }
  999.  
  1000. // todo assuming a font can have 50 
  1001. #define MAXfmiCOUNT    80
  1002. typedef struct fmiList_s {
  1003.     struct nffmi    **fmiTable;
  1004.     int             fmiCount;
  1005. }  *pFmiList_t;
  1006.  
  1007.  
  1008. #ifdef __cplusplus
  1009. extern "C"
  1010. #endif
  1011. int CALLBACK 
  1012. #ifndef WIN32 
  1013. _export 
  1014. #endif // no _export in windows 32
  1015.  
  1016. #ifdef WIN32
  1017. sampleEnumFontListFmiProc(
  1018.     ENUMLOGFONT FAR *lpelf,        // pointer to logical-font data 
  1019.     NEWTEXTMETRIC FAR *lpntm,    // pointer to physical-font data 
  1020.     int FontType,                // type of font 
  1021.     LPARAM lParam                 // address of application-defined data  
  1022.     )
  1023. #else
  1024. sampleEnumFontListFmiProc(
  1025.     LOGFONT FAR* lpelf,            /* address of structure with logical-font data    */
  1026.     TEXTMETRIC FAR* lpntm,        /* address of structure with physical-font data    */
  1027.     int FontType,                /* type of font    */
  1028.     LPARAM lParam                /* address of application-defined data    */
  1029.     )
  1030. #endif
  1031. {
  1032.     LOGFONT            *pFound;
  1033. #ifdef WIN32
  1034.     pFound                    = &(lpelf->elfLogFont);
  1035. #else
  1036.     pFound                     = lpelf;
  1037. #endif
  1038.  
  1039.     pFmiList_t  pfmiList;
  1040.     pfmiList = ( pFmiList_t ) lParam;
  1041.     if( pfmiList == NULL )
  1042.         return(0);          //stop enumerating
  1043.  
  1044.     if( pfmiList->fmiCount >= MAXfmiCOUNT )
  1045.         return(0);          //stop enumerating
  1046.  
  1047.     // (lpelf->elfLogFont).lfHeight;
  1048.     pfmiList->fmiTable[ pfmiList->fmiCount++ ] = createFMIfromLOGFONT( pFound );
  1049.  
  1050.     return(1);          // continue enumerating
  1051. }
  1052.  
  1053. #ifdef __cplusplus
  1054. extern "C"
  1055. #endif
  1056. JMC_PUBLIC_API(void*)
  1057. _winfp_ListFonts(struct winfp* self, jint op,
  1058.                     struct nfrc* rc, struct nffmi* pFmi,
  1059.                     struct JMCException* *exceptionThrown)
  1060. {
  1061.     struct winfpImpl      *pSampleDisplayerData = winfp2winfpImpl(self);
  1062.     struct rc_data        RenderingContextData;
  1063.     struct fmiList_s    fmiList;
  1064.  
  1065.     RenderingContextData = nfrc_GetPlatformData( rc, NULL/*exception*/ ); 
  1066.     if( RenderingContextData.majorType != NF_RC_DIRECT )
  1067.         return(NULL);
  1068.  
  1069.     fmiList.fmiTable = (struct nffmi **)   nffbu_malloc( 
  1070.                     getFontBrokerUtilityInterface(pSampleDisplayerData->m_pBrokerObj),
  1071.                     (MAXfmiCOUNT+1) * sizeof(struct nffmi *), NULL);
  1072.     fmiList.fmiCount = 0;
  1073.  
  1074. #ifdef WIN32
  1075.     ::EnumFontFamilies( 
  1076.         (HDC) RenderingContextData.t.directRc.dc,
  1077.         NULL, 
  1078.         (FONTENUMPROC) sampleEnumFontListFmiProc, 
  1079.         (LPARAM) &fmiList
  1080.         );
  1081. #else
  1082.     ::EnumFontFamilies( 
  1083.         (HDC) RenderingContextData.t.directRc.dc,
  1084.         NULL, 
  1085.         (FONTENUMPROC) sampleEnumFontListFmiProc, 
  1086.         (char FAR *)&fmiList
  1087.         );
  1088. #endif
  1089.  
  1090.     fmiList.fmiTable[ fmiList.fmiCount ] = NULL;    // terminate
  1091.     return( fmiList.fmiTable );     // caller free the memory
  1092. }
  1093.  
  1094.  
  1095. #ifdef __cplusplus
  1096. extern "C"
  1097. #endif
  1098. JMC_PUBLIC_API(void*)
  1099. _winfp_ListSizes(struct winfp* self, jint op, struct nfrc* rc, struct nffmi* pFmi,
  1100.                struct JMCException* *exceptionThrown)
  1101. {
  1102.     struct winfpImpl *pSampleDisplayerData = winfp2winfpImpl(self);
  1103.     //TODO
  1104.     return NULL;
  1105. }
  1106.  
  1107.  
  1108. #ifdef __cplusplus
  1109. extern "C"
  1110. #endif
  1111. JMC_PUBLIC_API(const char*)
  1112. _winfp_EnumerateMimeTypes(struct winfp* self, jint op,
  1113.                         struct JMCException* *exceptionThrown)
  1114. {
  1115.     struct winfpImpl *pSampleDisplayerData = winfp2winfpImpl(self);
  1116.     char *mimetypes = NULL;
  1117.     mimetypes = "Application/TestFontStream:dp:Sample Displayer testing font streaming;";
  1118.     return mimetypes;
  1119. }
  1120.  
  1121.  
  1122. #ifdef __cplusplus
  1123. extern "C"
  1124. #endif
  1125. JMC_PUBLIC_API(jint)
  1126. _winfp_CacheFontInfo(struct winfp* self, jint op,
  1127.                    struct JMCException* *exceptionThrown)
  1128. {
  1129.     struct winfpImpl *pSampleDisplayerData = winfp2winfpImpl(self);
  1130.     //TODO
  1131.     return(0);  // return NULL;
  1132. }
  1133.