107 lines
3.4 KiB
Objective-C
107 lines
3.4 KiB
Objective-C
//
|
|
// DTHTMLAttributedStringBuilder.h
|
|
// DTCoreText (WeRead custom fork)
|
|
//
|
|
// Reverse-engineered from WeChat Reading (读书) binary.
|
|
// This class converts HTML DOM into NSAttributedString via SAX-style parsing.
|
|
//
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
@class DTHTMLParserDelegate;
|
|
@class DTHTMLElement;
|
|
@class DTCSSStylesheet;
|
|
@class DTCoreTextFontDescriptor;
|
|
@class DTCoreTextParagraphStyle;
|
|
@class WRBook;
|
|
@class WRChapter;
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// WeRead custom NSAttributedString attribute keys
|
|
// ---------------------------------------------------------------------------
|
|
// These keys are used in the resulting attributed string to carry page layout
|
|
// and presentation metadata that goes beyond standard DTCoreText attributes.
|
|
|
|
extern NSString *const DTPageBackgroundColorAttribute;
|
|
extern NSString *const DTPageBackgroundImageAttribute;
|
|
extern NSString *const DTPageBackgroundImagePathAttribute;
|
|
extern NSString *const DTPageBreakAfterAttribute;
|
|
extern NSString *const DTPageBreakBeforeAttribute;
|
|
extern NSString *const DTPageBreakInsideAvoidAttribute;
|
|
extern NSString *const DTPageRelateAttribute;
|
|
extern NSString *const DTPageSize;
|
|
extern NSString *const DTPageFlippingStyle;
|
|
extern NSString *const DTHTMLVerticalCenterAttribute;
|
|
extern NSString *const DTHTMLTranslateTagAttribute;
|
|
extern NSString *const DTHTMLTranslateNoStyleAttribute;
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// DTHTMLAttributedStringBuilder
|
|
// ---------------------------------------------------------------------------
|
|
|
|
@interface DTHTMLAttributedStringBuilder : NSObject
|
|
|
|
// --- Initializers ---
|
|
|
|
/**
|
|
Designated initializer.
|
|
@param htmlData Raw HTML data (UTF-8 encoded).
|
|
@param options Dictionary of build options (base URL, CSS stylesheet, etc.).
|
|
*/
|
|
- (instancetype)initWithHTML:(NSData *)htmlData
|
|
options:(NSDictionary *)options;
|
|
|
|
/**
|
|
Convenience initializer that also supplies a CSS stylesheet.
|
|
*/
|
|
- (instancetype)initWithHTML:(NSData *)htmlData
|
|
cssStyleSheet:(DTCSSStylesheet *)styleSheet
|
|
options:(NSDictionary *)options;
|
|
|
|
// --- Building ---
|
|
|
|
/**
|
|
Triggers the full HTML parse → DOM tree → NSAttributedString pipeline.
|
|
Must be called before -generatedAttributedString.
|
|
*/
|
|
- (void)buildString;
|
|
|
|
/**
|
|
Returns the attributed string produced by the most recent -buildString call.
|
|
*/
|
|
- (NSAttributedString *)generatedAttributedString;
|
|
|
|
// --- DTHTMLParser delegate (SAX callbacks) ---
|
|
|
|
- (void)parser:(id)parser
|
|
didStartElement:(NSString *)elementName
|
|
attributes:(NSDictionary *)attributeDict
|
|
position:(NSUInteger)position;
|
|
|
|
- (void)parser:(id)parser
|
|
foundCDATA:(NSData *)CDATABlock;
|
|
|
|
- (void)parser:(id)parser
|
|
foundCharacters:(NSString *)string
|
|
position:(NSUInteger)position;
|
|
|
|
- (void)parserDidEndDocument:(id)parser;
|
|
|
|
// --- Tag handler registration (internal) ---
|
|
|
|
- (void)_registerTagStartHandlers;
|
|
- (void)_registerTagEndHandlers;
|
|
|
|
// --- Properties ---
|
|
|
|
@property (nonatomic, strong, readonly) NSData *htmlData;
|
|
@property (nonatomic, strong, readonly) DTCSSStylesheet *cssStyleSheet;
|
|
@property (nonatomic, strong, readonly) NSDictionary *options;
|
|
@property (nonatomic, strong, readonly) NSAttributedString *generatedAttributedString;
|
|
|
|
// WeRead-specific: the book / chapter context used during rendering.
|
|
@property (nonatomic, strong) WRBook *book;
|
|
@property (nonatomic, strong) WRChapter *chapter;
|
|
|
|
@end
|