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.
|
// The swift-tools-version declares the minimum version of Swift required to build this package.
|
||||||
|
|
||||||
import PackageDescription
|
import PackageDescription
|
||||||
|
import CoreGraphics
|
||||||
|
|
||||||
let package = Package(
|
let package = Package(
|
||||||
name: "SwiftMathRender",
|
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.
|
// Targets can depend on other targets in this package, and on products in packages this package depends on.
|
||||||
.target(
|
.target(
|
||||||
name: "SwiftMathRender",
|
name: "SwiftMathRender",
|
||||||
dependencies: []),
|
dependencies: [],
|
||||||
|
resources: [
|
||||||
|
.copy("mathFonts.bundle")
|
||||||
|
]),
|
||||||
.testTarget(
|
.testTarget(
|
||||||
name: "SwiftMathRenderTests",
|
name: "SwiftMathRenderTests",
|
||||||
dependencies: ["SwiftMathRender"]),
|
dependencies: ["SwiftMathRender"]),
|
||||||
|
|||||||
@@ -33,12 +33,12 @@ public class MTFont {
|
|||||||
// In particular it does not have the math italic characters which breaks our variable rendering.
|
// 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.
|
// So we first load a CGFont from the file and then convert it to a CTFont.
|
||||||
self.init()
|
self.init()
|
||||||
print("Loading font %@", name);
|
print("Loading font \(name)")
|
||||||
let bundle = MTFont.fontBundle
|
let bundle = MTFont.fontBundle
|
||||||
let fontPath = bundle.path(forResource: name, ofType: "otf")
|
let fontPath = bundle.path(forResource: name, ofType: "otf")
|
||||||
let fontDataProvider = CGDataProvider(filename: fontPath!)
|
let fontDataProvider = CGDataProvider(filename: fontPath!)
|
||||||
self.defaultCGFont = CGFont(fontDataProvider!)!
|
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);
|
self.ctFont = CTFontCreateWithGraphicsFont(self.defaultCGFont, size, nil, nil);
|
||||||
|
|
||||||
@@ -50,7 +50,8 @@ public class MTFont {
|
|||||||
|
|
||||||
static var fontBundle:Bundle {
|
static var fontBundle:Bundle {
|
||||||
// Uses bundle for class so that this can be access by the unit tests.
|
// 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 {
|
func copy(withSize size: CGFloat) -> MTFont {
|
||||||
|
|||||||
@@ -120,6 +120,11 @@ public class MTMathAtomFactory {
|
|||||||
return _accentValueToName!
|
return _accentValueToName!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static var supportedLatexSymbolNames:[String] {
|
||||||
|
let commands = MTMathAtomFactory.supportedLatexSymbols
|
||||||
|
return commands.keys.map { String($0) }
|
||||||
|
}
|
||||||
|
|
||||||
static var supportedLatexSymbols: [String: MTMathAtom] = [
|
static var supportedLatexSymbols: [String: MTMathAtom] = [
|
||||||
"square" : MTMathAtomFactory.placeholder(),
|
"square" : MTMathAtomFactory.placeholder(),
|
||||||
|
|
||||||
@@ -663,13 +668,21 @@ public class MTMathAtomFactory {
|
|||||||
/** Returns a fraction with the given numerator and denominator. */
|
/** Returns a fraction with the given numerator and denominator. */
|
||||||
public static func fraction(withNumerator num: MTMathList, denominator denom: MTMathList) -> MTFraction {
|
public static func fraction(withNumerator num: MTMathList, denominator denom: MTMathList) -> MTFraction {
|
||||||
let frac = MTFraction()
|
let frac = MTFraction()
|
||||||
|
|
||||||
frac.numerator = num
|
frac.numerator = num
|
||||||
frac.denominator = denom
|
frac.denominator = denom
|
||||||
|
|
||||||
return frac
|
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.
|
/** Simplification of above function when numerator and denominator are simple strings.
|
||||||
This function uses `mathListForCharacters` to convert the strings to `MTMathList`s. */
|
This function uses `mathListForCharacters` to convert the strings to `MTMathList`s. */
|
||||||
public static func fraction(withNumeratorString numStr: String, denominatorString denomStr: String) -> MTFraction {
|
public static func fraction(withNumeratorString numStr: String, denominatorString denomStr: String) -> MTFraction {
|
||||||
|
|||||||
@@ -8,8 +8,9 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
// type defines spacing and how it is rendered
|
// type defines spacing and how it is rendered
|
||||||
public enum MTMathAtomType: String, CustomStringConvertible {
|
public enum MTMathAtomType: Int, CustomStringConvertible, Comparable {
|
||||||
case ordinary // number or text
|
|
||||||
|
case ordinary = 1 // number or text
|
||||||
case number // number
|
case number // number
|
||||||
case variable // text in italic
|
case variable // text in italic
|
||||||
case largeOperator // sin/cos, integral
|
case largeOperator // sin/cos, integral
|
||||||
@@ -28,15 +29,15 @@ public enum MTMathAtomType: String, CustomStringConvertible {
|
|||||||
case accent // accented atom
|
case accent // accented atom
|
||||||
|
|
||||||
// these atoms do not support subscripts/superscripts:
|
// these atoms do not support subscripts/superscripts:
|
||||||
case boundary
|
case boundary = 101
|
||||||
case space
|
case space = 201
|
||||||
|
|
||||||
// Denotes style changes during randering
|
// Denotes style changes during randering
|
||||||
case style
|
case style
|
||||||
case color
|
case color
|
||||||
case colorBox
|
case colorBox
|
||||||
|
|
||||||
case table
|
case table = 1001
|
||||||
|
|
||||||
func isNotBinaryOperator() -> Bool {
|
func isNotBinaryOperator() -> Bool {
|
||||||
switch self {
|
switch self {
|
||||||
@@ -51,8 +52,38 @@ public enum MTMathAtomType: String, CustomStringConvertible {
|
|||||||
|
|
||||||
// we want string representations to be capitalized
|
// we want string representations to be capitalized
|
||||||
public var description: String {
|
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 {
|
public enum MTFontStyle:Int {
|
||||||
|
|||||||
@@ -378,7 +378,11 @@ class MTTypesetter {
|
|||||||
var currentLine:NSMutableAttributedString!
|
var currentLine:NSMutableAttributedString!
|
||||||
var currentAtoms = [MTMathAtom]() // List of atoms that make the line
|
var currentAtoms = [MTMathAtom]() // List of atoms that make the line
|
||||||
var currentLineIndexRange = NSMakeRange(0, 0)
|
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 styleFont:MTFont!
|
||||||
var cramped = false
|
var cramped = false
|
||||||
var spaced = 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) {
|
func addInterElementSpace(_ prevNode:MTMathAtom?, currentType type:MTMathAtomType) {
|
||||||
var interElementSpace = CGFloat(0)
|
var interElementSpace = CGFloat(0)
|
||||||
if prevNode != nil {
|
if prevNode != nil {
|
||||||
@@ -754,7 +753,7 @@ class MTTypesetter {
|
|||||||
if (currentLine.length > 0) {
|
if (currentLine.length > 0) {
|
||||||
self.addDisplayLine()
|
self.addDisplayLine()
|
||||||
}
|
}
|
||||||
if spaced && !lastType.rawValue.isEmpty {
|
if spaced {
|
||||||
// If spaced then add an interelement space between the last type and close
|
// If spaced then add an interelement space between the last type and close
|
||||||
let display = displayAtoms.last
|
let display = displayAtoms.last
|
||||||
let interElementSpace = self.getInterElementSpace(lastType, right:.close)
|
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