home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / libfont / src / wffpCat.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  10.3 KB  |  551 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.  * wffpCat.cpp (FontDisplayerCatalogObject.cpp)
  20.  *
  21.  * This maintains the displayer catalog.
  22.  *
  23.  * dp Suresh <dp@netscape.com>
  24.  */
  25.  
  26.  
  27. #include "wffpCat.h"
  28.  
  29. #include "Pcfmi.h"
  30. #include "fmi.h"
  31.  
  32. static void free_catalog_store(wfList *object, void *item);
  33.  
  34. #define WF_FMI_CHUNK_SIZE 16
  35.  
  36. FontDisplayerCatalogObject::
  37. FontDisplayerCatalogObject(const char *reconstructString)
  38. : wfList(free_catalog_store)
  39. {
  40.     if (reconstructString && *reconstructString)
  41.     {
  42.         // reconstruct(reconstructString);
  43.     }
  44. }
  45.  
  46. FontDisplayerCatalogObject::
  47. ~FontDisplayerCatalogObject(void)
  48. {
  49.     finalize();
  50. }
  51.  
  52. int FontDisplayerCatalogObject::
  53. finalize()
  54. {
  55.     wfList::removeAll();
  56.     return (0);
  57. }
  58.  
  59.  
  60. void
  61. /*ARGSUSED*/
  62. free_catalog_store(wfList *object, void *item)
  63. {
  64.     struct catalog_store *ele = (struct catalog_store *) item;
  65.     if (ele->fmiCount > 0)
  66.     {
  67.         for (int i = 0; i < ele->fmiCount; i++)
  68.         {
  69.             nffmi_release(ele->fmis[i], NULL);
  70.         }
  71.         delete ele->fmis;
  72.         ele->fmis = NULL;
  73.         ele->fmiCount = 0;
  74.         ele->maxFmiCount = 0;
  75.     }
  76. }
  77.  
  78. int FontDisplayerCatalogObject::
  79. update(struct nfrc *rc, struct nffmi **fmis)
  80. {
  81.     struct wfListElement *tmp = head;
  82.     while (tmp)
  83.     {
  84.         struct catalog_store *ele = (struct catalog_store *) tmp->item;
  85.         if (nfrc_IsEquivalent(rc, ele->rcMajorType, ele->rcMinorType, NULL))
  86.         {
  87.             copyFmis(ele, fmis);
  88.             break;
  89.         }
  90.         tmp = tmp->next;
  91.     }
  92.     if (!tmp)
  93.     {
  94.         // Add these into the list
  95.         struct catalog_store *newele = (struct catalog_store *)
  96.             new catalog_store;
  97.         newele->rcMajorType = nfrc_GetMajorType(rc, NULL);
  98.         newele->rcMinorType = nfrc_GetMinorType(rc, NULL);
  99.         newele->fmis = NULL;
  100.         newele->fmiCount = newele->maxFmiCount = 0;
  101.         copyFmis(newele, fmis);
  102.         add(newele);
  103.     }
  104.     return (0);
  105. }
  106.  
  107.  
  108. int FontDisplayerCatalogObject::
  109. addFmi(jint rcMajorType, jint rcMinorType, struct nffmi *fmi)
  110. {
  111.     struct wfListElement *tmp = head;
  112.     while (tmp)
  113.     {
  114.         struct catalog_store *ele = (struct catalog_store *) tmp->item;
  115.         if ((ele->rcMajorType == rcMajorType) &&
  116.             (ele->rcMinorType == rcMinorType))
  117.         {
  118.             addFmi(ele, fmi);
  119.             break;
  120.         }
  121.         tmp = tmp->next;
  122.     }
  123.  
  124.     if (!tmp)
  125.     {
  126.         // Add this {rc, fmi} as a new element
  127.         struct catalog_store *newele = (struct catalog_store *)
  128.             new catalog_store;
  129.         newele->rcMajorType = rcMajorType;
  130.         newele->rcMinorType = rcMinorType;
  131.         newele->fmis = NULL;
  132.         newele->fmiCount = newele->maxFmiCount = 0;
  133.         addFmi(newele, fmi);
  134.         add(newele);
  135.     }
  136.     return (0);
  137. }
  138.  
  139.  
  140. int FontDisplayerCatalogObject::
  141. isInitialized(struct nfrc *rc)
  142. {
  143.     int ret = 0;
  144.     struct wfListElement *tmp = head;
  145.     while (tmp)
  146.     {
  147.         struct catalog_store *ele = (struct catalog_store *) tmp->item;
  148.         if (nfrc_IsEquivalent(rc, ele->rcMajorType, ele->rcMinorType, NULL))
  149.         {
  150.             // yes. Initialized.
  151.             ret = 1;
  152.             break;
  153.         }
  154.         tmp = tmp->next;
  155.     }
  156.     return (ret);
  157. }
  158.  
  159.  
  160. int FontDisplayerCatalogObject::
  161. supportsFmi(struct nfrc *rc, struct nffmi *fmi)
  162. {
  163.     int supports = 0;
  164.     struct wfListElement *tmp = head;
  165.     struct catalog_store *ele = NULL;
  166.  
  167.     while (tmp)
  168.     {
  169.         ele = (struct catalog_store *) tmp->item;
  170.         if (nfrc_IsEquivalent(rc, ele->rcMajorType, ele->rcMinorType, NULL))
  171.         {
  172.             break;
  173.         }
  174.         tmp = tmp->next;
  175.     }
  176.  
  177.     if (!tmp)
  178.     {
  179.         // Catalog was never intialized for this rc
  180.         supports = -1;
  181.     }
  182.     else if (ele->fmiCount > 0)
  183.     {
  184.         // Check if ele supports the fmi
  185.         for (int i = 0; i < ele->fmiCount; i++)
  186.         {
  187.             if (nffmi_equals(fmi, ele->fmis[i], NULL))
  188.             {
  189.                 supports = 1;
  190.                 break;
  191.             }
  192.         }
  193.     }
  194.  
  195.     return (supports);
  196. }
  197.  
  198. int FontDisplayerCatalogObject::
  199. describe(FontCatalogFile &fc)
  200. {
  201.     struct wfListElement *tmp = head;
  202.     while (tmp)
  203.     {
  204.         struct catalog_store *ele = (struct catalog_store *) tmp->item;
  205.         fc.output("{");
  206.         fc.indentIn();
  207.  
  208.         fc.output("MajorType", (int)ele->rcMajorType);
  209.         fc.output("MinorType", (int)ele->rcMinorType);
  210.         int i = 0;
  211.         for (i = 0; i < ele->fmiCount; i++)
  212.         {
  213.             fc.output("font", nffmi_toString(ele->fmis[i], NULL));
  214.         }
  215.         
  216.         fc.indentOut();
  217.         fc.output("} // End of an element");
  218.         tmp = tmp->next;
  219.     }
  220.     return (0);
  221. }
  222.  
  223. int FontDisplayerCatalogObject::
  224. reconstruct(FontCatalogFile &fc)
  225. {
  226.     char buf[1024];
  227.     int buflen;
  228.     int over = 0;
  229.     char *variable, *value;
  230.     int inElement = 0;
  231.     char *ret;
  232.     jint rcMajorType = 0, rcMinorType = 0;
  233.     int fmiCount =0;
  234.  
  235.     finalize();
  236.     
  237.     while (!over)
  238.     {
  239.         ret = fc.readline(buf, sizeof(buf));
  240.         if (!ret)
  241.         {
  242.             over = 1;
  243.             continue;
  244.         }
  245.         buflen = strlen(buf);
  246.         if (buf[buflen-1] == '\n')
  247.         {
  248.             buf[buflen-1] = '\0';
  249.             buflen--;
  250.         }
  251.         
  252.         wf_scanVariableAndValue(buf, buflen, variable, value);
  253.         
  254.         if (inElement && !wf_strcasecmp(variable, "majortype"))
  255.         {
  256.             rcMajorType = atoi(value);
  257.         }
  258.         else if (inElement && !wf_strcasecmp(variable, "minortype"))
  259.         {
  260.             rcMinorType = atoi(value);
  261.         }
  262.         else if (inElement && !wf_strcasecmp(variable, "font"))
  263.         {
  264.             // Make an fmi out of the buffer
  265.             struct nffmi *fmi = nffbu_CreateFontMatchInfo( WF_fbu, NULL, NULL,
  266.                 NULL, 0, 0, 0, 0, 0, 0, 0, NULL);
  267.             cfmiImpl *oimpl = cfmi2cfmiImpl(fmi);
  268.             FontMatchInfoObject *fmiobj = (FontMatchInfoObject *)oimpl->object;
  269.             fmiobj->reconstruct(buf);
  270.             
  271.             // Add the fmi to this element
  272.             addFmi(rcMajorType, rcMinorType, fmi);
  273.             fmiCount++;
  274.         }
  275.         else if (!strncmp(variable, "{", 1))
  276.         {
  277.             // Begin of an element
  278.             inElement = 1;
  279.             fmiCount = 0;
  280.         }
  281.         else if (!strncmp(variable, "}", 1))
  282.         {
  283.             if (inElement)
  284.             {
  285.                 inElement = 0;
  286.                 // Take care of no fmis.
  287.                 if (fmiCount == 0)
  288.                 {
  289.                     addFmi(rcMajorType, rcMinorType, NULL);
  290.                 }
  291.             }
  292.             else
  293.             {
  294.                 over = 1;
  295.                 continue;
  296.             }
  297.         }
  298.     }
  299.     return (0);
  300. }
  301.  
  302. //
  303. // Private methods
  304. //
  305.  
  306. int FontDisplayerCatalogObject::
  307. copyFmis(struct catalog_store *ele, struct nffmi **fmis)
  308. {    
  309.     if (ele->fmiCount > 0)
  310.     {
  311.         for (int i = 0; i < ele->fmiCount; i++)
  312.         {
  313.             nffmi_release(ele->fmis[i], NULL);
  314.         }
  315.         delete ele->fmis;
  316.         ele->fmis = NULL;
  317.         ele->fmiCount = 0;
  318.         ele->maxFmiCount = 0;
  319.     }
  320.  
  321.     if (!fmis)
  322.     {
  323.         return (-1);
  324.     }
  325.  
  326.     int i = 0;
  327.     while (fmis[i]) i++;
  328.     if (i == 0)
  329.     {
  330.         // No fmis to be copied.
  331.         return (0);
  332.     }
  333.     ele->fmis = (struct nffmi **) WF_ALLOC(sizeof(struct nffmi *) * i);
  334.     if (!ele->fmis)
  335.     {
  336.         // No memory.
  337.         return (-2);
  338.     }
  339.     ele->fmiCount = i;
  340.     ele->maxFmiCount = i;
  341.     while (--i >= 0)
  342.     {
  343.         ele->fmis[i] = fmis[i];
  344.         nffmi_addRef(ele->fmis[i], NULL);
  345.     }
  346.  
  347.     return (0);
  348. }
  349.  
  350. int FontDisplayerCatalogObject::
  351. addFmi(struct catalog_store *ele, struct nffmi *fmi)
  352. {
  353.     if (!fmi)
  354.     {
  355.         return (-1);
  356.     }
  357.  
  358.     if (ele->maxFmiCount <= ele->fmiCount)
  359.     {
  360.         ele->fmis = (struct nffmi **)
  361.             WF_REALLOC(ele->fmis, sizeof(struct nffmi *) * (ele->fmiCount + WF_FMI_CHUNK_SIZE));
  362.         if (!ele->fmis)
  363.         {
  364.             return (-1);
  365.         }
  366.         ele->maxFmiCount = ele->fmiCount + WF_FMI_CHUNK_SIZE;
  367.     }
  368.     ele->fmis[ele->fmiCount++] = fmi;
  369.     nffmi_addRef(fmi, NULL);
  370.     return (0);
  371. }
  372.  
  373. // ==========================================================================
  374.  
  375. FontCatalogFile::FontCatalogFile(const char *name, int write)
  376. : fp(NULL), indentLevel(0), writing(write)
  377. {
  378.  
  379.     if (writing)
  380.     {
  381.         fp = fopen(name, "w");
  382.         
  383.         // Write catalog file header information
  384.         // NO I18N required
  385.         output(
  386.             "#\n"
  387.             "# Netscape Fonts Catalog File\n"
  388.             "#\n"
  389.             "# Architect: Suresh Duddi <dp@netscape.com>\n"
  390.             "#\n"
  391.             "# This file stores all data about font displayers and the kind of fonts\n"
  392.             "# they server. This will be used to optimize loading of font displayers\n"
  393.             "#\n"
  394.             "# #####################################################################\n"
  395.             "# THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT THIS FILE.\n"
  396.             "# #####################################################################\n"
  397.             "# Copyright Netscape Communications Corp (C) 1996, 1997"
  398.             );
  399.         
  400.         output("Version", WF_CATALOG_FILE_VERSION);
  401.         // PR_ExplodeTime(&pr_time, PR_Now());
  402.         // PR_FormatTime(buf, sizeof(buf), "", pr_time);
  403.         // output("Created", buf);
  404.     }
  405.     else
  406.     {
  407.         fp = fopen(name, "r");
  408.     }
  409. }
  410.  
  411. FontCatalogFile::~FontCatalogFile(void)
  412. {
  413.     if (fp)
  414.     {
  415.         fclose(fp);
  416.         fp = NULL;
  417.     }
  418. }
  419.  
  420. char *
  421. FontCatalogFile::readline(char *buf, int buflen)
  422. {
  423.     if (!fp || writing)
  424.     {
  425.         return (NULL);
  426.     }
  427.  
  428.     return (fgets(buf, buflen, fp));
  429. }
  430.  
  431.  
  432. int
  433. FontCatalogFile::isEof(void)
  434. {
  435.     int ret = 0;
  436.  
  437.     if (!fp || writing)
  438.     {
  439.         ret = -1;
  440.     }
  441.     else
  442.     {
  443.         ret = feof(fp);
  444.     }
  445.  
  446.     return (ret);
  447. }
  448.  
  449.  
  450. int
  451. FontCatalogFile::status(void)
  452. {
  453.     int ret = 0;
  454.  
  455.     if (fp == NULL)
  456.     {
  457.         ret = -1;
  458.     }
  459.  
  460.     return (ret);
  461. }
  462.  
  463. int
  464. FontCatalogFile::output(const char *name)
  465. {
  466.     if (!fp || !writing)
  467.     {
  468.         return (-1);
  469.     }
  470.  
  471.     if (!name)
  472.     {
  473.         return (-2);
  474.     }
  475.  
  476.     for (int i=0; i < indentLevel; i++)
  477.     {
  478.         fprintf(fp, "\t");
  479.     }
  480.     fprintf(fp, "%s\n", name);
  481.     return (0);
  482. }
  483.  
  484.  
  485. int
  486. FontCatalogFile::output(const char *name, int value)
  487. {
  488.     if (!fp || !writing)
  489.     {
  490.         return (-1);
  491.     }
  492.  
  493.     if (!name)
  494.     {
  495.         return (-2);
  496.     }
  497.  
  498.     for (int i=0; i < indentLevel; i++)
  499.     {
  500.         fprintf(fp, "\t");
  501.     }
  502.     fprintf(fp, "%s = %d\n", name, value);
  503.     return (0);
  504. }
  505.  
  506.  
  507. int
  508. FontCatalogFile::output(const char *name, const char *str)
  509. {
  510.     if (!fp || !writing)
  511.     {
  512.         return (-1);
  513.     }
  514.  
  515.     if (!name)
  516.     {
  517.         return (-2);
  518.     }
  519.     for (int i=0; i < indentLevel; i++)
  520.     {
  521.         fprintf(fp, "\t");
  522.     }
  523.     if (str)
  524.     {
  525.         fprintf(fp, "%s = %s\n", name, str);
  526.     }
  527.     else
  528.     {
  529.             fprintf(fp, "%s =\n", name);
  530.     }
  531.     return (0);
  532. }
  533.  
  534.  
  535. int
  536. FontCatalogFile::indentIn(void)
  537. {
  538.     indentLevel++;
  539.     return (indentLevel);
  540. }
  541.  
  542. int
  543. FontCatalogFile::indentOut(void)
  544. {
  545.     if (indentLevel)
  546.     {
  547.         indentLevel--;
  548.     }
  549.     return (indentLevel);
  550. }
  551.