Back to index

lightning-sunbird  0.9+nobinonly
Classes | Functions | Variables
nsMathMLOperators.cpp File Reference
#include "nsCOMPtr.h"
#include "nsString.h"
#include "nsHashtable.h"
#include "nsVoidArray.h"
#include "nsIComponentManager.h"
#include "nsIPersistentProperties2.h"
#include "nsNetUtil.h"
#include "nsCRT.h"
#include "nsMathMLOperators.h"

Go to the source code of this file.

Classes

struct  OperatorData

Functions

void SetProperty (OperatorData *aOperatorData, nsString aName, nsString aValue)
PRBool SetOperator (OperatorData *aOperatorData, nsOperatorFlags aForm, const nsCString &aOperator, nsString &aAttributes)
nsresult InitOperators (void)
nsresult InitGlobals ()

Variables

static OperatorDatagOperatorFound [4]
static PRInt32 gTableRefCount = 0
static PRInt32 gOperatorCount = 0
static OperatorDatagOperatorArray = nsnull
static nsHashtable * gOperatorTable = nsnull
static nsVoidArraygStretchyOperatorArray = nsnull
static nsStringArray * gInvariantCharArray = nsnull
static PRBool gInitialized = PR_FALSE
static const PRUnichar kNullCh = PRUnichar('\0')
static const PRUnichar kDashCh = PRUnichar('#')
static const PRUnichar kEqualCh = PRUnichar('=')
static const PRUnichar kColonCh = PRUnichar(':')
static const char *const kMathVariant_name []

Function Documentation

Definition at line 373 of file nsMathMLOperators.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 293 of file nsMathMLOperators.cpp.

