Skip to content

Commit

Permalink
draw character for space with font defined in settings (fixes #13); r…
Browse files Browse the repository at this point in the history
…eplace invisible character for tab
  • Loading branch information
Andreas Bentele committed Dec 25, 2016
1 parent cedd672 commit 7304527
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
5 changes: 5 additions & 0 deletions Classes/FRALayoutManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@
@interface FRALayoutManager : NSLayoutManager {

NSDictionary *attributes;
NSString *spaceCharacter;
NSString *tabCharacter;
NSString *newLineCharacter;

bool showsInvisibleChars;
}

@property bool showsInvisibleChars;

@end
25 changes: 19 additions & 6 deletions Classes/FRALayoutManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@ Maintainer before macOS Sierra (2010-2016):

@implementation FRALayoutManager

@synthesize showsInvisibleChars;

- (id)init
{
if (self = [super init]) {

attributes = @{NSFontAttributeName: [NSUnarchiver unarchiveObjectWithData:[FRADefaults valueForKey:@"TextFont"]], NSForegroundColorAttributeName: [NSUnarchiver unarchiveObjectWithData:[FRADefaults valueForKey:@"InvisibleCharactersColourWell"]]};
unichar tabUnichar = 0x00AC;
unichar spaceUnichar = 0x02FD;
spaceCharacter = [[NSString alloc] initWithCharacters:&spaceUnichar length:1];
unichar tabUnichar = 0x2192;
tabCharacter = [[NSString alloc] initWithCharacters:&tabUnichar length:1];
unichar newLineUnichar = 0x00B6;
newLineCharacter = [[NSString alloc] initWithCharacters:&newLineUnichar length:1];

self.showsInvisibleCharacters = [[FRADefaults valueForKey:@"ShowInvisibleCharacters"] boolValue];
self.showsInvisibleChars = [[FRADefaults valueForKey:@"ShowInvisibleCharacters"] boolValue];
[self setAllowsNonContiguousLayout:YES]; // Setting this to YES sometimes causes "an extra toolbar" and other graphical glitches to sometimes appear in the text view when one sets a temporary attribute, reported as ID #5832329 to Apple

NSUserDefaultsController *defaultsController = [NSUserDefaultsController sharedUserDefaultsController];
Expand All @@ -53,15 +57,17 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N

- (void)drawGlyphsForGlyphRange:(NSRange)glyphRange atPoint:(NSPoint)containerOrigin
{
if (self.showsInvisibleCharacters) {
if (self.showsInvisibleChars) {
NSString *completeString = [[self textStorage] string];
NSInteger lengthToRedraw = NSMaxRange(glyphRange);

for (NSInteger index = glyphRange.location; index < lengthToRedraw; index++) {
unichar characterToCheck = [completeString characterAtIndex:index];

NSString *character = nil;
if (characterToCheck == '\t') {
if (characterToCheck == ' ') {
character = spaceCharacter;
} else if (characterToCheck == '\t') {
character = tabCharacter;
} else if (characterToCheck == '\n' || characterToCheck == '\r') {
character = newLineCharacter;
Expand All @@ -70,13 +76,20 @@ - (void)drawGlyphsForGlyphRange:(NSRange)glyphRange atPoint:(NSPoint)containerOr
if (character != nil) {
NSPoint pointToDrawAt = [self locationForGlyphAtIndex:index];
NSRect glyphFragment = [self lineFragmentRectForGlyphAtIndex:index effectiveRange:NULL];

pointToDrawAt.x += glyphFragment.origin.x;
pointToDrawAt.y = glyphFragment.origin.y;

CGFloat yOffset = 0;
if (characterToCheck == '\t') {
yOffset = glyphFragment.size.height / 4;
}
pointToDrawAt.y = glyphFragment.origin.y - yOffset;

[character drawAtPoint:pointToDrawAt withAttributes:attributes];
}
}
}

[super drawGlyphsForGlyphRange:glyphRange atPoint:containerOrigin];
}

Expand Down

0 comments on commit 7304527

Please sign in to comment.