-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #289 from migueldeicaza/cursorRenderContentsOnLayer
The caret will now render the character underneath it.
- Loading branch information
Showing
9 changed files
with
275 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Miguel de Icaza on 4/16/23. | ||
// | ||
|
||
import Foundation | ||
import CoreText | ||
|
||
extension CaretView { | ||
func drawCursor (in context: CGContext, hasFocus: Bool) { | ||
guard let ctline else { | ||
return | ||
} | ||
guard let terminal else { | ||
return | ||
} | ||
context.setFillColor(TTColor.clear.cgColor) | ||
context.fill ([bounds]) | ||
|
||
if !hasFocus { | ||
context.setStrokeColor(bgColor) | ||
context.setLineWidth(2) | ||
context.stroke(bounds) | ||
return | ||
} | ||
context.setFillColor(bgColor) | ||
let region: CGRect | ||
switch style { | ||
case .blinkBar, .steadyBar: | ||
region = CGRect (x: 0, y: 0, width: bounds.width, height: 2) | ||
case .blinkBlock, .steadyBlock: | ||
region = bounds | ||
case .blinkUnderline, .steadyUnderline: | ||
region = CGRect (x: 0, y: 0, width: bounds.width, height: 2) | ||
} | ||
context.fill([region]) | ||
|
||
let lineDescent = CTFontGetDescent(terminal.fontSet.normal) | ||
let lineLeading = CTFontGetLeading(terminal.fontSet.normal) | ||
let yOffset = ceil(lineDescent+lineLeading) | ||
|
||
guard style == .steadyBlock || style == .blinkBlock else { | ||
return | ||
} | ||
let caretFG = caretTextColor ?? terminal.nativeForegroundColor | ||
context.setFillColor(TTColor.black.cgColor) | ||
for run in CTLineGetGlyphRuns(ctline) as? [CTRun] ?? [] { | ||
let runGlyphsCount = CTRunGetGlyphCount(run) | ||
let runAttributes = CTRunGetAttributes(run) as? [NSAttributedString.Key: Any] ?? [:] | ||
let runFont = runAttributes[.font] as! TTFont | ||
|
||
let runGlyphs = [CGGlyph](unsafeUninitializedCapacity: runGlyphsCount) { (bufferPointer, count) in | ||
CTRunGetGlyphs(run, CFRange(), bufferPointer.baseAddress!) | ||
count = runGlyphsCount | ||
} | ||
|
||
var positions = runGlyphs.enumerated().map { (i: Int, glyph: CGGlyph) -> CGPoint in | ||
CGPoint(x: 0, y: yOffset) | ||
} | ||
CTFontDrawGlyphs(runFont, runGlyphs, &positions, positions.count, context) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.