2008-03-16 05:10:47 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2004-04-18 18:30:37 +04:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1999-09-21 06:12:01 +04:00
|
|
|
*
|
2004-04-18 18:30:37 +04:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1999-09-21 06:12:01 +04:00
|
|
|
*
|
|
|
|
* The Original Code is Mozilla MathML Project.
|
|
|
|
*
|
2004-04-18 18:30:37 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* The University Of Queensland.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1999
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-09-21 06:12:01 +04:00
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Roger B. Sidje <rbs@maths.uq.edu.au>
|
2008-03-16 05:10:47 +03:00
|
|
|
* Karl Tomlinson <karlt+@karlt.net>, Mozilla Corporation
|
2009-09-28 00:32:54 +04:00
|
|
|
* Frederic Wang <fred.wang@free.fr>
|
2004-04-18 18:30:37 +04:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-09-21 06:12:01 +04:00
|
|
|
|
2001-02-02 12:34:15 +03:00
|
|
|
#include "nsCOMPtr.h"
|
1999-09-21 06:12:01 +04:00
|
|
|
#include "nsString.h"
|
2001-02-02 12:34:15 +03:00
|
|
|
#include "nsHashtable.h"
|
2009-01-18 23:14:14 +03:00
|
|
|
#include "nsTArray.h"
|
1999-09-21 06:12:01 +04:00
|
|
|
|
2001-02-02 12:34:15 +03:00
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsIPersistentProperties2.h"
|
|
|
|
#include "nsNetUtil.h"
|
2002-05-15 22:55:21 +04:00
|
|
|
#include "nsCRT.h"
|
1999-09-21 06:12:01 +04:00
|
|
|
|
2001-02-02 12:34:15 +03:00
|
|
|
#include "nsMathMLOperators.h"
|
1999-09-21 06:12:01 +04:00
|
|
|
|
|
|
|
// operator dictionary entry
|
2001-02-02 12:34:15 +03:00
|
|
|
struct OperatorData {
|
|
|
|
OperatorData(void)
|
2001-11-14 15:35:52 +03:00
|
|
|
: mFlags(0),
|
1999-09-21 06:12:01 +04:00
|
|
|
mLeftSpace(0.0f),
|
|
|
|
mRightSpace(0.0f)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// member data
|
|
|
|
nsString mStr;
|
|
|
|
nsOperatorFlags mFlags;
|
|
|
|
float mLeftSpace; // unit is em
|
|
|
|
float mRightSpace; // unit is em
|
|
|
|
};
|
|
|
|
|
2001-02-02 12:34:15 +03:00
|
|
|
static PRInt32 gTableRefCount = 0;
|
2009-11-22 00:11:05 +03:00
|
|
|
static PRUint32 gOperatorCount = 0;
|
2001-02-02 12:34:15 +03:00
|
|
|
static OperatorData* gOperatorArray = nsnull;
|
|
|
|
static nsHashtable* gOperatorTable = nsnull;
|
|
|
|
static PRBool gInitialized = PR_FALSE;
|
2009-02-03 17:42:18 +03:00
|
|
|
static nsTArray<nsString>* gInvariantCharArray = nsnull;
|
2000-03-23 12:56:50 +03:00
|
|
|
|
2001-02-02 12:34:15 +03:00
|
|
|
static const PRUnichar kNullCh = PRUnichar('\0');
|
|
|
|
static const PRUnichar kDashCh = PRUnichar('#');
|
|
|
|
static const PRUnichar kColonCh = PRUnichar(':');
|
1999-09-21 06:12:01 +04:00
|
|
|
|
2001-05-21 18:28:24 +04:00
|
|
|
static const char* const kMathVariant_name[] = {
|
|
|
|
"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"
|
|
|
|
};
|
|
|
|
|
2009-09-28 00:32:54 +04:00
|
|
|
static void
|
|
|
|
SetBooleanProperty(OperatorData* aOperatorData,
|
|
|
|
nsString aName)
|
|
|
|
{
|
|
|
|
if (aName.IsEmpty())
|
|
|
|
return;
|
|
|
|
|
2009-11-22 00:11:05 +03:00
|
|
|
if (aName.EqualsLiteral("stretchy") && (1 == aOperatorData->mStr.Length()))
|
2009-09-28 00:32:54 +04:00
|
|
|
aOperatorData->mFlags |= NS_MATHML_OPERATOR_STRETCHY;
|
2009-11-22 00:11:05 +03:00
|
|
|
else if (aName.EqualsLiteral("fence"))
|
2009-09-28 00:32:54 +04:00
|
|
|
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 (aName.EqualsLiteral("symmetric"))
|
|
|
|
aOperatorData->mFlags |= NS_MATHML_OPERATOR_SYMMETRIC;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-02-02 12:34:15 +03:00
|
|
|
SetProperty(OperatorData* aOperatorData,
|
|
|
|
nsString aName,
|
|
|
|
nsString aValue)
|
|
|
|
{
|
2009-09-28 00:32:54 +04:00
|
|
|
if (aName.IsEmpty() || aValue.IsEmpty())
|
2001-02-02 12:34:15 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
// XXX These ones are not kept in the dictionary
|
|
|
|
// Support for these requires nsString member variables
|
|
|
|
// maxsize (default: infinity)
|
|
|
|
// minsize (default: 1)
|
|
|
|
|
2009-09-28 00:32:54 +04:00
|
|
|
if (aName.EqualsLiteral("direction")) {
|
2004-04-30 17:23:43 +04:00
|
|
|
if (aValue.EqualsLiteral("vertical"))
|
2009-09-28 00:32:54 +04:00
|
|
|
aOperatorData->mFlags |= NS_MATHML_OPERATOR_DIRECTION_VERTICAL;
|
2004-04-30 17:23:43 +04:00
|
|
|
else if (aValue.EqualsLiteral("horizontal"))
|
2009-09-28 00:32:54 +04:00
|
|
|
aOperatorData->mFlags |= NS_MATHML_OPERATOR_DIRECTION_HORIZONTAL;
|
2001-02-02 12:34:15 +03:00
|
|
|
else return; // invalid value
|
2009-09-28 00:32:54 +04:00
|
|
|
} else {
|
2001-02-02 12:34:15 +03:00
|
|
|
PRBool isLeftSpace;
|
2004-04-30 17:23:43 +04:00
|
|
|
if (aName.EqualsLiteral("lspace"))
|
2001-02-02 12:34:15 +03:00
|
|
|
isLeftSpace = PR_TRUE;
|
2004-04-30 17:23:43 +04:00
|
|
|
else if (aName.EqualsLiteral("rspace"))
|
2001-02-02 12:34:15 +03:00
|
|
|
isLeftSpace = PR_FALSE;
|
|
|
|
else return; // input is not applicable
|
|
|
|
|
2009-09-28 00:32:54 +04:00
|
|
|
// aValue is assumed to be a digit from 0 to 7
|
|
|
|
PRInt32 error = 0;
|
|
|
|
float space = aValue.ToFloat(&error) / 18.0;
|
|
|
|
if (error) return;
|
2001-02-02 12:34:15 +03:00
|
|
|
|
|
|
|
if (isLeftSpace)
|
|
|
|
aOperatorData->mLeftSpace = space;
|
|
|
|
else
|
|
|
|
aOperatorData->mRightSpace = space;
|
|
|
|
}
|
|
|
|
}
|
1999-09-21 06:12:01 +04:00
|
|
|
|
2009-09-28 00:32:54 +04:00
|
|
|
static PRBool
|
2001-02-02 12:34:15 +03:00
|
|
|
SetOperator(OperatorData* aOperatorData,
|
|
|
|
nsOperatorFlags aForm,
|
2002-08-30 21:13:34 +04:00
|
|
|
const nsCString& aOperator,
|
|
|
|
nsString& aAttributes)
|
1999-09-21 06:12:01 +04:00
|
|
|
|
|
|
|
{
|
2001-02-02 12:34:15 +03:00
|
|
|
// 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;
|
1999-09-21 06:12:01 +04:00
|
|
|
}
|
2001-02-02 12:34:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
2009-09-28 00:32:54 +04:00
|
|
|
// Add operator to hash table
|
|
|
|
aOperatorData->mFlags |= aForm;
|
2001-02-02 12:34:15 +03:00
|
|
|
aOperatorData->mStr.Assign(value);
|
|
|
|
value.AppendInt(aForm, 10);
|
|
|
|
nsStringKey key(value);
|
|
|
|
gOperatorTable->Put(&key, aOperatorData);
|
|
|
|
|
|
|
|
#ifdef NS_DEBUG
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII str(aAttributes);
|
2001-02-02 12:34:15 +03:00
|
|
|
#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
|
2004-02-29 01:34:07 +03:00
|
|
|
PRUnichar* start = aAttributes.BeginWriting();
|
2001-02-02 12:34:15 +03:00
|
|
|
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;
|
2009-09-28 00:32:54 +04:00
|
|
|
// look for ':'
|
|
|
|
while ((kNullCh!=*end) && (kDashCh!=*end) && !nsCRT::IsAsciiSpace(*end) &&
|
|
|
|
(kColonCh!=*end)) {
|
2001-02-02 12:34:15 +03:00
|
|
|
++end;
|
|
|
|
}
|
2009-09-28 00:32:54 +04:00
|
|
|
// If ':' is not found, then it's a boolean property
|
|
|
|
PRBool IsBooleanProperty = (kColonCh != *end);
|
2001-02-02 12:34:15 +03:00
|
|
|
*end = kNullCh; // end segment here
|
|
|
|
// this segment is the name
|
|
|
|
if (start < end) {
|
|
|
|
name.Assign(start);
|
|
|
|
}
|
2009-09-28 00:32:54 +04:00
|
|
|
if (IsBooleanProperty) {
|
|
|
|
SetBooleanProperty(aOperatorData, name);
|
|
|
|
} else {
|
|
|
|
start = ++end;
|
|
|
|
// look for space or end of line
|
|
|
|
while ((kNullCh!=*end) && (kDashCh!=*end) &&
|
|
|
|
!nsCRT::IsAsciiSpace(*end)) {
|
|
|
|
++end;
|
|
|
|
}
|
|
|
|
*end = kNullCh; // end segment here
|
|
|
|
if (start < end) {
|
|
|
|
// this segment is the value
|
|
|
|
value.Assign(start);
|
|
|
|
}
|
|
|
|
SetProperty(aOperatorData, name, value);
|
2001-02-02 12:34:15 +03:00
|
|
|
}
|
|
|
|
start = ++end;
|
|
|
|
}
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2009-09-28 00:32:54 +04:00
|
|
|
static nsresult
|
2001-02-02 12:34:15 +03:00
|
|
|
InitOperators(void)
|
|
|
|
{
|
|
|
|
// Load the property file containing the Operator Dictionary
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIPersistentProperties> mathfontProp;
|
2003-07-12 01:31:54 +04:00
|
|
|
rv = NS_LoadPersistentPropertiesFromURISpec(getter_AddRefs(mathfontProp),
|
2003-12-19 21:50:34 +03:00
|
|
|
NS_LITERAL_CSTRING("resource://gre/res/fonts/mathfont.properties"));
|
2005-09-16 03:17:17 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-02-02 12:34:15 +03:00
|
|
|
|
2001-05-21 18:28:24 +04:00
|
|
|
// Get the list of invariant chars
|
2008-03-16 05:10:47 +03:00
|
|
|
for (PRInt32 i = 0; i < eMATHVARIANT_COUNT; ++i) {
|
2002-08-30 21:13:34 +04:00
|
|
|
nsCAutoString key(NS_LITERAL_CSTRING("mathvariant."));
|
|
|
|
key.Append(kMathVariant_name[i]);
|
|
|
|
nsAutoString value;
|
2001-05-21 18:28:24 +04:00
|
|
|
mathfontProp->GetStringProperty(key, value);
|
2009-01-18 23:14:14 +03:00
|
|
|
gInvariantCharArray->AppendElement(value); // i.e., gInvariantCharArray[i] holds this list
|
2001-05-21 18:28:24 +04:00
|
|
|
}
|
|
|
|
|
2001-02-02 12:34:15 +03:00
|
|
|
// 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;
|
2002-08-30 21:13:34 +04:00
|
|
|
if (NS_SUCCEEDED(mathfontProp->Enumerate(getter_AddRefs(iterator)))) {
|
2001-02-02 12:34:15 +03:00
|
|
|
PRBool more;
|
2009-11-22 00:11:05 +03:00
|
|
|
PRUint32 index = 0;
|
2002-08-30 21:13:34 +04:00
|
|
|
nsCAutoString name;
|
|
|
|
nsAutoString attributes;
|
2001-02-02 12:34:15 +03:00
|
|
|
while ((NS_SUCCEEDED(iterator->HasMoreElements(&more))) && more) {
|
|
|
|
nsCOMPtr<nsIPropertyElement> element;
|
|
|
|
if (NS_SUCCEEDED(iterator->GetNext(getter_AddRefs(element)))) {
|
2002-08-30 21:13:34 +04:00
|
|
|
if (NS_SUCCEEDED(element->GetKey(name)) &&
|
|
|
|
NS_SUCCEEDED(element->GetValue(attributes))) {
|
2001-02-02 12:34:15 +03:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-09-21 06:12:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-02-02 12:34:15 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-09-28 00:32:54 +04:00
|
|
|
static nsresult
|
2001-02-02 12:34:15 +03:00
|
|
|
InitGlobals()
|
|
|
|
{
|
|
|
|
gInitialized = PR_TRUE;
|
|
|
|
nsresult rv = NS_ERROR_OUT_OF_MEMORY;
|
2009-01-18 23:14:14 +03:00
|
|
|
gInvariantCharArray = new nsTArray<nsString>();
|
2009-11-22 00:11:05 +03:00
|
|
|
if (gInvariantCharArray) {
|
2001-02-02 12:34:15 +03:00
|
|
|
gOperatorTable = new nsHashtable();
|
|
|
|
if (gOperatorTable) {
|
|
|
|
rv = InitOperators();
|
|
|
|
}
|
|
|
|
}
|
2007-08-14 22:42:24 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
nsMathMLOperators::CleanUp();
|
2001-02-02 12:34:15 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2007-08-14 22:42:24 +04:00
|
|
|
void
|
|
|
|
nsMathMLOperators::CleanUp()
|
|
|
|
{
|
|
|
|
if (gInvariantCharArray) {
|
|
|
|
delete gInvariantCharArray;
|
|
|
|
gInvariantCharArray = nsnull;
|
|
|
|
}
|
|
|
|
if (gOperatorArray) {
|
|
|
|
delete[] gOperatorArray;
|
|
|
|
gOperatorArray = nsnull;
|
|
|
|
}
|
|
|
|
if (gOperatorTable) {
|
|
|
|
delete gOperatorTable;
|
|
|
|
gOperatorTable = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-02 12:34:15 +03:00
|
|
|
void
|
|
|
|
nsMathMLOperators::AddRefTable(void)
|
|
|
|
{
|
|
|
|
gTableRefCount++;
|
1999-09-21 06:12:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsMathMLOperators::ReleaseTable(void)
|
|
|
|
{
|
|
|
|
if (0 == --gTableRefCount) {
|
2007-08-14 22:42:24 +04:00
|
|
|
CleanUp();
|
1999-09-21 06:12:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-22 01:25:55 +03:00
|
|
|
static OperatorData*
|
|
|
|
GetOperatorData(const nsString& aOperator, nsOperatorFlags aForm)
|
|
|
|
{
|
|
|
|
nsAutoString key(aOperator);
|
|
|
|
key.AppendInt(aForm);
|
|
|
|
nsStringKey hkey(key);
|
|
|
|
return (OperatorData*)gOperatorTable->Get(&hkey);
|
|
|
|
}
|
|
|
|
|
1999-09-21 06:12:01 +04:00
|
|
|
PRBool
|
2001-02-02 12:34:15 +03:00
|
|
|
nsMathMLOperators::LookupOperator(const nsString& aOperator,
|
1999-09-21 06:12:01 +04:00
|
|
|
const nsOperatorFlags aForm,
|
|
|
|
nsOperatorFlags* aFlags,
|
|
|
|
float* aLeftSpace,
|
|
|
|
float* aRightSpace)
|
|
|
|
{
|
2001-02-02 12:34:15 +03:00
|
|
|
if (!gInitialized) {
|
|
|
|
InitGlobals();
|
|
|
|
}
|
|
|
|
if (gOperatorTable) {
|
|
|
|
NS_ASSERTION(aFlags && aLeftSpace && aRightSpace, "bad usage");
|
2009-11-22 01:25:55 +03:00
|
|
|
NS_ASSERTION(aForm > 0 && aForm < 4, "*** invalid call ***");
|
|
|
|
|
|
|
|
// The MathML REC says:
|
|
|
|
// If the operator does not occur in the dictionary with the specified form,
|
|
|
|
// the renderer should use one of the forms which is available there, in the
|
|
|
|
// order of preference: infix, postfix, prefix.
|
1999-12-10 15:47:24 +03:00
|
|
|
|
2001-02-02 12:34:15 +03:00
|
|
|
OperatorData* found;
|
|
|
|
PRInt32 form = NS_MATHML_OPERATOR_GET_FORM(aForm);
|
2009-11-22 01:25:55 +03:00
|
|
|
if (!(found = GetOperatorData(aOperator, form))) {
|
|
|
|
if (form == NS_MATHML_OPERATOR_FORM_INFIX ||
|
|
|
|
!(found =
|
|
|
|
GetOperatorData(aOperator, NS_MATHML_OPERATOR_FORM_INFIX))) {
|
|
|
|
if (form == NS_MATHML_OPERATOR_FORM_POSTFIX ||
|
|
|
|
!(found =
|
|
|
|
GetOperatorData(aOperator, NS_MATHML_OPERATOR_FORM_POSTFIX))) {
|
2001-02-02 12:34:15 +03:00
|
|
|
if (form != NS_MATHML_OPERATOR_FORM_PREFIX) {
|
2009-11-22 01:25:55 +03:00
|
|
|
found = GetOperatorData(aOperator, NS_MATHML_OPERATOR_FORM_PREFIX);
|
2001-02-02 12:34:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-09-21 06:12:01 +04:00
|
|
|
if (found) {
|
2001-02-02 12:34:15 +03:00
|
|
|
NS_ASSERTION(found->mStr.Equals(aOperator), "bad setup");
|
1999-09-21 06:12:01 +04:00
|
|
|
*aLeftSpace = found->mLeftSpace;
|
|
|
|
*aRightSpace = found->mRightSpace;
|
2001-02-02 12:34:15 +03:00
|
|
|
*aFlags &= ~NS_MATHML_OPERATOR_FORM; // clear the form bits
|
2000-05-08 11:12:39 +04:00
|
|
|
*aFlags |= found->mFlags; // just add bits without overwriting
|
1999-09-21 06:12:01 +04:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2002-02-06 23:48:35 +03:00
|
|
|
void
|
|
|
|
nsMathMLOperators::LookupOperators(const nsString& aOperator,
|
|
|
|
nsOperatorFlags* aFlags,
|
|
|
|
float* aLeftSpace,
|
|
|
|
float* aRightSpace)
|
2000-03-23 12:56:50 +03:00
|
|
|
{
|
2001-02-02 12:34:15 +03:00
|
|
|
if (!gInitialized) {
|
|
|
|
InitGlobals();
|
|
|
|
}
|
2002-02-06 23:48:35 +03:00
|
|
|
|
|
|
|
aFlags[NS_MATHML_OPERATOR_FORM_INFIX] = 0;
|
|
|
|
aLeftSpace[NS_MATHML_OPERATOR_FORM_INFIX] = 0.0f;
|
|
|
|
aRightSpace[NS_MATHML_OPERATOR_FORM_INFIX] = 0.0f;
|
|
|
|
|
|
|
|
aFlags[NS_MATHML_OPERATOR_FORM_POSTFIX] = 0;
|
|
|
|
aLeftSpace[NS_MATHML_OPERATOR_FORM_POSTFIX] = 0.0f;
|
|
|
|
aRightSpace[NS_MATHML_OPERATOR_FORM_POSTFIX] = 0.0f;
|
|
|
|
|
|
|
|
aFlags[NS_MATHML_OPERATOR_FORM_PREFIX] = 0;
|
|
|
|
aLeftSpace[NS_MATHML_OPERATOR_FORM_PREFIX] = 0.0f;
|
|
|
|
aRightSpace[NS_MATHML_OPERATOR_FORM_PREFIX] = 0.0f;
|
|
|
|
|
2001-02-02 12:34:15 +03:00
|
|
|
if (gOperatorTable) {
|
|
|
|
OperatorData* found;
|
2009-11-22 01:25:55 +03:00
|
|
|
found = GetOperatorData(aOperator, NS_MATHML_OPERATOR_FORM_INFIX);
|
2002-02-06 23:48:35 +03:00
|
|
|
if (found) {
|
|
|
|
aFlags[NS_MATHML_OPERATOR_FORM_INFIX] = found->mFlags;
|
|
|
|
aLeftSpace[NS_MATHML_OPERATOR_FORM_INFIX] = found->mLeftSpace;
|
|
|
|
aRightSpace[NS_MATHML_OPERATOR_FORM_INFIX] = found->mRightSpace;
|
2000-05-08 11:12:39 +04:00
|
|
|
}
|
2009-11-22 01:25:55 +03:00
|
|
|
found = GetOperatorData(aOperator, NS_MATHML_OPERATOR_FORM_POSTFIX);
|
2002-02-06 23:48:35 +03:00
|
|
|
if (found) {
|
|
|
|
aFlags[NS_MATHML_OPERATOR_FORM_POSTFIX] = found->mFlags;
|
|
|
|
aLeftSpace[NS_MATHML_OPERATOR_FORM_POSTFIX] = found->mLeftSpace;
|
|
|
|
aRightSpace[NS_MATHML_OPERATOR_FORM_POSTFIX] = found->mRightSpace;
|
2000-05-08 11:12:39 +04:00
|
|
|
}
|
2009-11-22 01:25:55 +03:00
|
|
|
found = GetOperatorData(aOperator, NS_MATHML_OPERATOR_FORM_PREFIX);
|
2002-02-06 23:48:35 +03:00
|
|
|
if (found) {
|
|
|
|
aFlags[NS_MATHML_OPERATOR_FORM_PREFIX] = found->mFlags;
|
|
|
|
aLeftSpace[NS_MATHML_OPERATOR_FORM_PREFIX] = found->mLeftSpace;
|
|
|
|
aRightSpace[NS_MATHML_OPERATOR_FORM_PREFIX] = found->mRightSpace;
|
2000-05-08 11:12:39 +04:00
|
|
|
}
|
2000-03-23 12:56:50 +03:00
|
|
|
}
|
2002-02-06 23:48:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsMathMLOperators::IsMutableOperator(const nsString& aOperator)
|
|
|
|
{
|
|
|
|
if (!gInitialized) {
|
|
|
|
InitGlobals();
|
|
|
|
}
|
|
|
|
// lookup all the variants of the operator and return true if there
|
|
|
|
// is a variant that is stretchy or largeop
|
|
|
|
nsOperatorFlags flags[4];
|
|
|
|
float lspace[4], rspace[4];
|
|
|
|
nsMathMLOperators::LookupOperators(aOperator, flags, lspace, rspace);
|
|
|
|
nsOperatorFlags allFlags =
|
|
|
|
flags[NS_MATHML_OPERATOR_FORM_INFIX] |
|
|
|
|
flags[NS_MATHML_OPERATOR_FORM_POSTFIX] |
|
|
|
|
flags[NS_MATHML_OPERATOR_FORM_PREFIX];
|
|
|
|
return NS_MATHML_OPERATOR_IS_STRETCHY(allFlags) ||
|
|
|
|
NS_MATHML_OPERATOR_IS_LARGEOP(allFlags);
|
2000-03-23 12:56:50 +03:00
|
|
|
}
|
2000-03-12 14:12:57 +03:00
|
|
|
|
2009-11-22 00:11:05 +03:00
|
|
|
/* static */ nsStretchDirection
|
|
|
|
nsMathMLOperators::GetStretchyDirection(const nsString& aOperator)
|
1999-09-21 06:12:01 +04:00
|
|
|
{
|
2009-11-22 00:11:05 +03:00
|
|
|
// LookupOperator will search infix, postfix and prefix forms of aOperator and
|
|
|
|
// return the first form found. It is assumed that all these forms have same
|
|
|
|
// direction.
|
|
|
|
nsOperatorFlags flags = 0;
|
|
|
|
float dummy;
|
|
|
|
nsMathMLOperators::LookupOperator(aOperator,
|
|
|
|
NS_MATHML_OPERATOR_FORM_INFIX,
|
|
|
|
&flags, &dummy, &dummy);
|
|
|
|
|
|
|
|
if (NS_MATHML_OPERATOR_IS_DIRECTION_VERTICAL(flags)) {
|
|
|
|
return NS_STRETCH_DIRECTION_VERTICAL;
|
|
|
|
} else if (NS_MATHML_OPERATOR_IS_DIRECTION_HORIZONTAL(flags)) {
|
|
|
|
return NS_STRETCH_DIRECTION_HORIZONTAL;
|
|
|
|
} else {
|
|
|
|
return NS_STRETCH_DIRECTION_UNSUPPORTED;
|
2001-02-02 12:34:15 +03:00
|
|
|
}
|
|
|
|
}
|
2001-05-21 18:28:24 +04:00
|
|
|
|
2008-03-16 05:10:47 +03:00
|
|
|
/* static */ eMATHVARIANT
|
|
|
|
nsMathMLOperators::LookupInvariantChar(const nsAString& aChar)
|
2001-05-21 18:28:24 +04:00
|
|
|
{
|
|
|
|
if (!gInitialized) {
|
|
|
|
InitGlobals();
|
|
|
|
}
|
2002-02-08 03:42:32 +03:00
|
|
|
if (gInvariantCharArray) {
|
2009-01-18 23:14:14 +03:00
|
|
|
for (PRInt32 i = gInvariantCharArray->Length()-1; i >= 0; --i) {
|
|
|
|
const nsString& list = gInvariantCharArray->ElementAt(i);
|
2008-03-16 05:10:47 +03:00
|
|
|
nsString::const_iterator start, end;
|
2009-01-18 23:14:14 +03:00
|
|
|
list.BeginReading(start);
|
|
|
|
list.EndReading(end);
|
2008-03-16 05:10:47 +03:00
|
|
|
// Style-invariant characters are at offset 3*j + 1.
|
|
|
|
if (FindInReadable(aChar, start, end) &&
|
|
|
|
start.size_backward() % 3 == 1) {
|
|
|
|
return eMATHVARIANT(i);
|
2001-05-21 18:28:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-03-16 05:10:47 +03:00
|
|
|
return eMATHVARIANT_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ const nsDependentSubstring
|
|
|
|
nsMathMLOperators::TransformVariantChar(const PRUnichar& aChar,
|
|
|
|
eMATHVARIANT aVariant)
|
|
|
|
{
|
|
|
|
if (!gInitialized) {
|
|
|
|
InitGlobals();
|
|
|
|
}
|
|
|
|
if (gInvariantCharArray) {
|
2009-01-18 23:14:14 +03:00
|
|
|
nsString list = gInvariantCharArray->ElementAt(aVariant);
|
|
|
|
PRInt32 index = list.FindChar(aChar);
|
2008-03-16 05:10:47 +03:00
|
|
|
// BMP characters are at offset 3*j
|
2009-01-18 23:14:14 +03:00
|
|
|
if (index != kNotFound && index % 3 == 0 && list.Length() - index >= 2 ) {
|
2008-03-16 05:10:47 +03:00
|
|
|
// The style-invariant character is the next character
|
|
|
|
// (and list should contain padding if the next character is in the BMP).
|
|
|
|
++index;
|
2009-01-18 23:14:14 +03:00
|
|
|
PRUint32 len = NS_IS_HIGH_SURROGATE(list.CharAt(index)) ? 2 : 1;
|
|
|
|
return nsDependentSubstring(list, index, len);
|
2008-03-16 05:10:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nsDependentSubstring(&aChar, &aChar + 1);
|
2001-05-21 18:28:24 +04:00
|
|
|
}
|