AppKit: Apply line fragment padding in text layout

CPTextContainer's line fragment padding was 0 and the typesetter never applied it, so text started 2 points from the text view's edge and ruler markers at 0 were cut off. It now follows Cocoa, with a default padding of 5 points at each end of every line fragment.

* CPTextView/CPTextContainer.j: Default the line fragment padding to 5 points, and archive it.

* CPTextView/CPTypesetter.j: Lay lines out inside the padding: offset line fragment origins by it, compute the right margin, right indent, centred and right alignment within the width inside it, and subtract it when layout resumes from a stored line fragment.

* CPTextView/CPTextView.j: Start the caret in empty text inside the padding.
This commit is contained in:
David Richardson committed 2026-10-23 19:33:16 -06:00
1 parent b4bff7ffe1
commit 7cd6295359
3 files changed
+30 -11

No files matched your search

+9 -2
View File
@@ -112,7 +112,8 @@ CPLineMovesUp = 4;
- (void)_init
{
_lineFragmentPadding = 0.0;
// Cocoa's default: room at each end of a line fragment, so that text does not touch the container's edges.
_lineFragmentPadding = 5.0;
_layoutManager = [[CPLayoutManager alloc] init];
[_layoutManager addTextContainer:self];
@@ -274,7 +275,8 @@ CPLineMovesUp = 4;
var CPTextContainerSizeKey = @"CPTextContainerSizeKey",
CPTextContainerLayoutManagerKey = @"CPTextContainerLayoutManagerKey",
CPTextContainerWidthTracksTextViewKey = @"CPTextContainerWidthTracksTextViewKey",
CPTextContainerHeightTracksTextViewKey = @"CPTextContainerHeightTracksTextViewKey";
CPTextContainerHeightTracksTextViewKey = @"CPTextContainerHeightTracksTextViewKey",
CPTextContainerLineFragmentPaddingKey = @"CPTextContainerLineFragmentPaddingKey";
@implementation CPTextContainer (CPCoding)
@@ -293,6 +295,10 @@ var CPTextContainerSizeKey = @"CPTextContainerSizeKey",
_widthTracksTextView = [aCoder decodeBoolForKey:CPTextContainerWidthTracksTextViewKey];
_heightTracksTextView = [aCoder decodeBoolForKey:CPTextContainerHeightTracksTextViewKey];
// Archives made before the padding was archived keep the default from _init.
if ([aCoder containsValueForKey:CPTextContainerLineFragmentPaddingKey])
_lineFragmentPadding = [aCoder decodeFloatForKey:CPTextContainerLineFragmentPaddingKey];
}
return self;
@@ -304,6 +310,7 @@ var CPTextContainerSizeKey = @"CPTextContainerSizeKey",
[aCoder encodeObject:_layoutManager forKey:CPTextContainerLayoutManagerKey];
[aCoder encodeBool:_widthTracksTextView forKey:CPTextContainerWidthTracksTextViewKey];
[aCoder encodeBool:_heightTracksTextView forKey:CPTextContainerHeightTracksTextViewKey];
[aCoder encodeFloat:_lineFragmentPadding forKey:CPTextContainerLineFragmentPaddingKey];
}
@end
+2 -1
View File
@@ -2386,7 +2386,8 @@ Sets the selection to a range of characters in response to user action.
if (!numberOfGlyphs)
{
var font = [_typingAttributes objectForKey:CPFontAttributeName] || [self font];
return CGRectMake(1, ([font ascender] - [font descender]) * 0.5 + _textContainerOrigin.y, 1, [font size]);
// Like the empty line after text, the caret starts inside the line fragment padding.
return CGRectMake(MAX(1.0, [_textContainer lineFragmentPadding]), ([font ascender] - [font descender]) * 0.5 + _textContainerOrigin.y, 1, [font size]);
}
// cursor is at a newline character -> jump to next line
+19 -8
View File
@@ -171,9 +171,12 @@ var CPSystemTypesetterFactory,
lineCount:(unsigned)lineCount
sameLine:(BOOL)sameLine
{
var myX = 0,
rect = CGRectMake(lineOrigin.x, lineOrigin.y, _lineWidth, _lineHeight),
containerSize = aContainer._size;
// Line positions are computed inside the line fragment padding. The padding is added here, where
// the line fragment rectangle is stored, and alignment uses the width inside the padding.
var padding = [aContainer lineFragmentPadding],
myX = 0,
rect = CGRectMake(lineOrigin.x + padding, lineOrigin.y, _lineWidth, _lineHeight),
usableWidth = aContainer._size.width - 2.0 * padding;
[_layoutManager _appendNewLineFragmentInTextContainer:_currentTextContainer forGlyphRange:lineRange];
@@ -190,11 +193,11 @@ var CPSystemTypesetterFactory,
break;
case CPCenterTextAlignment:
myX = (containerSize.width - _lineWidth) / 2;
myX = (usableWidth - _lineWidth) / 2;
break;
case CPRightTextAlignment:
myX = containerSize.width - _lineWidth;
myX = usableWidth - _lineWidth;
break;
}
@@ -237,8 +240,10 @@ var CPSystemTypesetterFactory,
_lineBase = 0;
_lineWidth = 0;
// containerSizeWidth is the width inside the line fragment padding at both ends.
var containerSize = [_currentTextContainer containerSize],
containerSizeWidth = containerSize.width,
lineFragmentPadding = [_currentTextContainer lineFragmentPadding],
containerSizeWidth = containerSize.width - 2.0 * lineFragmentPadding,
containerSizeHeight = containerSize.height,
lineRange = CPMakeRange(glyphIndex, 0),
wrapRange = CPMakeRange(0, 0),
@@ -274,7 +279,11 @@ var CPSystemTypesetterFactory,
rightMargin = containerSizeWidth;
if (glyphIndex > 0)
{
// Stored line fragment rectangles include the padding, which the layout below does not.
lineOrigin = CGPointCreateCopy([_layoutManager lineFragmentRectForGlyphAtIndex:glyphIndex effectiveRange:nil].origin);
lineOrigin.x -= lineFragmentPadding;
}
else if ([_layoutManager extraLineFragmentTextContainer])
lineOrigin = CGPointMake(0, [_layoutManager extraLineFragmentUsedRect].origin.y);
else
@@ -556,7 +565,8 @@ var CPSystemTypesetterFactory,
{
_currentTextContainer = textContainers[++_indexOfCurrentContainer];
containerSize = [_currentTextContainer containerSize];
containerSizeWidth = containerSize.width;
lineFragmentPadding = [_currentTextContainer lineFragmentPadding];
containerSizeWidth = containerSize.width - 2.0 * lineFragmentPadding;
containerSizeHeight = containerSize.height;
}
@@ -589,7 +599,8 @@ var CPSystemTypesetterFactory,
if (lineRange.length)
[self _flushRange:lineRange lineOrigin:lineOrigin currentContainer:_currentTextContainer advancements:advancements lineCount:numLines sameLine:NO];
var rect = CGRectMake(1, lineOrigin.y - descent, containerSizeWidth, currentFontLineHeight);
// The empty line after the text starts inside the padding, and at least 1 point in, so the insertion point stays visible.
var rect = CGRectMake(MAX(1.0, lineFragmentPadding), lineOrigin.y - descent, containerSizeWidth, currentFontLineHeight);
[_layoutManager setExtraLineFragmentRect:rect usedRect:rect textContainer:_currentTextContainer];
var fragment = [_layoutManager._lineFragments lastObject];