// // DTCoreTextLayoutLine.h // WeRead // // Reverse-engineered from binary analysis // Based on DTCoreText open-source library, customized by WeRead // // DTCoreText is an open-source library for rendering HTML with CoreText. // This class represents a single line of laid-out text. // #import #import @class DTCoreTextGlyphRun; @class DTCoreTextLayoutFrame; NS_ASSUME_NONNULL_BEGIN /** * DTCoreTextLayoutLine - Represents a single line of laid-out text. * * This class wraps CTLine and provides high-level access to the * layout of a single line. It contains an array of DTCoreTextGlyphRun objects * and provides methods for accessing typographic metrics and hit testing. * * Key features: * - Wraps CTLine for high-level access * - Contains array of DTCoreTextGlyphRun objects * - Provides typographic metrics (ascent, descent, leading, width) * - Supports hit testing within the line * - Handles baseline offsets for superscript/subscript */ @interface DTCoreTextLayoutLine : NSObject #pragma mark - Properties /** The CTLine reference */ @property (nonatomic, assign, readonly) CTLineRef ctLine; /** The origin point of this line in the frame */ @property (nonatomic, assign) CGPoint origin; /** The range of characters in this line */ @property (nonatomic, assign, readonly) NSRange stringRange; /** The index of this line in the frame */ @property (nonatomic, assign, readonly) NSUInteger lineIndex; /** Ascent of the line */ @property (nonatomic, assign, readonly) CGFloat ascent; /** Descent of the line */ @property (nonatomic, assign, readonly) CGFloat descent; /** Leading of the line */ @property (nonatomic, assign, readonly) CGFloat leading; /** Total height of the line (ascent + descent + leading) */ @property (nonatomic, assign, readonly) CGFloat height; /** Width of the line */ @property (nonatomic, assign, readonly) CGFloat width; /** Trailing whitespace width */ @property (nonatomic, assign, readonly) CGFloat trailingWhitespaceWidth; /** Array of glyph runs in this line */ @property (nonatomic, strong, readonly) NSArray *glyphRuns; /** The baseline offset (for superscript/subscript) */ @property (nonatomic, assign) CGFloat baselineOffset; /** Whether this line is the last line in a paragraph */ @property (nonatomic, assign) BOOL isLastLineInParagraph; /** Whether this line is a line break */ @property (nonatomic, assign) BOOL isLineBreak; /** The paragraph style for this line */ @property (nonatomic, strong, nullable) NSDictionary *paragraphStyle; /** Additional metrics for the line */ @property (nonatomic, strong, nullable) NSDictionary *metrics; #pragma mark - Initialization /** * Initialize with a CTLine and origin. * * @param ctLine The CoreText line * @param origin The origin point * @param range The string range * @param index The line index * @return Initialized layout line */ - (instancetype)initWithCTLine:(CTLineRef)ctLine origin:(CGPoint)origin range:(NSRange)range index:(NSUInteger)index; #pragma mark - Glyph Run Access /** * Returns the array of glyph runs. * * @return Array of DTCoreTextGlyphRun objects */ - (NSArray *)glyphRuns; /** * Returns the glyph run at the specified index. * * @param index The glyph run index * @return The glyph run at the index, or nil */ - (nullable DTCoreTextGlyphRun *)glyphRunAtIndex:(NSUInteger)index; /** * Returns the glyph run containing the given string index. * * @param index The string index * @return The glyph run containing the index, or nil */ - (nullable DTCoreTextGlyphRun *)glyphRunContainingStringIndex:(NSUInteger)index; #pragma mark - Typographic Metrics /** * Returns the typographic bounds of the line. * * @param ascent Output parameter for ascent * @param descent Output parameter for descent * @param leading Output parameter for leading * @return The width of the line */ - (CGFloat)getTypographicBoundsAscent:(CGFloat *)ascent descent:(CGFloat *)descent leading:(CGFloat *)leading; /** * Returns the bounds of the line (bounding box). */ - (CGRect)bounds; /** * Returns the frame of the line (position + bounds). */ - (CGRect)frame; #pragma mark - Hit Testing /** * Returns the string index at the given point within this line. * * @param point The point to test (in the line's coordinate system) * @return The string index, or NSNotFound */ - (NSUInteger)stringIndexAtPoint:(CGPoint)point; /** * Returns the offset for a given string index. * * @param index The string index * @return The horizontal offset */ - (CGFloat)offsetForStringIndex:(NSUInteger)index; /** * Returns the rect for a given string index. * * @param index The string index * @return The rect for the character */ - (CGRect)rectForStringIndex:(NSUInteger)index; #pragma mark - String Operations /** * Returns the substring represented by this line. */ - (nullable NSString *)substringFromAttributedString:(NSAttributedString *)attributedString; /** * Returns the attributes at a given string index. */ - (nullable NSDictionary *)attributesAtIndex:(NSUInteger)index fromAttributedString:(NSAttributedString *)attributedString; #pragma mark - Line Comparison /** * Compares this line to another line for ordering. */ - (NSComparisonResult)compareToLine:(DTCoreTextLayoutLine *)otherLine; /** * Returns whether this line contains the given string index. */ - (BOOL)containsStringIndex:(NSUInteger)index; /** * Returns whether this line intersects with the given range. */ - (BOOL)intersectsRange:(NSRange)range; #pragma mark - Drawing /** * Draws this line into a CGContext. * * @param context The CGContext to draw into */ - (void)drawInContext:(CGContextRef)context; /** * Draws this line with a specific color. * * @param context The CGContext to draw into * @param color The text color */ - (void)drawInContext:(CGContextRef)context withColor:(UIColor *)color; @end NS_ASSUME_NONNULL_END