Added mathFonts bundle and initial code translation to test MTTypesetter.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
// The swift-tools-version declares the minimum version of Swift required to build this package.
|
||||
|
||||
import PackageDescription
|
||||
import CoreGraphics
|
||||
|
||||
let package = Package(
|
||||
name: "SwiftMathRender",
|
||||
@@ -20,7 +21,10 @@ let package = Package(
|
||||
// Targets can depend on other targets in this package, and on products in packages this package depends on.
|
||||
.target(
|
||||
name: "SwiftMathRender",
|
||||
dependencies: []),
|
||||
dependencies: [],
|
||||
resources: [
|
||||
.copy("mathFonts.bundle")
|
||||
]),
|
||||
.testTarget(
|
||||
name: "SwiftMathRenderTests",
|
||||
dependencies: ["SwiftMathRender"]),
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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,10 +52,40 @@ 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 {
|
||||
/// The default latex rendering style. i.e. variables are italic and numbers are roman.
|
||||
case defaultStyle = 0,
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
public struct SwiftMathRender {
|
||||
public private(set) var text = "Hello, World!"
|
||||
|
||||
public init() {
|
||||
}
|
||||
}
|
||||
BIN
Sources/SwiftMathRender/mathFonts.bundle/Info.plist
Normal file
BIN
Sources/SwiftMathRender/mathFonts.bundle/Info.plist
Normal file
Binary file not shown.
Binary file not shown.
BIN
Sources/SwiftMathRender/mathFonts.bundle/latinmodern-math.otf
Normal file
BIN
Sources/SwiftMathRender/mathFonts.bundle/latinmodern-math.otf
Normal file
Binary file not shown.
BIN
Sources/SwiftMathRender/mathFonts.bundle/latinmodern-math.plist
Normal file
BIN
Sources/SwiftMathRender/mathFonts.bundle/latinmodern-math.plist
Normal file
Binary file not shown.
BIN
Sources/SwiftMathRender/mathFonts.bundle/texgyretermes-math.otf
Executable file
BIN
Sources/SwiftMathRender/mathFonts.bundle/texgyretermes-math.otf
Executable file
Binary file not shown.
Binary file not shown.
BIN
Sources/SwiftMathRender/mathFonts.bundle/xits-math.otf
Normal file
BIN
Sources/SwiftMathRender/mathFonts.bundle/xits-math.otf
Normal file
Binary file not shown.
BIN
Sources/SwiftMathRender/mathFonts.bundle/xits-math.plist
Normal file
BIN
Sources/SwiftMathRender/mathFonts.bundle/xits-math.plist
Normal file
Binary file not shown.
1568
Tests/SwiftMathRenderTests/MTTypesetterTests.swift
Normal file
1568
Tests/SwiftMathRenderTests/MTTypesetterTests.swift
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user