home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / libfont / src / wfStream.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  12.1 KB  |  431 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.  * wfStream.cpp (wfStream.cpp)
  20.  *
  21.  * Implements streaming functionality of Fonts
  22.  *
  23.  * dp Suresh <dp@netscape.com>
  24.  */
  25.  
  26.  
  27. #include "nf.h"            // Public header file
  28. #include "libfont.h"    // Private kitchen sink
  29.  
  30. /* Getting intl string ids */
  31. #define WANT_ENUM_STRING_IDS
  32. #include "allxpstr.h"
  33. #undef WANT_ENUM_STRING_IDS
  34.  
  35. #include "Mcfb.h"
  36. #include "fb.h"
  37. #include "Pcfb.h"
  38. #include "wffpPeer.h"
  39. #include "Pcf.h"
  40. #include "f.h"
  41.  
  42. #include "Mnffbc.h"
  43. #include "Mnfdoer.h"
  44. #include "Mnff.h"
  45. #include "Mnfstrm.h"
  46.  
  47. struct stream_data {
  48.     URL_Struct *urls;
  49.     struct nffbc *fbc;
  50.     FontDisplayerPeerObject *fpPeer;
  51.     struct nfstrm *fpStream;
  52.     struct nfdoer *observer;
  53.     struct nff *f;
  54. };
  55.  
  56. static void
  57. wf_ReleaseStreamData(struct stream_data *data)
  58. {
  59.     nffbc_release(data->fbc, NULL);
  60.  
  61.     // Release stream and tell the wffpPeer about it
  62.     data->fpPeer->StreamDone(data->fpStream);
  63.     nfstrm_release(data->fpStream, NULL);
  64.  
  65.     nfdoer_release(data->observer, NULL);
  66.     nff_release(data->f, NULL);
  67.  
  68.     delete data;
  69.     return;
  70. }
  71.  
  72. static void
  73. wf_NotifyObserver(struct stream_data *data)
  74. {
  75.     nfdoer_Update(data->observer, data->f, NULL);
  76. }
  77.  
  78.  
  79. extern "C" int
  80. wfWrite(NET_StreamClass *stream, const unsigned char *str, int32 len)
  81. {
  82.     struct stream_data *data = (struct stream_data *) stream->data_object;    
  83.     return ((int)nfstrm_Write((nfstrm *)data->fpStream, (char *)str, (jint)len, NULL));
  84. }
  85.  
  86. #ifndef XP_OS2
  87. extern "C"
  88. #endif
  89. unsigned int wfWriteReady(NET_StreamClass *stream)
  90. {
  91.     struct stream_data *data = (struct stream_data *) stream->data_object;    
  92.     return ((unsigned int)nfstrm_IsWriteReady((nfstrm *)data->fpStream, NULL));
  93. }
  94.  
  95. #ifndef XP_OS2
  96. extern "C"
  97. #endif
  98. void wfComplete(NET_StreamClass *stream)
  99. {
  100.     struct stream_data *data = (struct stream_data *) stream->data_object;
  101.     void *fh = nfstrm_Complete((nfstrm *)data->fpStream, NULL);
  102.     cfImpl *oimpl = cf2cfImpl(data->f);
  103.     FontObject *fob = (FontObject *)oimpl->object;    
  104.     if (fh)
  105.     {
  106.         // Include this fh in the correct FontObject
  107.         fob->addFontHandle(data->fpPeer, fh);
  108.         WF_TRACEMSG( ("NF: Stream done. Fonthandle created. Setting font state to COMPLETE.") );
  109.         fob->setState(NF_FONT_COMPLETE);
  110.     }
  111.     else
  112.     {
  113.         // This font will never become ok as the fonthandle
  114.         // that we got was 0. We will assume that the data
  115.         // that we streamed didn't make a valid font.
  116.         WF_TRACEMSG( ("NF: Stream done. Fonthandle NOT CREATED. Setting font state to ERROR.") );
  117.         fob->setState(NF_FONT_ERROR);
  118.     }
  119.     
  120.     // Notify the nfdoer on the status of Complete
  121.     wf_NotifyObserver(data);
  122.     wf_ReleaseStreamData(data);
  123.     return;
  124. }
  125.  
  126. #ifndef XP_OS2
  127. extern "C"
  128. #endif
  129. void wfAbort(NET_StreamClass *stream, int status)
  130. {
  131.     struct stream_data *data = (struct stream_data *) stream->data_object;
  132.     nfstrm_Abort((nfstrm *)data->fpStream, (jint) status, NULL);
  133.  
  134.     // Mark the font as in error
  135.     cfImpl *oimpl = cf2cfImpl(data->f);
  136.     FontObject *fob = (FontObject *)oimpl->object;
  137.     fob->setState(NF_FONT_ERROR);    
  138.     
  139.     // Notify nfdoer with status of Abort
  140.     wf_NotifyObserver(data);
  141.     wf_ReleaseStreamData(data);
  142.     return;
  143. }
  144.  
  145.  
  146. extern "C" void
  147. /*ARGSUSED*/
  148. wfUrlExit(URL_Struct *urls, int status, MWContext *cx)
  149. {
  150.     if (status < 0 && urls->error_msg)
  151.     {
  152.         WF_TRACEMSG (("NF: Font downloading unsuccessful : %s.",
  153.             urls->error_msg));
  154.     }
  155.     
  156.     if (status != MK_CHANGING_CONTEXT)
  157.     {
  158.         NET_FreeURLStruct (urls);
  159.     }
  160. }
  161.  
  162.  
  163. //
  164. // Font stream handler.
  165. // This will be called by netlib to when a stream of type FO_FONT
  166. // is available.
  167. //
  168. extern "C" NET_StreamClass*
  169. /*ARGSUSED*/
  170. wfNewStream(FO_Present_Types format_out, void* client_data,
  171.             URL_Struct* urls, MWContext* cx)
  172. {
  173.     struct nffbc *fbc = (struct nffbc *) client_data;
  174.     struct wf_new_stream_data *inData =
  175.         (struct wf_new_stream_data *) urls->fe_data;
  176.     cfbImpl *oimpl = cfb2cfbImpl(fbc);
  177.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  178.     
  179.     const char *mimetype = fbobj->GetMimetype(urls->content_type, urls->address);
  180.  
  181.     if (!mimetype || !*mimetype)
  182.     {
  183.         return (NULL);
  184.     }
  185.  
  186.     WF_TRACEMSG(("NF: Looking for mimetype: %s.", mimetype));
  187.  
  188.     NET_StreamClass *netStream = NULL;
  189.     
  190.     // Find which font Displayer implements the mimetype
  191.     struct wfListElement *tmp = fbobj->fpPeers.head;
  192.     for(; tmp; tmp = tmp->next)
  193.     {
  194.         FontDisplayerPeerObject *fpp = (FontDisplayerPeerObject *) tmp->item;
  195.         if (fpp->isMimetypeEnabled(mimetype) > 0)
  196.         {
  197.             // Get the font Displayer stream handle
  198.             WF_TRACEMSG(("NF: fppeer %s supports mimetype %s.", fpp->name(),
  199.                 mimetype));
  200.             struct nfstrm *fpStream = fpp->CreateFontStreamHandler(inData->rc, inData->url_of_page);
  201.             if (!fpStream)
  202.             {
  203.                 // Error. Cannot create stream from Font Displayer.
  204.                 // We will cycle through other Displayers to see if we can
  205.                 // find any other font Displayer who can service us.
  206.                 WF_TRACEMSG(("NF: fppeer %s cannot create fpStream. "
  207.                     "Continuing...", fpp->name()));
  208.                 fpp = NULL;
  209.                 continue;
  210.             }
  211.  
  212.             // Create a libnet stream
  213.             netStream = (NET_StreamClass *) WF_ALLOC(sizeof(NET_StreamClass));
  214.             if (!netStream)
  215.             {
  216.                 // Error. No point continuing.
  217.                 WF_TRACEMSG(("NF: Error: Cannot create net stream. No memory."));
  218.                 nfstrm_release(fpStream, NULL);
  219.                 break;
  220.             }
  221.  
  222.             // Create our stream data object
  223.             struct stream_data *wfStream = new stream_data;
  224.             if (!wfStream)
  225.             {
  226.                 WF_TRACEMSG(("NF: Error: No memory to allocate stream_data."));
  227.                 nfstrm_release(fpStream, NULL);
  228.                 delete netStream;
  229.                 netStream = NULL;
  230.                 break;
  231.             }
  232.  
  233.             // Fill our stream data
  234.             wfStream->urls = urls;
  235.  
  236.             nffbc_addRef(fbc, NULL);
  237.             wfStream->fbc = fbc;
  238.  
  239.             // fpstream was created here. So no need to addref it as
  240.             // the creation process would already have incremented the
  241.             // refcount.
  242.             //nfstrm_addRef(fpStream, NULL);
  243.             wfStream->fpStream = fpStream;
  244.  
  245.             // Tell the wffpPeer about this new stream
  246.             fpp->StreamCreated(fpStream);
  247.  
  248.             nfdoer_addRef(inData->observer, NULL);
  249.             wfStream->observer = inData->observer;
  250.  
  251.             nff_addRef(inData->f, NULL);
  252.             wfStream->f = inData->f;
  253.  
  254.             wfStream->fpPeer = fpp;
  255.  
  256.             // Fill libnet stream
  257.             netStream->name = "Font Broker";
  258.             netStream->abort = wfAbort;
  259.             netStream->complete = wfComplete;
  260.             netStream->put_block = (MKStreamWriteFunc)wfWrite;
  261.             netStream->is_write_ready = wfWriteReady;
  262.             netStream->data_object = (void *)wfStream;
  263.             netStream->window_id = cx;
  264.             break;
  265.         }
  266.     }
  267.  
  268.     // Cleanup our wf_new_stream_data that was passed in
  269.     nfdoer_release(inData->observer, NULL);
  270.     nff_release(inData->f, NULL);
  271.     nfrc_release(inData->rc, NULL);
  272.     if (inData->url_of_page)
  273.     {
  274.         delete inData->url_of_page;
  275.         inData->url_of_page = NULL;
  276.     }
  277.     delete inData;
  278.  
  279.     return (netStream);
  280. }
  281.  
  282.  
  283. //
  284. // Net registering converters
  285. //
  286. extern "C" void
  287. NF_RegisterConverters(void)
  288. {
  289.     // Register our converters with libnet
  290.     NET_RegisterContentTypeConverter("*", FO_CACHE_AND_FONT, NULL,
  291.         NET_CacheConverter);
  292.  
  293.     NET_RegisterContentTypeConverter("*", FO_FONT, WF_fbc, wfNewStream);
  294.  
  295.     // For each mimetype that we support, associate the mimetype with the
  296.     // extension
  297.     cfbImpl *oimpl = nffbc2cfbImpl(WF_fbc);
  298.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  299.     struct wfListElement *tmp = fbobj->fpPeers.head;
  300.     for(; tmp; tmp = tmp->next)
  301.     {
  302.         FontDisplayerPeerObject *fpp = (FontDisplayerPeerObject *) tmp->item;
  303.         fpp->registerConverters();
  304.     }
  305. }
  306.  
  307.  
  308. //
  309. // About fonts
  310. //
  311. #ifndef NO_HTML_DIALOGS_CHANGE
  312. PR_STATIC_CALLBACK(PRBool)
  313. /*ARGSUSED*/
  314. wf_AboutFontsDialogDone(XPDialogState* state, char** av, int ac,
  315.                         unsigned int button)
  316. {
  317.     cfbImpl *oimpl = nffbc2cfbImpl(WF_fbc);
  318.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  319.  
  320.     if (button != XP_DIALOG_OK_BUTTON) return PR_FALSE;
  321.  
  322.     for (int i=0; i+1<ac; i+=2)
  323.     {
  324.         if (!strcmp(av[i], "handle")) continue;
  325.         if (!strcmp(av[i], "button")) continue;
  326.         fbobj->EnableMimetype(av[i+1], av[i]);
  327.     }
  328.  
  329.     return PR_FALSE;
  330. }
  331. #endif /* NO_HTML_DIALOGS_CHANGE */
  332.  
  333. static char peopleMsg[] =
  334.     "\123\175\206\205\213\067\212\200\221\174\124\112\125\123\172\174\205\213"
  335.     "\174\211\125\123\171\211\125\123\171\125\123\214\125\145\174\213\212\172"
  336.     "\170\207\174\067\132\206\204\204\214\205\200\172\170\213\200\206\205\212"
  337.     "\123\106\171\125\123\106\214\125\123\171\211\125\123\171\125\152\214\211"
  338.     "\174\212\177\067\133\214\173\173\200\123\106\171\125\067\104\067\130\211"
  339.     "\172\177\200\213\174\172\213\067\123\171\211\125\123\171\125\133\206\205"
  340.     "\205\170\067\132\206\205\215\174\211\212\174\123\106\171\125\067\104\067"
  341.     "\154\205\200\217\123\171\211\125\123\171\125\130\211\213\177\214\211\067"
  342.     "\143\200\214\123\106\171\125\067\104\067\156\200\205\173\206\216\212\067"
  343.     "\123\171\211\125\123\171\125\134\211\200\172\067\144\170\173\174\211\123"
  344.     "\106\171\125\067\104\067\144\170\172\123\171\211\125\123\171\125\134\211"
  345.     "\200\172\067\131\220\214\205\205\123\106\171\125\067\104\067\144\170\211"
  346.     "\202\174\213\200\205\176\123\171\211\125\123\171\211\125\123\171\125"
  347.     "\123\214\125\131\200\213\212\213\211\174\170\204\067\140\205\172\105\123"
  348.     "\106\171\125\123\106\214\125\123\171\211\125\123\171\125\144\170\211\202"
  349.     "\067\136\206\203\173\216\170\213\174\211\123\106\171\125\067\104\067\135"
  350.     "\206\205\213\212\067\176\214\211\214\123\171\211\125\123\171\211\125\206"
  351.     "\177\105\105\105\170\205\173\123\171\211\125\123\171\125\142\174\174\211"
  352.     "\213\177\170\205\170\123\106\171\125\067\104\067\132\177\174\174\211\067"
  353.     "\203\174\170\173\174\211\123\106\172\174\205\213\174\211\125\123\106\175"
  354.     "\206\205\213\125"
  355. ;
  356.  
  357. extern "C" char *
  358. /*ARGSUSED*/
  359. NF_AboutFonts(MWContext *context, const char *which)
  360. {
  361. #ifndef NO_HTML_DIALOGS_CHANGE
  362.     cfbImpl *oimpl = nffbc2cfbImpl(WF_fbc);
  363.     FontBrokerObject *fbobj = (FontBrokerObject *)oimpl->object;
  364.     struct wfListElement *tmp = NULL;
  365.  
  366.     char *aboutData = NULL;
  367.     int aboutDataLen = 0;
  368.     int aboutDataMaxLen = 0;
  369. #define WF_ACCUMULATE(str) wf_addToString(&aboutData, &aboutDataLen, &aboutDataMaxLen, str);
  370.  
  371.     if (which && !wf_strncasecmp(which, "fonts?", 6)
  372.         && !wf_strncasecmp(which+6, "people", 6))
  373.     {
  374.         char *p;
  375.         for (p=peopleMsg; *p; p++) *p -= 23;
  376.         WF_ACCUMULATE(peopleMsg);
  377.         for (p=peopleMsg; *p; p++) *p += 23;
  378.         goto display;
  379.     }
  380.     
  381.     WF_ACCUMULATE(XP_GetString(WF_MSG_ABOUT_BEGIN_1));
  382.     WF_ACCUMULATE(XP_GetString(WF_MSG_ABOUT_BEGIN_2));
  383.     WF_ACCUMULATE(XP_GetString(WF_MSG_ABOUT_BEGIN_3));
  384.  
  385.     // List all the displayers and their mimetypes
  386.     tmp = fbobj->fpPeers.head;
  387.     for(; tmp; tmp = tmp->next)
  388.     {
  389.         FontDisplayerPeerObject *fpp = (FontDisplayerPeerObject *) tmp->item;
  390.         char *buf = fpp->aboutData();
  391.         if (buf)
  392.         {
  393.             WF_ACCUMULATE(buf);
  394.             WF_FREE(buf);
  395.         }
  396.     }
  397.  
  398. display:
  399.     // do html dialog
  400.     static XPDialogInfo dialogInfo = {
  401.         XP_DIALOG_OK_BUTTON | XP_DIALOG_CANCEL_BUTTON,
  402.             wf_AboutFontsDialogDone,
  403.             600,
  404.             440
  405.     };
  406.     XPDialogStrings* strings;
  407.     
  408.     strings = XP_GetDialogStrings(XP_EMPTY_STRINGS);
  409.     if (!strings) {
  410.         if (aboutData) WF_FREE(aboutData);
  411.         return (NULL);
  412.     }
  413.     if (aboutData)
  414.     {
  415.         XP_CopyDialogString(strings, 0, aboutData);
  416.         WF_FREE(aboutData);
  417.         aboutData = NULL;
  418.     }
  419.     else
  420.     {
  421.         XP_CopyDialogString(strings, 0,
  422.             XP_GetString(WF_MSG_ABOUT_NO_DISPLAYER));
  423.     }
  424.     XP_MakeHTMLDialog(context, &dialogInfo, WF_MSG_ABOUT_TITLE,
  425.         strings, NULL, PR_FALSE); 
  426.     
  427. #undef WF_ACCUMULATE
  428. #endif /* NO_HTML_DIALOGS_CHANGE */
  429.     return (NULL);    
  430. }
  431.