{
  // Load the property file containing the Operator Dictionary
  nsresult rv;
  nsCOMPtr<nsIPersistentProperties> mathfontProp;
  rv = NS_LoadPersistentPropertiesFromURISpec(getter_AddRefs(mathfontProp),
       NS_LITERAL_CSTRING("resource://gre/res/fonts/mathfont.properties"));
  if NS_FAILED(rv) return rv;

  // Get the list of invariant chars
  for (PRInt32 i = 0; i < nsMathMLOperators::eMATHVARIANT_COUNT; ++i) {
    nsCAutoString key(NS_LITERAL_CSTRING("mathvariant."));
    key.Append(kMathVariant_name[i]);
    nsAutoString value;
    mathfontProp->GetStringProperty(key, value);
    gInvariantCharArray->AppendString(value); // i.e., gInvariantCharArray[i] holds this list
  }

  // Parse the Operator Dictionary in two passes.
  // The first pass is to count the number of operators; the second pass is to
  // allocate the necessary space for them and to add them in the hash table.
  for (PRInt32 pass = 1; pass <= 2; pass++) {
    OperatorData dummyData;
    OperatorData* operatorData = &dummyData;
    nsCOMPtr<nsISimpleEnumerator> iterator;
    if (NS_SUCCEEDED(mathfontProp->Enumerate(getter_AddRefs(iterator)))) {
      PRBool more;
      PRInt32 index = 0;
      nsCAutoString name;
      nsAutoString attributes;
      while ((NS_SUCCEEDED(iterator->HasMoreElements(&more))) && more) {
        nsCOMPtr<nsIPropertyElement> element;
        if (NS_SUCCEEDED(iterator->GetNext(getter_AddRefs(element)))) {
          if (NS_SUCCEEDED(element->GetKey(name)) &&
              NS_SUCCEEDED(element->GetValue(attributes))) {
            // expected key: operator.\uNNNN.{infix,postfix,prefix}
            if ((21 <= name.Length()) && (0 == name.Find("operator.\\u"))) {
              name.Cut(0, 9); // 9 is the length of "operator.";
              PRInt32 len = name.Length();
              nsOperatorFlags form = 0;
              if (kNotFound != name.RFind(".infix")) {
                form = NS_MATHML_OPERATOR_FORM_INFIX;
                len -= 6;  // 6 is the length of ".infix";
              }
              else if (kNotFound != name.RFind(".postfix")) {
                form = NS_MATHML_OPERATOR_FORM_POSTFIX;
                len -= 8; // 8 is the length of ".postfix";
              }
              else if (kNotFound != name.RFind(".prefix")) {
                form = NS_MATHML_OPERATOR_FORM_PREFIX;
                len -= 7; // 7 is the length of ".prefix";
              }
              else continue; // input is not applicable
              name.SetLength(len);
              if (2 == pass) { // allocate space and start the storage
                if (!gOperatorArray) {
                  if (0 == gOperatorCount) return NS_ERROR_UNEXPECTED;
                  gOperatorArray = new OperatorData[gOperatorCount];
                  if (!gOperatorArray) return NS_ERROR_OUT_OF_MEMORY;
                }
                operatorData = &gOperatorArray[index];
              }
              else {
                form = 0; // to quickly return from SetOperator() at pass 1
              }
              // See if the operator should be retained
              if (SetOperator(operatorData, form, name, attributes)) {
                index++;
                if (1 == pass) gOperatorCount = index;
              }
            }
          }
        }
      }
    }
  }
  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PRBool SetOperator ( OperatorData aOperatorData,
nsOperatorFlags  aForm,
const nsCString aOperator,
nsString aAttributes 
)

Definition at line 185 of file nsMathMLOperators.cpp.

{
  // aOperator is in the expanded format \uNNNN\uNNNN ...
  // First compress these Unicode points to the internal nsString format
  PRInt32 i = 0;
  nsAutoString name, value;
  PRInt32 len = aOperator.Length();
  PRUnichar c = aOperator[i++];
  PRUint32 state  = 0;
  PRUnichar uchar = 0;
  while (i <= len) {
    if (0 == state) {
      if (c != '\\')
        return PR_FALSE;
      if (i < len)
        c = aOperator[i];
      i++;
      if (('u' != c) && ('U' != c))
        return PR_FALSE;
      if (i < len)
        c = aOperator[i];
      i++;
      state++;
    }
    else {
      if (('0' <= c) && (c <= '9'))
         uchar = (uchar << 4) | (c - '0');
      else if (('a' <= c) && (c <= 'f'))
         uchar = (uchar << 4) | (c - 'a' + 0x0a);
      else if (('A' <= c) && (c <= 'F'))
         uchar = (uchar << 4) | (c - 'A' + 0x0a);
      else return PR_FALSE;
      if (i < len)
        c = aOperator[i];
      i++;
      state++;
      if (5 == state) {
        value.Append(uchar);
        uchar = 0;
        state = 0;
      }
    }
  }
  if (0 != state) return PR_FALSE;

  // Quick return when the caller doesn't care about the attributes and just wants
  // to know if this is a valid operator (this is the case at the first pass of the
  // parsing of the dictionary in InitOperators())
  if (!aForm) return PR_TRUE;

  // Add operator to hash table (symmetric="true" by default for all operators)
  aOperatorData->mFlags |= aForm | NS_MATHML_OPERATOR_SYMMETRIC;
  aOperatorData->mStr.Assign(value);
  value.AppendInt(aForm, 10);
  nsStringKey key(value);
  gOperatorTable->Put(&key, aOperatorData);

#ifdef NS_DEBUG
  NS_LossyConvertUCS2toASCII str(aAttributes);
#endif
  // Loop over the space-delimited list of attributes to get the name:value pairs
  aAttributes.Append(kNullCh);  // put an extra null at the end
  PRUnichar* start = aAttributes.BeginWriting();
  PRUnichar* end   = start;
  while ((kNullCh != *start) && (kDashCh != *start)) {
    name.SetLength(0);
    value.SetLength(0);
    // skip leading space, the dash amounts to the end of the line
    while ((kNullCh!=*start) && (kDashCh!=*start) && nsCRT::IsAsciiSpace(*start)) {
      ++start;
    }
    end = start;
    // look for ':' or '='
    while ((kNullCh!=*end) && (kDashCh!=*end) && (kColonCh!=*end) && (kEqualCh!=*end)) {
      ++end;
    }
    if ((kColonCh!=*end) && (kEqualCh!=*end)) {
#ifdef NS_DEBUG
      printf("Bad MathML operator: %s\n", str.get());
#endif
      return PR_TRUE;
    }
    *end = kNullCh; // end segment here
    // this segment is the name
    if (start < end) {
      name.Assign(start);
    }
    start = ++end;
    // look for space or end of line
    while ((kNullCh!=*end) && (kDashCh!=*start) && !nsCRT::IsAsciiSpace(*end)) {
      ++end;
    }
    *end = kNullCh; // end segment here
    // this segment is the value
    if (start < end) {
      value.Assign(start);
    }
    SetProperty(aOperatorData, name, value);
    start = ++end;
  }
  return PR_TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void SetProperty ( OperatorData aOperatorData,
nsString  aName,
nsString  aValue 
)

Definition at line 108 of file nsMathMLOperators.cpp.

{
  if (!aName.Length() || !aValue.Length())
    return;

  // XXX These ones are not kept in the dictionary
  // Support for these requires nsString member variables 
  // maxsize (default: infinity)
  // minsize (default: 1)

  if (aValue.EqualsLiteral("true")) {
    // see if we should enable flags with default value=false
    if (aName.EqualsLiteral("fence"))
      aOperatorData->mFlags |= NS_MATHML_OPERATOR_FENCE;
    else if (aName.EqualsLiteral("accent"))
      aOperatorData->mFlags |= NS_MATHML_OPERATOR_ACCENT;
    else if (aName.EqualsLiteral("largeop"))
      aOperatorData->mFlags |= NS_MATHML_OPERATOR_LARGEOP;
    else if (aName.EqualsLiteral("separator"))
      aOperatorData->mFlags |=  NS_MATHML_OPERATOR_SEPARATOR;
    else if (aName.EqualsLiteral("movablelimits"))
      aOperatorData->mFlags |= NS_MATHML_OPERATOR_MOVABLELIMITS;
  }
  else if (aValue.EqualsLiteral("false")) {
    // see if we should disable flags with default value=true
    if (aName.EqualsLiteral("symmetric"))
      aOperatorData->mFlags &= ~NS_MATHML_OPERATOR_SYMMETRIC;
  }
  else if (aName.EqualsLiteral("stretchy") &&
          (1 == aOperatorData->mStr.Length())) {
    if (aValue.EqualsLiteral("vertical"))
      aOperatorData->mFlags |= NS_MATHML_OPERATOR_STRETCHY_VERT;
    else if (aValue.EqualsLiteral("horizontal"))
      aOperatorData->mFlags |= NS_MATHML_OPERATOR_STRETCHY_HORIZ;
    else return; // invalid value
    if (kNotFound == nsMathMLOperators::FindStretchyOperator(aOperatorData->mStr[0])) {
      gStretchyOperatorArray->AppendElement(aOperatorData);
    }
  }
  else {
    PRInt32 i = 0;
    float space = 0.0f;
    PRBool isLeftSpace;
    if (aName.EqualsLiteral("lspace"))
      isLeftSpace = PR_TRUE;
    else if (aName.EqualsLiteral("rspace"))
      isLeftSpace = PR_FALSE;
    else return;  // input is not applicable

    // See if it is a numeric value (unit is assumed to be 'em')
    if (nsCRT::IsAsciiDigit(aValue[0])) {
      PRInt32 error = 0;
      space = aValue.ToFloat(&error);
      if (error) return;
    }
    // See if it is one of the 'namedspace' (ranging 1/18em...7/18em)
    else if (aValue.EqualsLiteral("veryverythinmathspace"))  i = 1;
    else if (aValue.EqualsLiteral("verythinmathspace"))      i = 2;
    else if (aValue.EqualsLiteral("thinmathspace"))          i = 3;
    else if (aValue.EqualsLiteral("mediummathspace"))        i = 4;
    else if (aValue.EqualsLiteral("thickmathspace"))         i = 5;
    else if (aValue.EqualsLiteral("verythickmathspace"))     i = 6;
    else if (aValue.EqualsLiteral("veryverythickmathspace")) i = 7;

    if (0 != i) // it was a namedspace value
      space = float(i)/float(18);

    if (isLeftSpace)
      aOperatorData->mLeftSpace = space;
    else
      aOperatorData->mRightSpace = space;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 83 of file nsMathMLOperators.cpp.

nsStringArray* gInvariantCharArray = nsnull [static]

Definition at line 82 of file nsMathMLOperators.cpp.

Definition at line 79 of file nsMathMLOperators.cpp.

PRInt32 gOperatorCount = 0 [static]

Definition at line 78 of file nsMathMLOperators.cpp.

Definition at line 75 of file nsMathMLOperators.cpp.

nsHashtable* gOperatorTable = nsnull [static]

Definition at line 80 of file nsMathMLOperators.cpp.

Definition at line 81 of file nsMathMLOperators.cpp.

PRInt32 gTableRefCount = 0 [static]

Definition at line 77 of file nsMathMLOperators.cpp.

const PRUnichar kColonCh = PRUnichar(':') [static]

Definition at line 88 of file nsMathMLOperators.cpp.

const PRUnichar kDashCh = PRUnichar('#') [static]

Definition at line 86 of file nsMathMLOperators.cpp.

const PRUnichar kEqualCh = PRUnichar('=') [static]

Definition at line 87 of file nsMathMLOperators.cpp.

const char* const kMathVariant_name[] [static]
Initial value:
 {
  "normal",
  "bold",
  "italic",
  "bold-italic",
  "sans-serif",
  "bold-sans-serif",
  "sans-serif-italic",
  "sans-serif-bold-italic",
  "monospace",
  "script",
  "bold-script",
  "fraktur",
  "bold-fraktur",
  "double-struck"
}

Definition at line 90 of file nsMathMLOperators.cpp.

const PRUnichar kNullCh = PRUnichar('\0') [static]

Definition at line 85 of file nsMathMLOperators.cpp.