Revision: 12945 http://sourceforge.net/p/skim-app/code/12945 Author: hofman Date: 2022-06-25 17:46:00 +0000 (Sat, 25 Jun 2022) Log Message: ----------- custom method to get line index for a point on a page
Modified Paths: -------------- trunk/PDFPage_SKExtensions.h trunk/PDFPage_SKExtensions.m trunk/SKPDFView.m trunk/SKReadingBar.m Modified: trunk/PDFPage_SKExtensions.h =================================================================== --- trunk/PDFPage_SKExtensions.h 2022-06-25 17:27:35 UTC (rev 12944) +++ trunk/PDFPage_SKExtensions.h 2022-06-25 17:46:00 UTC (rev 12945) @@ -78,6 +78,7 @@ - (NSURL *)skimURL; - (NSPointerArray *)lineRects; +- (NSInteger)indexOfLineRectAtPoint:(NSPoint)point lower:(BOOL)lower; - (NSUInteger)pageIndex; - (NSString *)sequentialLabel; Modified: trunk/PDFPage_SKExtensions.m =================================================================== --- trunk/PDFPage_SKExtensions.m 2022-06-25 17:27:35 UTC (rev 12944) +++ trunk/PDFPage_SKExtensions.m 2022-06-25 17:46:00 UTC (rev 12945) @@ -443,6 +443,28 @@ return lines; } +- (NSInteger)indexOfLineRectAtPoint:(NSPoint)point lower:(BOOL)lower { + NSPointerArray *rectArray = [self lineRects]; + NSInteger i = 0, iMax = [rectArray count]; + + for (i = 0; i < iMax; i++) { + NSRect rect = [rectArray rectAtIndex:i]; + NSInteger pos; + switch ([self lineDirectionAngle]) { + case 0: pos = point.x > NSMaxX(rect) ? -1 : point.x > NSMinX(rect) ? 0 : 1; break; + case 90: pos = point.y > NSMaxY(rect) ? -1 : point.y > NSMinY(rect) ? 0 : 1; break; + case 180: pos = point.x < NSMinX(rect) ? -1 : point.x < NSMaxX(rect) ? 0 : 1; break; + case 270: pos = point.y < NSMinY(rect) ? -1 : point.y < NSMaxY(rect) ? 0 : 1; break; + default: pos = point.y < NSMinY(rect) ? -1 : point.y < NSMaxY(rect) ? 0 : 1; break; + } + if (pos != -1) { + if (pos == 1 && lower && i > 0) i--; + break; + } + } + return MIN(i, iMax - 1); +} + - (NSUInteger)pageIndex { return [[self document] indexForPage:self]; } Modified: trunk/SKPDFView.m =================================================================== --- trunk/SKPDFView.m 2022-06-25 17:27:35 UTC (rev 12944) +++ trunk/SKPDFView.m 2022-06-25 17:46:00 UTC (rev 12945) @@ -161,8 +161,6 @@ static inline PDFAreaOfInterest SKAreaOfInterestForResizeHandle(SKRectEdges mask, PDFPage *page); -static inline NSInteger SKIndexOfRectAtPointInOrderedRects(NSPoint point, NSPointerArray *rectArray, NSInteger lineAngle, BOOL lower); - static inline NSSize SKFitTextNoteSize(NSString *string, NSFont *font, CGFloat width); enum { @@ -4495,7 +4493,7 @@ - (void)doDragReadingBarWithEvent:(NSEvent *)theEvent { PDFPage *readingBarPage = [readingBar page]; PDFPage *page = readingBarPage; - NSPointerArray *lineRects = [page lineRects]; + NSInteger numberOfLines = [[page lineRects] count]; NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:page, SKPDFViewOldPageKey, nil]; NSInteger lineAngle = [page lineDirectionAngle]; @@ -4502,7 +4500,7 @@ NSEvent *lastMouseEvent = theEvent; NSPoint lastMouseLoc = [theEvent locationInView:self]; NSPoint point = [self convertPoint:lastMouseLoc toPage:page]; - NSInteger lineOffset = SKIndexOfRectAtPointInOrderedRects(point, lineRects, lineAngle, YES) - [readingBar currentLine]; + NSInteger lineOffset = [page indexOfLineRectAtPoint:point lower:YES] - [readingBar currentLine]; NSDate *lastPageChangeDate = [NSDate distantPast]; lastMouseLoc = [self convertPoint:lastMouseLoc toView:[self documentView]]; @@ -4550,16 +4548,15 @@ if ([mousePage isEqual:page] == NO) { page = mousePage; - lineRects = [page lineRects]; + numberOfLines = [[page lineRects] count]; lineAngle = [page lineDirectionAngle]; } - if ([lineRects count] == 0) + if (numberOfLines == 0) continue; - currentLine = SKIndexOfRectAtPointInOrderedRects(mouseLocInPage, lineRects, lineAngle, mouseLocInDocument.y < lastMouseLoc.y) - lineOffset; - currentLine = MIN((NSInteger)[lineRects count] - (NSInteger)[readingBar numberOfLines], currentLine); - currentLine = MAX(0, currentLine); + currentLine = [page indexOfLineRectAtPoint:mouseLocInPage lower:mouseLocInDocument.y < lastMouseLoc.y] - lineOffset; + currentLine = MAX(0, MIN(numberOfLines - (NSInteger)[readingBar numberOfLines], currentLine)); if ([page isEqual:readingBarPage] == NO || currentLine != [readingBar currentLine]) { NSRect newRect, oldRect = [readingBar currentBoundsForBox:[self displayBox]]; @@ -4594,10 +4591,8 @@ - (void)doResizeReadingBarWithEvent:(NSEvent *)theEvent { PDFPage *page = [readingBar page]; NSInteger firstLine = [readingBar currentLine]; - NSPointerArray *lineRects = [page lineRects]; NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:page, SKPDFViewOldPageKey, page, SKPDFViewNewPageKey, nil]; - NSInteger lineAngle = [page lineDirectionAngle]; - + [[NSCursor resizeUpDownCursor] push]; while (YES) { @@ -4611,7 +4606,7 @@ if ([[self pageAndPoint:&point forEvent:theEvent nearest:YES] isEqual:page] == NO) continue; - NSInteger numberOfLines = MAX(0, SKIndexOfRectAtPointInOrderedRects(point, lineRects, lineAngle, YES)) - firstLine + 1; + NSInteger numberOfLines = MAX(0, [page indexOfLineRectAtPoint:point lower:YES]) - firstLine + 1; if (numberOfLines > 0 && numberOfLines != (NSInteger)[readingBar numberOfLines]) { NSRect oldRect = [readingBar currentBoundsForBox:[self displayBox]]; @@ -5362,28 +5357,6 @@ return kPDFNoArea; } -static inline NSInteger SKIndexOfRectAtPointInOrderedRects(NSPoint point, NSPointerArray *rectArray, NSInteger lineAngle, BOOL lower) -{ - NSInteger i = 0, iMax = [rectArray count]; - - for (i = 0; i < iMax; i++) { - NSRect rect = [rectArray rectAtIndex:i]; - NSInteger pos; - switch (lineAngle) { - case 0: pos = point.x > NSMaxX(rect) ? -1 : point.x > NSMinX(rect) ? 0 : 1; break; - case 90: pos = point.y > NSMaxY(rect) ? -1 : point.y > NSMinY(rect) ? 0 : 1; break; - case 180: pos = point.x < NSMinX(rect) ? -1 : point.x < NSMaxX(rect) ? 0 : 1; break; - case 270: pos = point.y < NSMinY(rect) ? -1 : point.y < NSMaxY(rect) ? 0 : 1; break; - default: pos = point.y < NSMinY(rect) ? -1 : point.y < NSMaxY(rect) ? 0 : 1; break; - } - if (pos != -1) { - if (pos == 1 && lower && i > 0) i--; - break; - } - } - return MIN(i, iMax - 1); -} - static inline NSSize SKFitTextNoteSize(NSString *string, NSFont *font, CGFloat width) { NSMutableParagraphStyle *parStyle = [[NSMutableParagraphStyle alloc] init]; CGFloat descent = -[font descender]; Modified: trunk/SKReadingBar.m =================================================================== --- trunk/SKReadingBar.m 2022-06-25 17:27:35 UTC (rev 12944) +++ trunk/SKReadingBar.m 2022-06-25 17:46:00 UTC (rev 12945) @@ -232,16 +232,6 @@ return [self goToPreviousPageAtTop:YES]; } -static inline BOOL topAbovePoint(NSRect rect, NSPoint point, NSInteger lineAngle) { - switch (lineAngle) { - case 0: return NSMinX(rect) <= point.x; - case 90: return NSMinY(rect) <= point.y; - case 180: return NSMaxX(rect) >= point.x; - case 270: return NSMaxY(rect) >= point.y; - default: return NSMaxY(rect) >= point.x; - } -} - - (BOOL)goToLineForPoint:(NSPoint)point { if ([lineRects count] == 0) { if (currentLine == -1) @@ -248,11 +238,7 @@ return [self goToNextPageAtTop:YES]; return NO; } - NSInteger i = [self maxLine] + 1; - NSInteger lineAngle = [page lineDirectionAngle]; - while (--i >= 0) - if (topAbovePoint([lineRects rectAtIndex:i], point, lineAngle)) break; - currentLine = MAX(0, i); + currentLine = MAX(0, MAX([self maxLine], [page indexOfLineRectAtPoint:point lower:YES])); [self updateCurrentBounds]; return YES; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. _______________________________________________ Skim-app-commit mailing list Skim-app-commit@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/skim-app-commit