home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / libfont / src / cfb.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  23.2 KB  |  849 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.  *
  20.  * Implements a link between the (cfb) CFontBroker Interface implementation
  21.  * and its C++ implementation viz (fb) FontBrokerObject.
  22.  *
  23.  * dp Suresh <dp@netscape.com>
  24.  */
  25.  
  26.  
  27. #include "libfont.h"
  28.  
  29. #include "nf.h"
  30. #include "Mcfb.h"
  31. #include "Pcfb.h"
  32. #include "fb.h"
  33.  
  34. #include "wfList.h"
  35.  
  36.  /* Hack: libnet defines this locally. We need to move it out into a public
  37.   * header like net.h
  38.   */
  39. extern "C" int NET_FindURLInCache(URL_Struct *URL_s, MWContext *ctxt);
  40.  
  41. int wf_trace_flag = 0;
  42.  
  43. #ifdef MOZILLA_CLIENT
  44. #define WF_PREF_ENABLE_WEBFONTS "browser.use_document_fonts"
  45. PR_STATIC_CALLBACK(int)
  46. /*ARGSUSED*/
  47. wf_PrefHandler(const char *pref, void *data)
  48. {
  49.     int32 value = 2;
  50.     struct nffbc *fbc = (struct nffbc *)data;
  51.     struct nffbu *fbu = (struct nffbu *)nffbc_getInterface(fbc, &nffbu_ID, NULL);
  52. #ifndef NO_PREF_CHANGE
  53.     PREF_GetIntPref(WF_PREF_ENABLE_WEBFONTS, &value);
  54. #endif /* NO_PREF_CHANGE */
  55.     if (value == 0 || value == 1)
  56.     {
  57.         nffbu_DisableWebfonts(fbu, NULL);                
  58.     }
  59.     else
  60.     {
  61.         nffbu_EnableWebfonts(fbu, NULL);
  62.     }
  63.     return (0);
  64. }
  65. #endif /* MOZILLA_CLIENT */
  66.  
  67. /*
  68.  * Broker initialization. I would like to put this in libfont.c but
  69.  * since that defines JMC_INITID to define objects, adding this which means
  70.  * adding Mcfb.h would interfere. Hence I am putting this with the cfb stuff.
  71.  */
  72. NF_PUBLIC_API_IMPLEMENT(struct nffbc *)
  73. NF_FontBrokerInitialize()
  74. {
  75.     /* Create the broker */    
  76.     struct nffbc *fbc = (struct nffbc *)cfbFactory_Create(NULL);
  77.  
  78.     /* Register the converters for font streaming */
  79.     NF_RegisterConverters();
  80.  
  81. #ifdef DEBUG
  82.     const char *WF_TRACE = getenv("WF_TRACE");
  83.     if (WF_TRACE)
  84.     {
  85.         wf_trace_flag = atoi(WF_TRACE);
  86.     }
  87. #endif
  88.  
  89. #ifdef MOZILLA_CLIENT
  90. #ifndef NO_PREF_CHANGE
  91.     // Register for preference changes
  92.     PREF_RegisterCallback(WF_PREF_ENABLE_WEBFONTS, wf_PrefHandler, fbc);
  93. #endif /* NO_PREF_CHANGE */
  94.  
  95.     // Initialize the pref by faking a prefchange
  96.     wf_PrefHandler(WF_PREF_ENABLE_WEBFONTS, fbc);
  97. #endif /* MOZILLA_CLIENT */
  98.  
  99.     return (fbc);
  100. }
  101.  
  102.  
  103. /****************************************************************************
  104.  *                 Implementation of common interface methods                    *
  105.  ****************************************************************************/
  106.  
  107. #ifdef OVERRIDE_cfb_getInterface
  108. #include "Mnffbc.h"
  109. #include "Mnffbp.h"
  110. #include "Mnffbu.h"
  111.  
  112. extern "C" JMC_PUBLIC_API(void*)
  113. /* ARGSUSED */
  114. _cfb_getInterface(struct cfb* self, jint op, const JMCInterfaceID* iid, JMCException* *exc)
  115. {
  116.     if (memcmp(iid, &nffbc_ID, sizeof(JMCInterfaceID)) == 0)
  117.         return cfbImpl2cfb(cfb2cfbImpl(self));
  118.     if (memcmp(iid, &nffbp_ID, sizeof(JMCInterfaceID)) == 0)
  119.         return cfbImpl2nffbp(cfb2cfbImpl(self));
  120.     if (memcmp(iid, &nffbu_ID, sizeof(JMCInterfaceID)) == 0)
  121.         return cfbImpl2nffbu(cfb2cfbImpl(self));
  122.     return _cfb_getBackwardCompatibleInterface(self, iid, exc);
  123. }
  124. #endif /* OVERRIDE_cfb_getInterface */
  125.  
  126. extern "C" JMC_PUBLIC_API(void*)
  127. /*ARGSUSED*/
  128. _cfb_getBackwardCompatibleInterface(struct cfb* self,
  129.                                     const JMCInterfaceID* iid,
  130.                                     struct JMCException* *exceptionThrown)
  131. {
  132.     return(NULL);
  133. }
  134.  
  135. extern "C" JMC_PUBLIC_API(void)
  136. /*ARGSUSED*/
  137. _cfb_init(struct cfb* self, struct JMCException* *exceptionThrown)
  138. {
  139.     cfbImpl *oimpl = cfb2cfbImpl(self);
  140.  
  141.     // Populate our global interface variables before creating
  142.     // FontBrokerObject as the constructor uses them.
  143.     WF_fbc = (struct nffbc *) self;
  144.     WF_fbp = cfbImpl2nffbp(cfb2cfbImpl(self));
  145.     WF_fbu = cfbImpl2nffbu(cfb2cfbImpl(self));
  146.  
  147.     FontBrokerObject *fbobj = new FontBrokerObject();
  148.     if (fbobj == NULL)
  149.       {
  150.         WF_fbc = NULL;
  151.         WF_fbp = NULL;
  152.         WF_fbu = NULL;
  153.         JMC_EXCEPTION(exceptionThrown, JMCEXCEPTION_OUT_OF_MEMORY);
  154.       }
  155.     else
  156.       {
  157.         oimpl->object = fbobj;
  158.       }
  159.  
  160.     return;
  161. }
  162.  
  163. #ifdef OVERRIDE_cfb_finalize
  164. extern "C" JMC_PUBLIC_API(void)
  165. /*ARGSUSED*/
  166. _cfb_finalize(struct cfb* self, jint op, JMCException* *exception)
  167. {
  168.     struct cfbImpl *oimpl = cfb2cfbImpl(self);
  169.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  170.     delete fbobj;
  171.     
  172.     /* Finally, free the memory for the object containter. */
  173.     XP_FREEIF(self);
  174. }
  175. #endif /* OVERRIDE_cfb_finalize */
  176.  
  177.  
  178.  
  179.  
  180. /****************************************************************************
  181.  *                 Implementation of Object specific methods                    *
  182.  ****************************************************************************/
  183.  
  184.  
  185. extern "C" JMC_PUBLIC_API(struct nff*)
  186. /*ARGSUSED*/
  187. _cfb_LookupFont(struct cfb* self, jint op, struct nfrc* rc, struct nffmi* fmi,
  188.                 const char *accessor, struct JMCException* *exceptionThrown)
  189. {
  190.     cfbImpl *oimpl = cfb2cfbImpl(self);
  191.     XP_ASSERT(oimpl->header.refcount);
  192.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  193.     return (fbobj->LookupFont(rc, fmi, accessor));
  194. }
  195.  
  196.  
  197. extern "C" JMC_PUBLIC_API(struct nff*)
  198. /*ARGSUSED*/
  199. _cfb_CreateFontFromUrl(struct cfb* self, jint op, struct nfrc* rc,
  200.                        const char* url_of_font, const char *url_of_page,
  201.                        jint faux, struct nfdoer* completion_callback,
  202.                        MWContext* context,
  203.                        struct JMCException* *exceptionThrown)
  204. {
  205.     cfbImpl *oimpl = cfb2cfbImpl(self);
  206.     XP_ASSERT(oimpl->header.refcount);
  207.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  208.     return (fbobj->CreateFontFromUrl(rc, url_of_font, url_of_page, faux, completion_callback, context));
  209. }
  210.  
  211.  
  212. extern "C" JMC_PUBLIC_API(struct nff*)
  213. /*ARGSUSED*/
  214. _cfb_CreateFontFromFile(struct cfb* self, jint op, struct nfrc *rc,
  215.                         const char *mimetype, const char* fontfilename,
  216.                         const char *url_of_page,
  217.                         struct JMCException* *exceptionThrown)
  218. {
  219.     cfbImpl *oimpl = cfb2cfbImpl(self);
  220.     XP_ASSERT(oimpl->header.refcount);
  221.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  222.     return (fbobj->CreateFontFromFile(rc, mimetype, fontfilename, url_of_page));
  223. }
  224.  
  225.  
  226. extern "C" JMC_PUBLIC_API(void*)
  227. /*ARGSUSED*/
  228. _cfb_ListFonts(struct cfb* self, jint op, struct nfrc* rc, struct nffmi* fmi,
  229.                     struct JMCException* *exceptionThrown)
  230. {
  231.     cfbImpl *oimpl = cfb2cfbImpl(self);
  232.     XP_ASSERT(oimpl->header.refcount);
  233.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  234.     return fbobj->ListFonts(rc, fmi);
  235. }
  236.  
  237.  
  238. extern "C" JMC_PUBLIC_API(void*)
  239. /*ARGSUSED*/
  240. _cfb_ListSizes(struct cfb* self, jint op, struct nfrc* rc, struct nffmi* fmi,
  241.                struct JMCException* *exceptionThrown)
  242. {
  243.     cfbImpl *oimpl = cfb2cfbImpl(self);
  244.     XP_ASSERT(oimpl->header.refcount);
  245.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  246.     return fbobj->ListSizes(rc, fmi);
  247. }
  248.  
  249.  
  250. extern "C" JMC_PUBLIC_API(struct nff*)
  251. /*ARGSUSED*/
  252. _cfb_GetBaseFont(struct cfb* self, jint op, struct nfrf* rf,
  253.                  struct JMCException* *exceptionThrown)
  254. {
  255.     cfbImpl *oimpl = cfb2cfbImpl(self);
  256.     XP_ASSERT(oimpl->header.refcount);
  257.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  258.     return (fbobj->GetBaseFont(rf));
  259. }
  260.  
  261. extern "C" JMC_PUBLIC_API(jint)
  262. /*ARGSUSED*/
  263. _cfb_nffbp_RegisterFontDisplayer(struct nffbp* self, jint op,
  264.                                 struct nffp* fp,
  265.                                 struct JMCException* *exceptionThrown)
  266. {
  267.     cfbImpl *oimpl = nffbp2cfbImpl(self);
  268.     XP_ASSERT(oimpl->header.refcount);
  269.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  270.     return (fbobj->RegisterFontDisplayer(fp));
  271. }
  272.  
  273. extern "C" JMC_PUBLIC_API(jint)
  274. /*ARGSUSED*/
  275. _cfb_nffbp_CreateFontDisplayerFromDLM(struct nffbp* self, jint op,
  276.                                      const char* dlm_name,
  277.                                      struct JMCException* *exceptionThrown)
  278. {
  279.     cfbImpl *oimpl = nffbp2cfbImpl(self);
  280.     XP_ASSERT(oimpl->header.refcount);
  281.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  282.     return (fbobj->CreateFontDisplayerFromDLM(dlm_name));
  283. }
  284.  
  285.  
  286. extern "C" JMC_PUBLIC_API(jint)
  287. /*ARGSUSED*/
  288. _cfb_nffbp_ScanForFontDisplayers(struct nffbp* self, jint op,
  289.                                 const char* dlm_dir,
  290.                                 struct JMCException* *exceptionThrown)
  291. {
  292.     cfbImpl *oimpl = nffbp2cfbImpl(self);
  293.     XP_ASSERT(oimpl->header.refcount);
  294.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  295.     return (fbobj->ScanForFontDisplayers(dlm_dir));
  296. }
  297.  
  298.  
  299. extern "C" JMC_PUBLIC_API(void)
  300. /*ARGSUSED*/
  301. _cfb_nffbp_RfDone(struct nffbp* self, jint op, struct nfrf *rf,
  302.                   struct JMCException* *exceptionThrown)
  303. {
  304.     cfbImpl *oimpl = nffbp2cfbImpl(self);
  305.     XP_ASSERT(oimpl->header.refcount);
  306.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  307.     fbobj->RfDone(rf);
  308. }
  309.  
  310.  
  311. extern "C" JMC_PUBLIC_API(struct nffmi*)
  312. /*ARGSUSED*/
  313. _cfb_nffbu_CreateFontMatchInfo(struct nffbu* self, jint op, const char* name,
  314.                                const char* charset, const char* encoding,
  315.                                jint weight, jint pitch, jint style,
  316.                                jint underline, jint strikeOut,
  317.                                jint resX, jint resY,
  318.                                struct JMCException* *exceptionThrown)
  319. {
  320.     cfbImpl *oimpl = nffbu2cfbImpl(self);
  321.     XP_ASSERT(oimpl->header.refcount);
  322.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  323.     return (fbobj->CreateFontMatchInfo(name, charset, encoding, weight,
  324.                                        pitch, style, underline, strikeOut,
  325.                                        resX, resY));
  326. }
  327.  
  328.  
  329. extern "C" JMC_PUBLIC_API(struct nfrc*)
  330. /*ARGSUSED*/
  331. _cfb_nffbu_CreateRenderingContext(struct nffbu* self, jint op,
  332.                                   jint majorType, jint minorType,
  333.                                   void ** args, jsize nargs,
  334.                                   struct JMCException* *exceptionThrown)
  335. {
  336.     cfbImpl *oimpl = nffbu2cfbImpl(self);
  337.     XP_ASSERT(oimpl->header.refcount);
  338.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  339.     return (fbobj->CreateRenderingContext(majorType, minorType, args, nargs));
  340. }
  341.  
  342. extern "C" JMC_PUBLIC_API(struct nfdoer*)
  343. /*ARGSUSED*/
  344. _cfb_nffbu_CreateFontObserver(struct nffbu* self, jint op,
  345.                               nfFontObserverCallback callback,
  346.                               void *client_data,
  347.                               struct JMCException* *exceptionThrown)
  348. {
  349.     cfbImpl *oimpl = nffbu2cfbImpl(self);
  350.     XP_ASSERT(oimpl->header.refcount);
  351.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  352.     return (fbobj->CreateFontObserver(callback, client_data));
  353. }
  354.  
  355.  
  356. extern "C" JMC_PUBLIC_API(void *)
  357. /*ARGSUSED*/
  358. _cfb_nffbu_malloc(struct nffbu* self, jint op, jint size,
  359.                   struct JMCException* *exceptionThrown)
  360. {
  361.     return (WF_ALLOC(size));
  362. }
  363.  
  364. extern "C" JMC_PUBLIC_API(void)
  365. /*ARGSUSED*/
  366. _cfb_nffbu_free(struct nffbu* self, jint op, void *mem,
  367.                 struct JMCException* *exceptionThrown)
  368. {
  369.     if (mem)
  370.       {
  371.         WF_FREE(mem);
  372.       }
  373. }
  374.  
  375. extern "C" JMC_PUBLIC_API(void *)
  376. /*ARGSUSED*/
  377. _cfb_nffbu_realloc(struct nffbu* self, jint op, void *mem, jint size,
  378.                    struct JMCException* *exceptionThrown)
  379. {
  380.     return (WF_REALLOC(mem, size));
  381. }
  382.  
  383. //
  384. // Font Broker Preferences
  385. //
  386.  
  387. extern "C" JMC_PUBLIC_API(jint)
  388. /*ARGSUSED*/
  389. _cfb_nffbu_IsWebfontsEnabled(struct nffbu* self, jint op,
  390.                              struct JMCException* *exceptionThrown)
  391. {
  392.     cfbImpl *oimpl = nffbu2cfbImpl(self);
  393.     XP_ASSERT(oimpl->header.refcount);
  394.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  395.     return (fbobj->IsWebfontsEnabled());
  396. }
  397.  
  398.  
  399. extern "C" JMC_PUBLIC_API(jint)
  400. /*ARGSUSED*/
  401. _cfb_nffbu_EnableWebfonts(struct nffbu* self, jint op,
  402.                           struct JMCException* *exceptionThrown)
  403. {
  404.     cfbImpl *oimpl = nffbu2cfbImpl(self);
  405.     XP_ASSERT(oimpl->header.refcount);
  406.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  407.     return (fbobj->EnableWebfonts());
  408. }
  409.  
  410.  
  411. extern "C" JMC_PUBLIC_API(jint)
  412. /*ARGSUSED*/
  413. _cfb_nffbu_DisableWebfonts(struct nffbu* self, jint op,
  414.                            struct JMCException* *exceptionThrown)
  415. {
  416.     cfbImpl *oimpl = nffbu2cfbImpl(self);
  417.     XP_ASSERT(oimpl->header.refcount);
  418.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  419.     return (fbobj->DisableWebfonts());
  420. }
  421.  
  422. // The following are font preference related queries
  423.  
  424. extern "C" JMC_PUBLIC_API(void *)
  425. /*ARGSUSED*/
  426. _cfb_nffbu_ListFontDisplayers(struct nffbu* self, jint op,
  427.                               struct JMCException* *exceptionThrown)
  428. {
  429.     cfbImpl *oimpl = nffbu2cfbImpl(self);
  430.     XP_ASSERT(oimpl->header.refcount);
  431.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  432.     return ((void *)fbobj->ListFontDisplayers());
  433. }
  434.  
  435. extern "C" JMC_PUBLIC_API(jint)
  436. /*ARGSUSED*/
  437. _cfb_nffbu_IsFontDisplayerEnabled(struct nffbu* self, jint op,
  438.                                   const char *displayer,
  439.                                   struct JMCException* *exceptionThrown)
  440. {
  441.     cfbImpl *oimpl = nffbu2cfbImpl(self);
  442.     XP_ASSERT(oimpl->header.refcount);
  443.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  444.     return (fbobj->IsFontDisplayerEnabled(displayer));
  445. }
  446.  
  447. extern "C" JMC_PUBLIC_API(void *)
  448. /*ARGSUSED*/
  449. _cfb_nffbu_ListFontDisplayersForMimetype(struct nffbu* self, jint op,
  450.                                          const char *mimetype,
  451.                                          struct JMCException* *exceptionThrown)
  452. {
  453.     cfbImpl *oimpl = nffbu2cfbImpl(self);
  454.     XP_ASSERT(oimpl->header.refcount);
  455.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  456.     return ((void *)fbobj->ListFontDisplayersForMimetype(mimetype));
  457. }
  458.  
  459. extern "C" JMC_PUBLIC_API(const char *)
  460. /*ARGSUSED*/
  461. _cfb_nffbu_FontDisplayerForMimetype(struct nffbu* self, jint op,
  462.                                     const char *mimetype,
  463.                                     struct JMCException* *exceptionThrown)
  464. {
  465.     cfbImpl *oimpl = nffbu2cfbImpl(self);
  466.     XP_ASSERT(oimpl->header.refcount);
  467.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  468.     return (fbobj->FontDisplayerForMimetype(mimetype));
  469. }
  470.  
  471. // The following are used to change state
  472.  
  473. extern "C" JMC_PUBLIC_API(jint)
  474. /*ARGSUSED*/
  475. _cfb_nffbu_EnableFontDisplayer(struct nffbu* self, jint op,
  476.                                 const char *displayer,
  477.                                 struct JMCException* *exceptionThrown)
  478. {
  479.     cfbImpl *oimpl = nffbu2cfbImpl(self);
  480.     XP_ASSERT(oimpl->header.refcount);
  481.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  482.     return (fbobj->EnableFontDisplayer(displayer));
  483. }
  484.  
  485.  
  486. extern "C" JMC_PUBLIC_API(jint)
  487. /*ARGSUSED*/
  488. _cfb_nffbu_DisableFontDisplayer(struct nffbu* self, jint op,
  489.                                 const char *displayer,
  490.                                 struct JMCException* *exceptionThrown)
  491. {
  492.     cfbImpl *oimpl = nffbu2cfbImpl(self);
  493.     XP_ASSERT(oimpl->header.refcount);
  494.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  495.     return (fbobj->DisableFontDisplayer(displayer));
  496. }
  497.  
  498.  
  499. extern "C" JMC_PUBLIC_API(jint)
  500. /*ARGSUSED*/
  501. _cfb_nffbu_EnableMimetype(struct nffbu* self, jint op,
  502.                           const char *displayer, const char *mimetype,
  503.                           struct JMCException* *exceptionThrown)
  504. {
  505.     cfbImpl *oimpl = nffbu2cfbImpl(self);
  506.     XP_ASSERT(oimpl->header.refcount);
  507.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  508.     return (fbobj->EnableMimetype(displayer, mimetype));
  509. }
  510.  
  511.  
  512. extern "C" JMC_PUBLIC_API(jint)
  513. /*ARGSUSED*/
  514. _cfb_nffbu_DisableMimetype(struct nffbu* self, jint op,
  515.                            const char *displayer, const char *mimetype,
  516.                            struct JMCException* *exceptionThrown)
  517. {
  518.     cfbImpl *oimpl = nffbu2cfbImpl(self);
  519.     XP_ASSERT(oimpl->header.refcount);
  520.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  521.     return (fbobj->DisableMimetype(displayer, mimetype));
  522. }
  523.  
  524.  
  525. //
  526. // Font Broker Catalog
  527. //
  528.  
  529. extern "C" JMC_PUBLIC_API(jint)
  530. /*ARGSUSED*/
  531. _cfb_nffbu_LoadCatalog(struct nffbu* self, jint op,
  532.                        const char *catalogFilename,
  533.                        struct JMCException* *exceptionThrown)
  534.                        
  535. {
  536.     cfbImpl *oimpl = nffbu2cfbImpl(self);
  537.     XP_ASSERT(oimpl->header.refcount);
  538.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  539.     return (fbobj->LoadCatalog(catalogFilename));
  540. }
  541.  
  542. extern "C" JMC_PUBLIC_API(jint)
  543. /*ARGSUSED*/
  544. _cfb_nffbu_SaveCatalog(struct nffbu* self, jint op,
  545.                        const char *catalogFilename,
  546.                        struct JMCException* *exceptionThrown)
  547.                        
  548. {
  549.     cfbImpl *oimpl = nffbu2cfbImpl(self);
  550.     XP_ASSERT(oimpl->header.refcount);
  551.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  552.     return (fbobj->SaveCatalog(catalogFilename));
  553. }
  554.  
  555. //
  556. // Webfonts & MWContext specific
  557. //
  558.  
  559. #ifndef XP_OS2
  560. extern "C" 
  561. #endif
  562. void
  563. /*ARGSUSED*/
  564. release_webfonts(wfList *list, void *item)
  565. {
  566.     struct nff *f = (struct nff *)item;
  567.     nff_release(f, NULL);
  568. }
  569.  
  570. extern "C" void wf_ObserverCallback(struct nff *ff, void *data)
  571. {
  572.     MWContext *context = (MWContext *) data;
  573.  
  574.     // fprintf(stderr, "fontObserverCallback: State of font is %d.", nff_GetState(f, NULL));
  575.      switch (nff_GetState(ff, NULL))
  576.      {
  577.      case NF_FONT_COMPLETE:
  578.          {
  579.              wfList *webfontsList = (wfList *)context->webfontsList;
  580.              if (!webfontsList)
  581.              {
  582.                  webfontsList = new wfList(release_webfonts);
  583.                  context->webfontsList = webfontsList;
  584.              }
  585.              webfontsList->add(ff);
  586.              context->WebFontDownLoadCount++;
  587.              nff_addRef(ff, NULL);
  588.              break;
  589.          }
  590.      case NF_FONT_ERROR:
  591.      case NF_FONT_INCOMPLETE:        // This should not happen.
  592.      default :
  593.          break;
  594.      }
  595.      return;
  596. }
  597.  
  598. extern "C" JMC_PUBLIC_API(jint)
  599. /*ARGSUSED*/
  600. _cfb_nffbu_LoadWebfont(struct nffbu* self, jint op,
  601.                        MWContext *context, const char *url, jint force,
  602.                        struct JMCException* *exceptionThrown)
  603. {
  604.      cfbImpl *oimpl = nffbu2cfbImpl(self);
  605.     XP_ASSERT(oimpl->header.refcount);
  606.  
  607.     int ret = 0;
  608.     struct nff *f;
  609.     struct nfdoer *observer;
  610.     struct nfrc *rc;
  611.        void *rcbuf[2]; 
  612.     int url_type;
  613.     URL_Struct *url_s = NULL;
  614.     char *fullFilePath = NULL;
  615.     NET_ReloadMethod reloadMethod = (NET_ReloadMethod) force;
  616.     History_entry *he = NULL;
  617.     const char *accessing_url_str = NULL;
  618.     
  619.     if (reloadMethod == NET_RESIZE_RELOAD)
  620.     {
  621.         return (ret);
  622.     }
  623.  
  624.     rcbuf[0] = (void *)NULL;
  625.     rcbuf[1] = 0;
  626.     rc = nffbu_CreateRenderingContext(self, NF_RC_DIRECT, 0, rcbuf, 1, NULL);
  627.  
  628.     url_type = NET_URL_Type(url);
  629.  
  630.     /* Do cache checking only if this isn't NET_SUPER_RELOAD and
  631.      * this isn't a mail/news url.
  632.      */
  633.     if (reloadMethod != NET_SUPER_RELOAD &&
  634.         url_type != MAILBOX_TYPE_URL && url_type != NEWS_TYPE_URL)
  635.     {
  636.         url_s = NET_CreateURLStruct(url, NET_NORMAL_RELOAD); 
  637.         if (url_s) 
  638.         { 
  639.             NET_FindURLInCache(url_s, context); 
  640.         }
  641.         if (url_s && url_s->cache_file && *url_s->cache_file)
  642.         {
  643.             fullFilePath = WH_FileName(url_s->cache_file, xpCache);
  644.         }
  645.  
  646.         // See if this a local file url
  647.         if (!fullFilePath && url_s && url_s->address &&
  648.             // NET_IsLocalFile() says Yes to mailbox: urls too. Yuck.
  649.             !wf_strncasecmp(url_s->address, "file:", 5) &&
  650.             NET_IsLocalFileURL(url_s->address))
  651.         {
  652.             fullFilePath = NET_ParseURL(url_s->address, GET_PATH_PART);
  653.             if (fullFilePath && *fullFilePath)
  654.             {
  655.                 fullFilePath = NET_UnEscape(fullFilePath);
  656.                 char *s = WH_FileName(fullFilePath, xpTemporary);
  657.                 XP_FREE(fullFilePath);
  658.                 fullFilePath = s;
  659.             }
  660.         }
  661.     }
  662.  
  663.     // Find the url that is loading this font
  664.     he = SHIST_GetCurrent(&context->hist);
  665.     if (he)
  666.     {
  667.         accessing_url_str = he->address;
  668.     }
  669.  
  670.     if (fullFilePath && *fullFilePath)
  671.     {
  672.         // The url was cached.
  673.         struct nffbc *fbc =
  674.             (struct nffbc *)nffbu_getInterface(self, &nffbc_ID, NULL);
  675.  
  676.         f =    nffbc_CreateFontFromFile(fbc, rc, url_s->content_type,
  677.                                     fullFilePath, accessing_url_str, NULL);
  678.  
  679.         // Add the font to the list of created webfonts
  680.         if (f)
  681.         {
  682.             wfList *webfontsList = (wfList *)context->webfontsList;
  683.             if (!webfontsList)
  684.             {
  685.                 webfontsList = new wfList(release_webfonts);
  686.                 context->webfontsList = webfontsList;
  687.             }
  688.  
  689.             // Add the font to the list of created webfonts
  690.             // and increment its refcount
  691.             webfontsList->add(f);
  692.             nff_addRef(f, NULL);
  693.         }
  694.     }
  695.     else
  696.     {
  697.         // Create the font observer
  698.         observer = nffbu_CreateFontObserver(self, wf_ObserverCallback, context,
  699.                                             NULL);
  700.         
  701.         if (!observer)
  702.         {
  703.             return (-2);
  704.         }
  705.         
  706.         f =    nffbc_CreateFontFromUrl(
  707.             (struct nffbc *)nffbu_getInterface(self, &nffbc_ID, NULL),
  708.             rc, url, accessing_url_str, 0, observer, context, NULL);
  709.     }
  710.  
  711.     if (url_s)
  712.     {
  713.         NET_FreeURLStruct(url_s); 
  714.     }
  715.     if (fullFilePath)
  716.     {
  717.         XP_FREE(fullFilePath);
  718.     }
  719.  
  720.     if (!f)
  721.     {
  722.         // Coudn't create font
  723.         ret = -1;
  724.     }
  725.     else
  726.     {
  727.         // Release the current 'f' that we are holding
  728.         nff_release(f, NULL);
  729.     }
  730.     return (ret);
  731. }
  732.  
  733. extern "C" JMC_PUBLIC_API(jint)
  734. /*ARGSUSED*/
  735. _cfb_nffbu_ReleaseWebfonts(struct nffbu* self, jint op,
  736.                            MWContext *context,
  737.                            struct JMCException* *exceptionThrown)
  738. {
  739.     wfList *webfontsList = (wfList *) context->webfontsList;
  740.     if (!webfontsList)
  741.     {
  742.         // No webfonts were ever loaded
  743.         return (-1);
  744.     }
  745.  
  746.     delete webfontsList;
  747.     context->webfontsList = NULL;
  748.     context->WebFontDownLoadCount = 0;
  749.     context->MissedFontFace = 0;
  750.     return (0);
  751. }
  752.  
  753. extern "C" JMC_PUBLIC_API(jint)
  754. /*ARGSUSED*/
  755. _cfb_nffbu_WebfontsNeedReload(struct nffbu* self, jint op,
  756.                               MWContext *context,
  757.                               struct JMCException* *exceptionThrown)
  758. {
  759.     if (!context)
  760.     {
  761.         return (-1);
  762.     }
  763.  
  764.     if (context->WebFontDownLoadCount <= 0)
  765.     {
  766.         // No webfonts downloaded by this document
  767.         return (0);
  768.     }
  769.  
  770.     /* A reload should happen iff
  771.      * 1. There were successful downloads of webfonts.
  772.      *        Creating webfonts from cache doesn't count
  773.      * 2. If the current document was a NET_RESIZE_RELOAD
  774.      *
  775.      * Even if FE didn't fail to load a font we have to download
  776.      * because webfonts always override system fonts and we could
  777.      * have a request that was earlier satisfied by a system font be
  778.      * satisfied with a webfont.
  779.      */
  780.     NET_ReloadMethod reloadMethod =  LO_GetReloadMethod(context);
  781.  
  782.     /* Before returning reset context->MissedFontFace */
  783.     context->MissedFontFace = 0; /* Not used for now */
  784.     return (reloadMethod != NET_RESIZE_RELOAD);
  785. }
  786.  
  787.  
  788. extern "C" JMC_PUBLIC_API(jint)
  789. /*ARGSUSED*/
  790. _cfb_nffbu_LookupFailed(struct nffbu* self, jint op,
  791.                         MWContext *context, struct nfrc *rc,
  792.                         struct nffmi *fmi,
  793.                         struct JMCException* *exceptionThrown)
  794. {
  795.     // The simplest implementation is to set a flag and use it
  796.     // when deciding to reload.
  797.     //
  798.     // More complicated schemes where we keep this list of fmi
  799.     // and enable reload only if these fmis will be satisfied by
  800.     // the downloaded fonts.
  801.     if (!context)
  802.     {
  803.         return (-1);
  804.     }
  805.  
  806.     context->MissedFontFace = 1;
  807.     return (0);
  808. }
  809.  
  810.  
  811. extern "C" JMC_PUBLIC_API(jint)
  812. /*ARGSUSED*/
  813. _cfb_nffbu_ToUnicode(struct nffbu* self, jint op,
  814.                      const char* encoding,
  815.                      jbyte* src, jsize src_length,
  816.                      jshort* dest, jsize dest_length,
  817.                      struct JMCException* *exceptionThrown)
  818. {
  819.     jint ret = 0;
  820.     
  821.     if (src_length)
  822.     {
  823.         INTL_Encoding_ID encoding_ID = INTL_CharSetNameToID((char *)encoding);
  824.         ret = INTL_TextToUnicode(encoding_ID,
  825.             (unsigned char *)src, (uint32)src_length,
  826.             (INTL_Unicode *)dest, (uint32)dest_length);
  827.     }
  828.     return (ret);
  829. }
  830.  
  831.  
  832. extern "C" JMC_PUBLIC_API(jint)
  833. /*ARGSUSED*/
  834. _cfb_nffbu_UnicodeLen(struct nffbu* self, jint op,
  835.                       const char* encoding,
  836.                       jbyte* src, jsize src_length,
  837.                       struct JMCException* *exceptionThrown)
  838. {
  839.     jint ret = 0;
  840.     
  841.     if (src_length)
  842.     {
  843.         INTL_Encoding_ID encoding_ID = INTL_CharSetNameToID((char *)encoding);
  844.         ret = INTL_TextToUnicodeLen(encoding_ID,
  845.             (unsigned char *)src, (uint32) src_length);
  846.     }
  847.     return (ret);
  848. }
  849.