Added a comment
[MacVim/jjgod.git] / MMTypesetter.m
blobd551f447c9853fb26ec098bbaa1e85df65124005
1 /* vi:set ts=8 sts=4 sw=4 ft=objc:
2  *
3  * VIM - Vi IMproved            by Bram Moolenaar
4  *                              MacVim GUI port by Bjorn Winckler
5  *
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.
9  */
11 #import "MMTypesetter.h"
12 #import "MMTextStorage.h"
13 #import "MacVim.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
22 #if 0
23 @interface MMTypesetter (Private)
24 - (NSCharacterSet *)hiddenCharSet;
25 @end
26 #endif
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))
58         return;
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 };
69 #if MM_USE_LINERANGE
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;
78         }
80         i = NSMaxRange(lineRange);
81     }
82 #else
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;
89 #endif
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 };
101         unsigned j;
103         endGlyphIdx = NSMaxRange(glyphRange);
105         [lm setTextContainer:tc forGlyphRange:glyphRange];
106         [lm setLineFragmentRect:lineRect forGlyphRange:glyphRange
107                        usedRect:lineRect];
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];
119             }
120         }
122 #if MM_USE_LINERANGE
123         lineRange = [text lineRangeForRange:NSMakeRange(endLineIdx, 0)];
124 #else
125         lineRange.location = endLineIdx;
126         len = [text length] - lineRange.location;
127         if (len < lineRange.length)
128             lineRange.length = len;
129 #endif
130     }
132     if (nextGlyph)
133         *nextGlyph = endGlyphIdx;
136 @end // MMTypesetter
141 #if 0
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];
153     }
155     return hiddenCharSet;
158 @end // MMTypesetter (Private)
159 #endif