Added mathFonts bundle and initial code translation to test MTTypesetter.

This commit is contained in:
Michael Griebling
2023-01-12 08:32:02 -05:00
parent 82f8b08c61
commit 2ee1cc0b19
15 changed files with 1635 additions and 25 deletions

View File

@@ -33,12 +33,12 @@ public class MTFont {
// In particular it does not have the math italic characters which breaks our variable rendering.
// So we first load a CGFont from the file and then convert it to a CTFont.
self.init()
print("Loading font %@", name);
print("Loading font \(name)")
let bundle = MTFont.fontBundle
let fontPath = bundle.path(forResource: name, ofType: "otf")
let fontDataProvider = CGDataProvider(filename: fontPath!)
self.defaultCGFont = CGFont(fontDataProvider!)!
print("Num glyphs: %zd", self.defaultCGFont.numberOfGlyphs)
print("Num glyphs: \(self.defaultCGFont.numberOfGlyphs)")
self.ctFont = CTFontCreateWithGraphicsFont(self.defaultCGFont, size, nil, nil);
@@ -50,7 +50,8 @@ public class MTFont {
static var fontBundle:Bundle {
// Uses bundle for class so that this can be access by the unit tests.
return Bundle(url: Bundle(for: self).url(forResource: "iosMathFonts", withExtension: "bundle")!)!
let url = Bundle.module.url(forResource: "mathFonts", withExtension: "bundle")!
return Bundle(url: url)!
}
func copy(withSize size: CGFloat) -> MTFont {

View File

@@ -120,6 +120,11 @@ public class MTMathAtomFactory {
return _accentValueToName!
}
static var supportedLatexSymbolNames:[String] {
let commands = MTMathAtomFactory.supportedLatexSymbols
return commands.keys.map { String($0) }
}
static var supportedLatexSymbols: [String: MTMathAtom] = [
"square" : MTMathAtomFactory.placeholder(),
@@ -663,13 +668,21 @@ public class MTMathAtomFactory {
/** Returns a fraction with the given numerator and denominator. */
public static func fraction(withNumerator num: MTMathList, denominator denom: MTMathList) -> MTFraction {
let frac = MTFraction()
frac.numerator = num
frac.denominator = denom
return frac
}
public static func mathListForCharacters(_ chars:String) -> MTMathList? {
let list = MTMathList()
for ch in chars {
if let atom = self.atom(forCharacter: ch) {
list.add(atom)
}
}
return list
}
/** Simplification of above function when numerator and denominator are simple strings.
This function uses `mathListForCharacters` to convert the strings to `MTMathList`s. */
public static func fraction(withNumeratorString numStr: String, denominatorString denomStr: String) -> MTFraction {

View File

@@ -8,8 +8,9 @@
import Foundation
// type defines spacing and how it is rendered
public enum MTMathAtomType: String, CustomStringConvertible {
case ordinary // number or text
public enum MTMathAtomType: Int, CustomStringConvertible, Comparable {
case ordinary = 1 // number or text
case number // number
case variable // text in italic
case largeOperator // sin/cos, integral
@@ -28,15 +29,15 @@ public enum MTMathAtomType: String, CustomStringConvertible {
case accent // accented atom
// these atoms do not support subscripts/superscripts:
case boundary
case space
case boundary = 101
case space = 201
// Denotes style changes during randering
case style
case color
case colorBox
case table
case table = 1001
func isNotBinaryOperator() -> Bool {
switch self {
@@ -51,8 +52,38 @@ public enum MTMathAtomType: String, CustomStringConvertible {
// we want string representations to be capitalized
public var description: String {
self.rawValue.capitalized
switch self {
case .ordinary: return "Ordinary"
case .number: return "Number"
case .variable: return "Variable"
case .largeOperator: return "Large Operator"
case .binaryOperator: return "Binary Operator"
case .unaryOperator: return "Unary Operator"
case .relation: return "Relation"
case .open: return "Open"
case .close: return "Close"
case .fraction: return "Fraction"
case .radical: return "Radical"
case .punctuation: return "Punctuation"
case .placeholder: return "Placeholder"
case .inner: return "Inner"
case .underline: return "Underline"
case .overline: return "Overline"
case .accent: return "Accent"
case .boundary: return "Boundary"
case .space: return "Space"
case .style: return "Style"
case .color: return "Color"
case .colorBox: return "Colorbox"
case .table: return "Table"
}
}
// comparable support
public static func < (lhs: MTMathAtomType, rhs: MTMathAtomType) -> Bool {
lhs.rawValue < rhs.rawValue
}
}
public enum MTFontStyle:Int {

View File

@@ -378,7 +378,11 @@ class MTTypesetter {
var currentLine:NSMutableAttributedString!
var currentAtoms = [MTMathAtom]() // List of atoms that make the line
var currentLineIndexRange = NSMakeRange(0, 0)
var style:MTLineStyle = .display
var style:MTLineStyle {
didSet {
self.styleFont = self.font.copy(withSize: Self.getStyleSize(self.style, font: self.font))
}
}
var styleFont:MTFont!
var cramped = false
var spaced = false
@@ -472,11 +476,6 @@ class MTTypesetter {
}
}
func setStyle(_ style:MTLineStyle) {
self.style = style
self.styleFont = self.font.copy(withSize: Self.getStyleSize(self.style, font: self.font)) //copyFontWithSize:[self.class] getStyleSize:style font:font])
}
func addInterElementSpace(_ prevNode:MTMathAtom?, currentType type:MTMathAtomType) {
var interElementSpace = CGFloat(0)
if prevNode != nil {
@@ -754,7 +753,7 @@ class MTTypesetter {
if (currentLine.length > 0) {
self.addDisplayLine()
}
if spaced && !lastType.rawValue.isEmpty {
if spaced {
// If spaced then add an interelement space between the last type and close
let display = displayAtoms.last
let interElementSpace = self.getInterElementSpace(lastType, right:.close)

View File

@@ -1,6 +0,0 @@
public struct SwiftMathRender {
public private(set) var text = "Hello, World!"
public init() {
}
}

Binary file not shown.

Binary file not shown.