1 /* vi:set ts=8 sts=4 sw=4 ft=objc:
3 * VIM - Vi IMproved by Bram Moolenaar
4 * MacVim GUI port by Bjorn Winckler
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
11 #import "MMTypesetter.h"
12 #import "MMTextStorage.h"
16 // The 'linerange' functions count U+2028 and U+2029 as line end characters,
17 // which causes rendering to be screwed up because Vim does not count them as
18 // line end characters.
19 #define MM_USE_LINERANGE 0
23 @interface MMTypesetter (Private)
24 - (NSCharacterSet *)hiddenCharSet;
30 @implementation MMTypesetter
33 // Layout glyphs so that each line fragment has a fixed size.
35 // It is assumed that the font for each character has been chosen so that every
36 // glyph has the right advancement (either 2*cellSize.width or half that,
37 // depending on whether it is a wide character or not). This is taken care of
38 // by MMTextStorage in setAttributes:range: and in setFont:. All that is left
39 // for the typesetter to do is to make sure each line fragment has the same
40 // height and that unwanted glyphs are hidden.
42 - (void)layoutGlyphsInLayoutManager:(NSLayoutManager *)lm
43 startingAtGlyphIndex:(unsigned)startGlyphIdx
44 maxNumberOfLineFragments:(unsigned)maxNumLines
45 nextGlyphIndex:(unsigned *)nextGlyph
47 // TODO: Check that it really is an MMTextStorage.
48 MMTextStorage *ts = (MMTextStorage*)[lm textStorage];
49 NSTextView *tv = [lm firstTextView];
50 NSTextContainer *tc = [tv textContainer];
51 NSFont *font = [ts font];
52 NSString *text = [ts string];
53 unsigned textLen = [text length];
54 NSSize cellSize = [ts cellSize];
55 float baseline = [font descender];
57 if (!(ts && tv && tc && font && text && textLen))
60 float baselineOffset = [[NSUserDefaults standardUserDefaults]
61 floatForKey:MMBaselineOffsetKey];
63 baseline += baselineOffset;
65 unsigned startCharIdx = [lm characterIndexForGlyphAtIndex:startGlyphIdx];
66 unsigned i, numberOfLines = 0, firstLine = 0;
67 NSRange firstLineRange = { 0, 0 };
70 // Find the first line and its range, and count the number of lines. (This
71 // info could also be gleaned from MMTextStorage, but we do it here anyway
72 // to make absolutely sure everything is right.)
73 for (i = 0; i < textLen; numberOfLines++) {
74 NSRange lineRange = [text lineRangeForRange:NSMakeRange(i, 0)];
75 if (NSLocationInRange(startCharIdx, lineRange)) {
76 firstLine = numberOfLines;
77 firstLineRange = lineRange;
80 i = NSMaxRange(lineRange);
83 unsigned stride = 1 + [ts actualColumns];
84 numberOfLines = [ts actualRows];
85 firstLine = (unsigned)(startCharIdx/stride);
86 firstLineRange.location = firstLine * stride;
87 unsigned len = [text length] - firstLineRange.location;
88 firstLineRange.length = len < stride ? len : stride;
91 // Perform line fragment generation one line at a time.
92 NSRange lineRange = firstLineRange;
93 unsigned endGlyphIdx = startGlyphIdx;
94 for (i = 0; i < maxNumLines && lineRange.length; ++i) {
95 NSRange glyphRange = [lm glyphRangeForCharacterRange:lineRange
96 actualCharacterRange:nil];
97 NSRect lineRect = { 0, (firstLine+i)*cellSize.height,
98 cellSize.width*(lineRange.length-1), cellSize.height };
99 unsigned endLineIdx = NSMaxRange(lineRange);
100 NSPoint glyphPt = { 0, cellSize.height+baseline };
103 endGlyphIdx = NSMaxRange(glyphRange);
105 [lm setTextContainer:tc forGlyphRange:glyphRange];
106 [lm setLineFragmentRect:lineRect forGlyphRange:glyphRange
108 [lm setLocation:glyphPt forStartOfGlyphRange:glyphRange];
110 // Hide end-of-line and non-zero space characters (there is one after
111 // every wide character).
112 for (j = lineRange.location; j < endLineIdx; ++j) {
113 unichar ch = [text characterAtIndex:j];
114 if (ch == 0x200b || ch == '\n') {
115 NSRange range = { j, 1 };
116 range = [lm glyphRangeForCharacterRange:range
117 actualCharacterRange:nil];
118 [lm setNotShownAttribute:YES forGlyphAtIndex:range.location];
123 lineRange = [text lineRangeForRange:NSMakeRange(endLineIdx, 0)];
125 lineRange.location = endLineIdx;
126 len = [text length] - lineRange.location;
127 if (len < lineRange.length)
128 lineRange.length = len;
133 *nextGlyph = endGlyphIdx;
142 @implementation MMTypesetter (Private)
144 - (NSCharacterSet *)hiddenCharSet
146 static NSCharacterSet *hiddenCharSet = nil;
148 if (!hiddenCharSet) {
149 NSString *string = [NSString stringWithFormat:@"%C\n", 0x200b];
150 hiddenCharSet = [NSCharacterSet
151 characterSetWithCharactersInString:string];
152 [hiddenCharSet retain];
155 return hiddenCharSet;
158 @end // MMTypesetter (Private)