Skip to content

Commit

Permalink
Merge pull request #16 from OskarPersson/textcolor
Browse files Browse the repository at this point in the history
Add support for textcolor. Thanks for adding this.
  • Loading branch information
mgriebling authored Nov 24, 2023
2 parents aa8ae54 + 2f485c9 commit 3964a0c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
36 changes: 36 additions & 0 deletions Sources/SwiftMath/MathRender/MTMathList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public enum MTMathAtomType: Int, CustomStringConvertible, Comparable {
/// Denotes style changes during rendering.
case style
case color
case textcolor
case colorBox

// Atoms after this point are not part of TeX and do not have the usual structure.
Expand Down Expand Up @@ -106,6 +107,7 @@ public enum MTMathAtomType: Int, CustomStringConvertible, Comparable {
case .space: return "Space"
case .style: return "Style"
case .color: return "Color"
case .textcolor: return "TextColor"
case .colorBox: return "Colorbox"
case .table: return "Table"
}
Expand Down Expand Up @@ -235,6 +237,8 @@ public class MTMathAtom: NSObject {
return MTMathSpace(self as? MTMathSpace)
case .color:
return MTMathColor(self as? MTMathColor)
case .textcolor:
return MTMathTextColor(self as? MTMathTextColor)
case .colorBox:
return MTMathColorbox(self as? MTMathColorbox)
case .table:
Expand Down Expand Up @@ -675,6 +679,38 @@ public class MTMathColor: MTMathAtom {
}
}

// MARK: - MTMathTextColor
/** An atom representing an textcolor element.
Note: None of the usual fields of the `MTMathAtom` apply even though this
class inherits from `MTMathAtom`. i.e. it is meaningless to have a value
in the nucleus, subscript or superscript fields. */
public class MTMathTextColor: MTMathAtom {
public var colorString:String=""
public var innerList:MTMathList?

init(_ color: MTMathTextColor?) {
super.init(color)
self.type = .textcolor
self.colorString = color?.colorString ?? ""
self.innerList = MTMathList(color?.innerList)
}

override init() {
super.init()
self.type = .textcolor
}

public override var string: String {
"\\textcolor{\(self.colorString)}{\(self.innerList!.string)}"
}

override public var finalized: MTMathAtom {
let newColor = super.finalized as! MTMathTextColor
newColor.innerList = newColor.innerList?.finalized
return newColor
}
}

// MARK: - MTMathColorbox
/** An atom representing an colorbox element.
Note: None of the usual fields of the `MTMathAtom` apply even though this
Expand Down
6 changes: 6 additions & 0 deletions Sources/SwiftMath/MathRender/MTMathListBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,12 @@ public struct MTMathListBuilder {
mathColor.colorString = self.readColor()!
mathColor.innerList = self.buildInternal(true)
return mathColor
} else if command == "textcolor" {
// A textcolor command has 2 arguments
let mathColor = MTMathTextColor()
mathColor.colorString = self.readColor()!
mathColor.innerList = self.buildInternal(true)
return mathColor
} else if command == "colorbox" {
// A color command has 2 arguments
let mathColorbox = MTMathColorbox()
Expand Down
34 changes: 32 additions & 2 deletions Sources/SwiftMath/MathRender/MTTypesetter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func getInterElementSpaces() -> [[InterElementSpaceType]] {
// Get's the index for the given type. If row is true, the index is for the row (i.e. left element) otherwise it is for the column (right element)
func getInterElementSpaceArrayIndexForType(_ type:MTMathAtomType, row:Bool) -> Int {
switch type {
case .color, .colorBox, .ordinary, .placeholder: // A placeholder is treated as ordinary
case .color, .textcolor, .colorBox, .ordinary, .placeholder: // A placeholder is treated as ordinary
return 0
case .largeOperator:
return 1
Expand Down Expand Up @@ -511,7 +511,37 @@ class MTTypesetter {
display!.position = currentPosition
currentPosition.x += display!.width
displayAtoms.append(display!)


case .textcolor:
// stash the existing layout
if currentLine.length > 0 {
self.addDisplayLine()
}
let colorAtom = atom as! MTMathTextColor
let display = MTTypesetter.createLineForMathList(colorAtom.innerList, font: font, style: style)
display!.localTextColor = MTColor(fromHexString: colorAtom.colorString)

if prevNode != nil {
let subDisplay: MTDisplay = display!.subDisplays[0]
let subDisplayAtom = (subDisplay as? MTCTLineDisplay)!.atoms[0]
let interElementSpace = self.getInterElementSpace(prevNode!.type, right:subDisplayAtom.type)
if currentLine.length > 0 {
if interElementSpace > 0 {
// add a kerning of that space to the previous character
currentLine.addAttribute(kCTKernAttributeName as NSAttributedString.Key,
value:NSNumber(floatLiteral: interElementSpace),
range:currentLine.mutableString.rangeOfComposedCharacterSequence(at: currentLine.length-1))
}
} else {
// increase the space
currentPosition.x += interElementSpace
}
}

display!.position = currentPosition
currentPosition.x += display!.width
displayAtoms.append(display!)

case .colorBox:
// stash the existing layout
if currentLine.length > 0 {
Expand Down

0 comments on commit 3964a0c

Please sign in to comment.