Passing about half of the MathListTest tests. Need to work on copy-related tests.

Currently the copy() is not working.
This commit is contained in:
Michael Griebling
2023-01-09 10:59:07 -05:00
parent aef877099e
commit 04de18e5c9
5 changed files with 679 additions and 64 deletions

View File

@@ -120,7 +120,7 @@ public class MTMathAtomFactory {
return _accentValueToName!
}
public static var supportedLatexSymbols: [String: MTMathAtom] = [
static var supportedLatexSymbols: [String: MTMathAtom] = [
"square" : MTMathAtomFactory.placeholder(),
// Greek characters
@@ -448,35 +448,16 @@ public class MTMathAtomFactory {
public static func fontNameForStyle(_ fontStyle:MTFontStyle) -> String {
switch fontStyle {
case .defaultStyle:
return "mathnormal";
case .roman:
return "mathrm";
case .bold:
return "mathbf";
case .fraktur:
return "mathfrak";
case .caligraphic:
return "mathcal";
case .italic:
return "mathit";
case .sansSerif:
return "mathsf";
case .blackboard:
return "mathbb";
case .typewriter:
return "mathtt";
case .boldItalic:
return "bm";
case .defaultStyle: return "mathnormal"
case .roman: return "mathrm"
case .bold: return "mathbf"
case .fraktur: return "mathfrak"
case .caligraphic: return "mathcal"
case .italic: return "mathit"
case .sansSerif: return "mathsf"
case .blackboard: return "mathbb"
case .typewriter: return "mathtt"
case .boldItalic: return "bm"
}
}
@@ -599,6 +580,7 @@ public class MTMathAtomFactory {
}
if let atom = supportedLatexSymbols[name] {
// FIXME: A kludge - objects should be copied here
if name == "int" { return MTMathAtomFactory.operatorWithName( "\u{222B}", limits: false) }
if name == "sum" { return MTMathAtomFactory.operatorWithName( "\u{2211}", limits: true) }
return atom
@@ -707,6 +689,15 @@ public class MTMathAtomFactory {
@note The reason this function returns a `MTMathAtom` and not a `MTMathTable` is because some
matrix environments are have builtin delimiters added to the table and hence are returned as inner atoms.
*/
static let matrixEnvs = [
"matrix": [],
"pmatrix": ["(", ")"],
"bmatrix": ["[", "]"],
"Bmatrix": ["{", "}"],
"vmatrix": ["vert", "vert"],
"Vmatrix": ["Vert", "Vert"]
]
public static func table(withEnvironment env: String?, rows: [[MTMathList]], error:inout NSError?) -> MTMathAtom? {
let table = MTMathTable(environment: env)
@@ -717,15 +708,6 @@ public class MTMathAtomFactory {
}
}
let matrixEnvs = [
"matrix": [],
"pmatrix": ["(", ")"],
"bmatrix": ["[", "]"],
"Bmatrix": ["{", "}"],
"vmatrix": ["vert", "vert"],
"Vmatrix": ["Vert", "Vert"]
]
if env == nil {
table.interColumnSpacing = 0
table.interRowAdditionalSpacing = 1

View File

@@ -341,7 +341,6 @@ public class MTLargeOperator: MTMathAtom {
init(value: String, limits: Bool) {
super.init(type: .largeOperator, value: value)
self.limits = limits
//print("Operator \(value) limits:\(limits)")
}
public override func copy(with zone: NSZone? = nil) -> Any {
@@ -740,7 +739,7 @@ public class MTMathList: NSObject, NSCopying {
if prevNode != nil && prevNode!.type == .binaryOperator {
prevNode!.type = .unaryOperator
finalizedList.removeLastAtom()
finalizedList.add(prevNode!)
finalizedList.add(prevNode)
}
return finalizedList
@@ -758,7 +757,19 @@ public class MTMathList: NSObject, NSCopying {
self.atoms = []
}
func add(_ atom: MTMathAtom) {
func NSParamException(_ param:Any?) {
if param == nil {
NSException(name: NSExceptionName(rawValue: "Error"), reason: "Parameter cannot be nil").raise()
}
}
func NSIndexException(_ array:[Any], index: Int) {
guard !array.indices.contains(index) else { return }
NSException(name: NSExceptionName(rawValue: "Error"), reason: "Index \(index) out of bounds").raise()
}
func add(_ atom: MTMathAtom?) {
guard let atom = atom else { return }
if self.isAtomAllowed(atom) {
self.atoms.append(atom)
} else {
@@ -766,15 +777,21 @@ public class MTMathList: NSObject, NSCopying {
}
}
func insert(_ atom: MTMathAtom, at index: Int) {
func insert(_ atom: MTMathAtom?, at index: Int) {
// NSParamException(atom)
guard let atom = atom else { return }
guard self.atoms.indices.contains(index) || index == self.atoms.endIndex else { return }
// guard self.atoms.endIndex >= index else { NSIndexException(); return }
if self.isAtomAllowed(atom) {
// NSIndexException(self.atoms, index: index)
self.atoms.insert(atom, at: index)
} else {
NSException(name: NSExceptionName(rawValue: "Error"), reason: "Cannot add atom of type \(atom.type.rawValue) into mathlist").raise()
}
}
func append(_ list: MTMathList) {
func append(_ list: MTMathList?) {
guard let list = list else { return }
self.atoms += list.atoms
}
@@ -785,14 +802,17 @@ public class MTMathList: NSObject, NSCopying {
}
func removeAtom(at index: Int) {
NSIndexException(self.atoms, index:index)
self.atoms.remove(at: index)
}
func removeAtoms(in range: ClosedRange<Int>) {
NSIndexException(self.atoms, index: range.lowerBound)
NSIndexException(self.atoms, index: range.upperBound)
self.atoms.removeSubrange(range)
}
func isAtomAllowed(_ atom: MTMathAtom) -> Bool {
return atom.type != .boundary
func isAtomAllowed(_ atom: MTMathAtom?) -> Bool {
return atom?.type != .boundary
}
}

View File

@@ -10,7 +10,7 @@ import Foundation
/** `MTMathListBuilder` is a class for parsing LaTeX into an `MTMathList` that
can be rendered and processed mathematically.
*/
class MTEnvProperties {
struct MTEnvProperties {
var envName: String?
var ended: Bool
var numRows: Int
@@ -376,7 +376,7 @@ public class MTMathListBuilder {
// this puts us in a recursive routine, and sets oneCharOnly to false and no stop character
let subList = self.buildInternal(false, stopChar: "}")
prevAtom = subList!.atoms.last
list.append(subList!)
list.append(subList)
if oneCharOnly {
return list
}
@@ -425,7 +425,7 @@ public class MTMathListBuilder {
// (note setError will not set the error if there is already one, so we flag internal error
// in the odd case that an _error is not set.
self.setError(.internalError, message:"Internal error")
return nil;
return nil
}
} else if char == "&" {
assert(!oneCharOnly, "This should have been handled before")
@@ -446,7 +446,7 @@ public class MTMathListBuilder {
assert(atom != nil, "Atom shouldn't be nil")
atom?.fontStyle = currentFontStyle
list.add(atom!)
list.add(atom)
prevAtom = atom
if oneCharOnly {