Merge pull request #3339 from enquora/integration-test-document-based-app

Merge TextEditor fixes: superscript, subscript and baseline management

* AppKit: CPTextView implements Cocoa's superscript:, subscript:, unscript:,
  raiseBaseline: and lowerBaseline: actions, with undo and menu validation.

* TextEditor: The work list documents text layout and records the Justify
  crash, kerning and ligature settings, and font panel as open items.
This commit is contained in:
David Richardson authored and GitHub committed 2026-10-24 09:46:52 -06:00
commit f01d83204e
3 files changed
+228 -1

No files matched your search

+163 -1
View File
@@ -721,7 +721,8 @@ var kDelegateRespondsTo_textShouldBeginEditing
For \c -toggleRuler:, the item's title becomes "Hide Ruler" or "Show Ruler" to reflect the
ruler's visibility, and the item is enabled only when the text view uses a ruler and lies
in a scroll view. \c -copyRuler: is enabled in rich text, and \c -pasteRuler: in editable
rich text when the ruler pasteboard holds a ruler. Every other item is enabled.
rich text when the ruler pasteboard holds a ruler. The superscript, subscript and baseline
actions are enabled in editable rich text. Every other item is enabled.
@param anItem the item to validate
@return \c YES if the item should be enabled
*/
@@ -735,6 +736,11 @@ var kDelegateRespondsTo_textShouldBeginEditing
if (action === @selector(redo:))
return [[[self window] undoManager] canRedo];
// Baseline changes apply only to editable rich text.
if (action === @selector(superscript:) || action === @selector(subscript:) || action === @selector(unscript:) ||
action === @selector(raiseBaseline:) || action === @selector(lowerBaseline:))
return [self isEditable] && [self isRichText];
// As in Cocoa, rulers can be copied and pasted only in rich text.
if (action === @selector(copyRuler:))
return [self isRichText];
@@ -2074,6 +2080,162 @@ Sets the selection to a range of characters in response to user action.
[[CPFontManager sharedFontManager] addFontTrait:CPItalicFontMask];
}
// MARK: -
// MARK: Baseline methods
/*!
Raises the superscript level of the selected text by one, or of the text typed next when
nothing is selected. Each run of the selection keeps its own level, raised by one.
@param aSender the object requesting this
*/
- (@action)superscript:(id)aSender
{
[self _changeCharacterAttribute:CPSuperscriptAttributeName byAmount:1];
}
/*!
Lowers the superscript level of the selected text by one, or of the text typed next when
nothing is selected. A negative level is a subscript.
@param aSender the object requesting this
*/
- (@action)subscript:(id)aSender
{
[self _changeCharacterAttribute:CPSuperscriptAttributeName byAmount:-1];
}
/*!
Removes the superscript level and the baseline offset of the selected text, or of the text
typed next when nothing is selected.
@param aSender the object requesting this
*/
- (@action)unscript:(id)aSender
{
[self _removeCharacterAttributes:[CPSuperscriptAttributeName, CPBaselineOffsetAttributeName]];
}
/*!
Raises the baseline of the selected text by one point, or of the text typed next when
nothing is selected.
@param aSender the object requesting this
*/
- (@action)raiseBaseline:(id)aSender
{
[self _changeCharacterAttribute:CPBaselineOffsetAttributeName byAmount:1.0];
}
/*!
Lowers the baseline of the selected text by one point, or of the text typed next when
nothing is selected.
@param aSender the object requesting this
*/
- (@action)lowerBaseline:(id)aSender
{
[self _changeCharacterAttribute:CPBaselineOffsetAttributeName byAmount:-1.0];
}
/* @ignore
Adds anAmount to a numeric character attribute, run by run across the selection, or in the
typing attributes when nothing is selected. A value that reaches zero removes the attribute.
*/
- (void)_changeCharacterAttribute:(CPString)anAttributeName byAmount:(float)anAmount
{
var range = CPMakeRangeCopy(_selectionRange);
if (!range.length)
{
var newValue = ([_typingAttributes objectForKey:anAttributeName] || 0) + anAmount;
if (newValue)
[_typingAttributes setObject:newValue forKey:anAttributeName];
else
[_typingAttributes removeObjectForKey:anAttributeName];
[[CPNotificationCenter defaultCenter] postNotificationName:CPTextViewDidChangeTypingAttributesNotification object:self];
return;
}
if (![self _beginCharacterAttributeChangeInRange:range])
return;
var location = range.location,
end = CPMaxRange(range);
while (location < end)
{
var effectiveRange = CPMakeRange(0, 0),
value = [_textStorage attribute:anAttributeName atIndex:location effectiveRange:effectiveRange] || 0,
runRange = CPIntersectionRange(effectiveRange, range),
newValue = value + anAmount;
if (!runRange.length)
runRange = CPMakeRange(location, end - location);
if (newValue)
[_textStorage addAttribute:anAttributeName value:newValue range:runRange];
else
[_textStorage removeAttribute:anAttributeName range:runRange];
location = CPMaxRange(runRange);
}
[self _endCharacterAttributeChangeInRange:range];
}
/* @ignore
Removes character attributes from the selection, or from the typing attributes when nothing
is selected.
*/
- (void)_removeCharacterAttributes:(CPArray)attributeNames
{
var range = CPMakeRangeCopy(_selectionRange);
if (!range.length)
{
for (var i = 0; i < attributeNames.length; i++)
[_typingAttributes removeObjectForKey:attributeNames[i]];
[[CPNotificationCenter defaultCenter] postNotificationName:CPTextViewDidChangeTypingAttributesNotification object:self];
return;
}
if (![self _beginCharacterAttributeChangeInRange:range])
return;
for (var i = 0; i < attributeNames.length; i++)
[_textStorage removeAttribute:attributeNames[i] range:CPMakeRangeCopy(range)];
[self _endCharacterAttributeChangeInRange:range];
}
/* @ignore
Asks whether the change may happen, and registers undo for the attributes in aRange.
*/
- (BOOL)_beginCharacterAttributeChangeInRange:(CPRange)aRange
{
if (![self isEditable] || ![self isRichText] || ![self shouldChangeTextInRange:aRange replacementString:nil])
return NO;
// Undo restores the text's previous attributes, and registers the reverse for redo.
[[[self _registeringUndoManager] prepareWithInvocationTarget:self]
_replaceCharactersInRange:CPMakeRangeCopy(aRange)
withAttributedString:[_textStorage attributedSubstringFromRange:CPMakeRangeCopy(aRange)]
selectionRange:CPMakeRangeCopy(_selectionRange)];
return YES;
}
/* @ignore
Lays the changed text out again and shows it.
*/
- (void)_endCharacterAttributeChangeInRange:(CPRange)aRange
{
[_layoutManager textStorage:_textStorage edited:0 range:CPMakeRangeCopy(aRange) changeInLength:0 invalidatedRange:CPMakeRangeCopy(aRange)];
[_layoutManager _validateLayoutAndGlyphs];
[self sizeToFit];
[self setNeedsDisplay:YES];
[self didChangeText];
}
- (void)alignLeft:(id)sender
{
[self _setAlignment:CPLeftTextAlignment];
+55
View File
@@ -167,6 +167,61 @@
[self assertTrue:[undoManager canUndo] message:@"a text view that allows undo should register its change"];
}
- (void)testSuperscriptSubscriptAndUnscript
{
[textView setRichText:YES];
[self assertTrue:[theWindow makeFirstResponder:textView]];
[textView setSelectedRange:CPMakeRange(0, 2)];
[textView superscript:nil];
[self assert:1 equals:[[textView textStorage] attribute:CPSuperscriptAttributeName atIndex:0 effectiveRange:nil]];
[textView superscript:nil];
[self assert:2 equals:[[textView textStorage] attribute:CPSuperscriptAttributeName atIndex:1 effectiveRange:nil]];
[textView subscript:nil];
[textView subscript:nil];
[self assertTrue:([[textView textStorage] attribute:CPSuperscriptAttributeName atIndex:0 effectiveRange:nil] == nil) message:@"a level of zero removes the attribute"];
[textView subscript:nil];
[textView raiseBaseline:nil];
[self assert:-1 equals:[[textView textStorage] attribute:CPSuperscriptAttributeName atIndex:0 effectiveRange:nil]];
[self assert:1 equals:[[textView textStorage] attribute:CPBaselineOffsetAttributeName atIndex:0 effectiveRange:nil]];
[textView unscript:nil];
[self assertTrue:([[textView textStorage] attribute:CPSuperscriptAttributeName atIndex:0 effectiveRange:nil] == nil)];
[self assertTrue:([[textView textStorage] attribute:CPBaselineOffsetAttributeName atIndex:0 effectiveRange:nil] == nil)];
[self assertTrue:([[textView textStorage] attribute:CPSuperscriptAttributeName atIndex:5 effectiveRange:nil] == nil) message:@"text outside the selection is unchanged"];
}
- (void)testSuperscriptWithoutSelectionChangesTypingAttributes
{
[textView setRichText:YES];
[self assertTrue:[theWindow makeFirstResponder:textView]];
[textView setSelectedRange:CPMakeRange(3, 0)];
[textView superscript:nil];
[self assert:1 equals:[[textView typingAttributes] objectForKey:CPSuperscriptAttributeName]];
[textView unscript:nil];
[self assertTrue:([[textView typingAttributes] objectForKey:CPSuperscriptAttributeName] == nil)];
}
- (void)testBaselineActionsAreEnabledOnlyInEditableRichText
{
var item = [[CPMenuItem alloc] initWithTitle:@"Superscript" action:@selector(superscript:) keyEquivalent:@""];
[textView setRichText:NO];
[self assertFalse:[textView validateUserInterfaceItem:item]];
[textView setRichText:YES];
[self assertTrue:[textView validateUserInterfaceItem:item]];
[textView setEditable:NO];
[self assertFalse:[textView validateUserInterfaceItem:item]];
}
- (void)testUndoItemIsEnabledOnlyWhenThereIsAChangeToUndo
{
var undoManager = [theWindow undoManager],
+10
View File
@@ -26,7 +26,12 @@ TextEditor is a document-based test application and a tutorial. When the app use
### Text view
How the text system works, for the entries below: `CPTextView` does not use CSS text layout. `CPSimpleTypesetter` measures text with the canvas `measureText` (`sizeWithFont:inWidth:` in `CPStringDrawing.j`), measuring growing substrings so that each character's advancement includes the browser's kerning. It then breaks lines, places tabs, and applies indents and alignment itself (`CPTypesetter.j`). `_CPLineFragment` (`CPLayoutManager.j`) draws each run of identical attributes as one absolutely positioned `<span>`, at the computed position, with `white-space: pre` and the run's font, colour and underline in CSS. Inside a span the browser shapes the glyphs, including its own kerning and ligatures.
- **Format > Font > Copy Style and Paste Style raise an exception.** They send `copyFont:` and `pasteFont:`, which `CPText` declares as abstract methods that raise (`CPText.j:210` and `:261`). `CPTextView` does not implement them, so the menu items are enabled and choosing one raises. `CPTextView` needs Cocoa's implementations: copy the font and character attributes at the selection to the font pasteboard (`CPFontPboard`), and apply them to the selection.
- **Format > Text > Justify crashes the application.** `alignJustified:` sets the paragraph style's alignment to `CPJustifiedTextAlignment`, but the typesetter's alignment handling has cases only for left, centred and right text (`_flushRange:` in `CPTypesetter.j`). The exception text from the browser console will show where the crash occurs. Justified layout needs a full implementation: spreading each line's spare width across its spaces, except on the last line of a paragraph.
- **Kerning and ligature settings are not implemented.** Format > Font > Kern sends `useStandardKerning:`, `turnOffKerning:`, `tightenKerning:` and `loosenKerning:`, and Format > Font > Ligatures sends `useStandardLigatures:`, `turnOffLigatures:` and `useAllLigatures:`. `CPTextView` implements none of them, so the items are disabled. `CPText.j` defines `CPKernAttributeName` and `CPLigatureAttributeName`, but only the RTF producer reads them. A complete implementation needs: the seven actions, changing the attributes over the selection or the typing attributes with undo, as the baseline actions do; the typesetter adding a run's `CPKernAttributeName` value to each character's advancement, and measuring with the same ligature setting it draws with; and `_CPLineFragment` setting CSS `letter-spacing` and `font-variant-ligatures` on each run's span to match.
- **The font panel needs polish.** Found during the Format menu review on 24 October: the panel works with the text view, but its appearance and behaviour need refinement. The specific problems are not yet recorded; list them here, compared with the native font panel, when the panel is reviewed. `CPFontPanel` is in `AppKit/CPTextView/CPFontPanel.j`.
### Ruler
@@ -45,6 +50,10 @@ TextEditor is a document-based test application and a tutorial. When the app use
- **Key equivalents on platforms other than macOS.** Filed as an issue.
## Opportunities for improvement
- **Line breaking is greedy.** `CPSimpleTypesetter` fills each line as far as it fits, as Cocoa does. Typesetting quality, especially in justified text, needs paragraph-wide line breaking in the manner of Knuth-Plass, as Typst also uses, with language-aware hyphenation. Cappuccino measures and lays out text itself, so this belongs in the typesetter, as its only behaviour, with no setting for users or developers. It follows justification.
## Planned additions to the app
- Fonts through `CPFontManager` and `CPFontPanel`.
@@ -73,4 +82,5 @@ TextEditor is a document-based test application and a tutorial. When the app use
- Text stays inside the visible width, with Cocoa's 5-point line fragment padding.
- Tab characters: the Tab key inserts a tab in the text view, and left, centre, right and decimal tab stops align their columns.
- Format > Text > Copy Ruler and Paste Ruler apply one paragraph's indents and tab stops to others.
- Format > Font > Baseline: superscript, subscript, raised and lowered baselines lay out correctly, and Use Default restores the text, for an exact or a wider selection.
- Builds with both the legacy toolchain and capp-build.