Why must [NSLayoutManager lineFragmentRectForGlyphAtIndex:effectiveRange:] be computed twice?

  • Hello all,

    I have a Cocoa-made text editor involving an extension of the
    "MultiplePageView"
    class from the TextEdit source code. One of its functionalities is the
    ability
    to transform a rectangular region of a page (selected by the user) into
    several columns (each corresponding to a new NSTextView object).

      To compute the coordinates of this rectangular region, I use
    the -selectedRange method of NSTextView and the
    -lineFragmentRectForGlyphAtIndex: method of NSLayoutManager
    (basically, the rect I want to compute is the union of the
    lineFragmentRect of all the glyphs in the selected range).

    The surprising thing is, to make my code work I have
    to execute
    [myLayoutManager lineFragmentRectForGlyphAtIndex:myGlyphIndex
    effectiveRange:NULL] twice in order to get the correct result. The first call
    to this method yields a wrong value for one of the coordinates of the
    rectangle :
    (gdb) p lm
    $9 = (class NSLayoutManager *) 0x4641620
    (gdb) p (NSRect) [lm lineFragmentRectForGlyphAtIndex:42025]
    $10 = {
      origin = {
        x = 67,
        y = 509
      },
      size = {
        width = 461,
        height = 23
      }
    }
    (gdb) p (NSRect) [lm lineFragmentRectForGlyphAtIndex:42025]
    $11 = {
      origin = {
        x = 67,
        y = 65728
      },
      size = {
        width = 461,
        height = 23
      }
    }

    The corresponding part of my code is as follows :

    @implementation NSLayoutManager (LayoutManagerAdditions)

    -(NSRect)lineFragmentRectForGlyphAtIndex:(unsigned)aGlyphIndex
    {

      unsigned theContainerIndex=[self
    textContainerPositiveIndexForGlyphAtIndex:aGlyphIndex];
      NSTextContainer* theContainer=[[self textContainers]
    objectAtPositiveIndex:theContainerIndex];
      NSTextView* theTextView=[theContainer textView];
      NSRect theRect=[self lineFragmentRectForGlyphAtIndex:aGlyphIndex
    effectiveRange:NULL];
      theRect=[theTextView convertRect:theRect toView:[theTextView
    superview]]; return theRect;
    }

    (...)

    @end

      Can anyone explain to me what's going on here ? Should i use another
    method of
    NSLayoutManager to compute this selected rectangle's coordinates ?

                                                                                            TIA,

                                                                                                Ewan
  • On Dec 27, 2007, at 6:09 AM, Ewan Delanoy wrote:

    >
    > The surprising thing is, to make my code work I have
    > to execute
    > [myLayoutManager lineFragmentRectForGlyphAtIndex:myGlyphIndex
    > effectiveRange:NULL] twice in order to get the correct result.

    Is this on Leopard? In a complex application have a problem which I
    suspect might be related: Opening a document from within
    applicationDidFinishLaunching: causes some text paragraphs to be drawn
    incorrectly. If I use a timer to have the document opened just a hair
    later, it draws fine. This was not a problem in Tiger.

    I spent many hours to no avail trying to narrow it down to either fix
    it or at least be able to file a useful bug report.

    Gerd
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today