Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implemented basic vertical-align support for attachments

  • Loading branch information...
commit b091443fb1923920b536ef4d2206afab7c66f9fa 1 parent dc59483
@Cocoanetics authored
View
3  Core/Source/DTCoreText.h
@@ -53,3 +53,6 @@
#import "DTCoreTextLayouter.h"
#endif
+
+
+#define DT_ADD_FONT_ON_ATTACHMENTS
View
3  Core/Source/DTCoreTextGlyphRun.m
@@ -9,6 +9,7 @@
#import "DTCoreTextGlyphRun.h"
#import "DTCoreTextLayoutLine.h"
#import "DTTextAttachment.h"
+#import "DTCoreTextConstants.h"
#ifndef __IPHONE_4_3
#define __IPHONE_4_3 40300
@@ -217,7 +218,7 @@ - (DTTextAttachment *)attachment
{
if (!_didCheckForAttachmentInAttributes)
{
- _attachment = [self.attributes objectForKey:@"DTTextAttachment"];
+ _attachment = [self.attributes objectForKey:NSAttachmentAttributeName];
_didCheckForAttachmentInAttributes = YES;
}
View
18 Core/Source/DTHTMLAttributedStringBuilder.m
@@ -12,6 +12,7 @@
#import "DTCSSStylesheet.h"
#import "DTCoreTextFontDescriptor.h"
#import "DTCoreTextParagraphStyle.h"
+#import "DTTextAttachment.h"
#import "DTColor+HTML.h"
#import "DTImage+HTML.h"
@@ -280,6 +281,23 @@ - (BOOL)buildString
dispatch_group_wait(_stringParsingGroup, DISPATCH_TIME_FOREVER);
dispatch_group_wait(_stringAssemblyGroup, DISPATCH_TIME_FOREVER);
+
+
+ // walk through attachments and tell them about their surrounding font
+ [tmpString enumerateAttribute:NSAttachmentAttributeName inRange:NSMakeRange(0, [tmpString length])
+ options:0
+ usingBlock:^(DTTextAttachment *attachment, NSRange range, BOOL *stop) {
+ if (attachment)
+ {
+ CTFontRef usedFont = (__bridge CTFontRef)[tmpString attribute:(id)kCTFontAttributeName atIndex:range.location effectiveRange:NULL];
+
+ if (usedFont)
+ {
+ [attachment adjustVerticalAlignmentForFont:usedFont];
+ }
+ }
+ }];
+
return result;
}
View
43 Core/Source/DTHTMLElement.m
@@ -42,7 +42,8 @@ @implementation DTHTMLElement
DTCoreTextFontDescriptor *fontDescriptor;
DTCoreTextParagraphStyle *paragraphStyle;
- DTTextAttachment *textAttachment;
+ DTTextAttachment *_textAttachment;
+ DTTextAttachmentVerticalAlignment _textAttachmentAlignment;
NSURL *link;
DTColor *_textColor;
@@ -110,16 +111,16 @@ - (NSDictionary *)attributesDictionary
}
// add text attachment
- if (textAttachment)
+ if (_textAttachment)
{
#if TARGET_OS_IPHONE
// need run delegate for sizing (only supported on iOS)
- CTRunDelegateRef embeddedObjectRunDelegate = createEmbeddedObjectRunDelegate(textAttachment);
+ CTRunDelegateRef embeddedObjectRunDelegate = createEmbeddedObjectRunDelegate(_textAttachment);
[tmpDict setObject:CFBridgingRelease(embeddedObjectRunDelegate) forKey:(id)kCTRunDelegateAttributeName];
#endif
// add attachment
- [tmpDict setObject:textAttachment forKey:@"DTTextAttachment"];
+ [tmpDict setObject:_textAttachment forKey:NSAttachmentAttributeName];
// remember original paragraphSpacing
[tmpDict setObject:[NSNumber numberWithFloat:self.paragraphStyle.paragraphSpacing] forKey:@"DTAttachmentParagraphSpacing"];
@@ -225,7 +226,7 @@ - (NSAttributedString *)attributedString
{
NSDictionary *attributes = [self attributesDictionary];
- if (textAttachment)
+ if (_textAttachment)
{
// ignore text, use unicode object placeholder
NSMutableAttributedString *tmpString = [[NSMutableAttributedString alloc] initWithString:UNICODE_OBJECT_PLACEHOLDER attributes:attributes];
@@ -331,7 +332,7 @@ - (NSAttributedString *)prefixForListItem
#endif
// add attachment
- [attributes setObject:attachment forKey:@"DTTextAttachment"];
+ [attributes setObject:attachment forKey:NSAttachmentAttributeName];
if (calculatedListStyle.position == DTCSSListStylePositionInside)
{
@@ -729,6 +730,28 @@ - (void)applyStyleDictionary:(NSDictionary *)styles
// nothing to do
}
}
+
+ // only works for objects!
+ NSString *verticalAlignString = [styles objectForKey:@"vertical-align"];
+ if (verticalAlignString)
+ {
+ if ([verticalAlignString isEqualToString:@"text-top"])
+ {
+ _textAttachmentAlignment = DTTextAttachmentVerticalAlignmentTop;
+ }
+ else if ([verticalAlignString isEqualToString:@"middle"])
+ {
+ _textAttachmentAlignment = DTTextAttachmentVerticalAlignmentCenter;
+ }
+ else if ([verticalAlignString isEqualToString:@"text-bottom"])
+ {
+ _textAttachmentAlignment = DTTextAttachmentVerticalAlignmentBottom;
+ }
+ else if ([verticalAlignString isEqualToString:@"baseline"])
+ {
+ _textAttachmentAlignment = DTTextAttachmentVerticalAlignmentBaseline;
+ }
+ }
}
- (void)parseStyleString:(NSString *)styleString
@@ -1019,6 +1042,12 @@ - (void)setAttributes:(NSDictionary *)attributes
}
}
+- (void)setTextAttachment:(DTTextAttachment *)textAttachment
+{
+ textAttachment.verticalAlignment = _textAttachmentAlignment;
+ _textAttachment = textAttachment;
+}
+
@synthesize parent;
@synthesize fontDescriptor;
@@ -1029,7 +1058,7 @@ - (void)setAttributes:(NSDictionary *)attributes
@synthesize text;
@synthesize link;
@synthesize underlineStyle;
-@synthesize textAttachment;
+@synthesize textAttachment = _textAttachment;
@synthesize tagContentInvisible;
@synthesize strikeOut;
@synthesize superscriptStyle;
View
15 Core/Source/DTTextAttachment.h
@@ -19,6 +19,14 @@ typedef enum
DTTextAttachmentTypeGeneric
} DTTextAttachmentType;
+typedef enum
+{
+ DTTextAttachmentVerticalAlignmentBaseline = 0,
+ DTTextAttachmentVerticalAlignmentTop,
+ DTTextAttachmentVerticalAlignmentCenter,
+ DTTextAttachmentVerticalAlignmentBottom
+} DTTextAttachmentVerticalAlignment;
+
@interface DTTextAttachment : NSObject
@@ -29,10 +37,17 @@ typedef enum
@property (nonatomic, strong) NSURL *contentURL;
@property (nonatomic, strong) NSURL *hyperLinkURL;
@property (nonatomic, strong) NSDictionary *attributes;
+@property (nonatomic, assign) DTTextAttachmentVerticalAlignment verticalAlignment;
+ (DTTextAttachment *)textAttachmentWithElement:(DTHTMLElement *)element options:(NSDictionary *)options;
- (NSString *)dataURLRepresentation;
+- (void)adjustVerticalAlignmentForFont:(CTFontRef)font;
+
+// customized ascend and descent for the run delegates
+- (CGFloat)ascentForLayout;
+- (CGFloat)descentForLayout;
+
@end
View
63 Core/Source/DTTextAttachment.m
@@ -12,11 +12,13 @@
#import "NSAttributedString+HTML.h"
#import "NSData+DTBase64.h"
#import "DTImage+HTML.h"
+#import "DTCoreTextConstants.h"
@implementation DTTextAttachment
{
CGSize _originalSize;
CGSize _displaySize;
+ DTTextAttachmentVerticalAlignment _verticalAlignment;
id contents;
NSDictionary *_attributes;
@@ -24,6 +26,10 @@ @implementation DTTextAttachment
NSURL *_contentURL;
NSURL *_hyperLinkURL;
+
+ CGFloat _fontLeading;
+ CGFloat _fontAscent;
+ CGFloat _fontDescent;
}
+ (DTTextAttachment *)textAttachmentWithElement:(DTHTMLElement *)element options:(NSDictionary *)options
@@ -199,6 +205,62 @@ - (NSString *)dataURLRepresentation
return [@"data:image/png;base64," stringByAppendingString:encoded];
}
+- (void)adjustVerticalAlignmentForFont:(CTFontRef)font
+{
+ _fontLeading = CTFontGetLeading(font);
+ _fontAscent = CTFontGetAscent(font);
+ _fontDescent = CTFontGetDescent(font);
+}
+
+- (CGFloat)ascentForLayout
+{
+ switch (_verticalAlignment)
+ {
+ case DTTextAttachmentVerticalAlignmentBaseline:
+ {
+ return _displaySize.height;
+ }
+ case DTTextAttachmentVerticalAlignmentTop:
+ {
+ return _fontAscent;
+ }
+ case DTTextAttachmentVerticalAlignmentCenter:
+ {
+ CGFloat halfHeight = (_fontAscent + _fontDescent) / 2.0f;
+
+ return halfHeight - _fontDescent + _displaySize.height/2.0f;
+ }
+ case DTTextAttachmentVerticalAlignmentBottom:
+ {
+ return _displaySize.height - _fontDescent;
+ }
+ }
+}
+
+- (CGFloat)descentForLayout
+{
+ switch (_verticalAlignment)
+ {
+ case DTTextAttachmentVerticalAlignmentBaseline:
+ {
+ return 0;
+ }
+ case DTTextAttachmentVerticalAlignmentTop:
+ {
+ return _displaySize.height - _fontAscent;
+ }
+ case DTTextAttachmentVerticalAlignmentCenter:
+ {
+ CGFloat halfHeight = (_fontAscent + _fontDescent) / 2.0f;
+
+ return halfHeight - _fontAscent + _displaySize.height/2.0f;
+ }
+ case DTTextAttachmentVerticalAlignmentBottom:
+ {
+ return _fontDescent;
+ }
+ }
+}
#pragma mark Properties
@@ -230,5 +292,6 @@ - (id)contents
@synthesize contentURL = _contentURL;
@synthesize hyperLinkURL = _hyperLinkURL;
@synthesize attributes = _attributes;
+@synthesize verticalAlignment = _verticalAlignment;
@end
View
4 Core/Source/NSAttributedString+DTCoreText.m
@@ -41,7 +41,7 @@ - (NSArray *)textAttachmentsWithPredicate:(NSPredicate *)predicate
NSRange range;
NSDictionary *attributes = [self attributesAtIndex:index effectiveRange:&range];
- DTTextAttachment *attachment = [attributes objectForKey:@"DTTextAttachment"];
+ DTTextAttachment *attachment = [attributes objectForKey:NSAttachmentAttributeName];
if (attachment)
{
@@ -185,7 +185,7 @@ - (NSString *)htmlString
continue;
}
- DTTextAttachment *attachment = [attributes objectForKey:@"DTTextAttachment"];
+ DTTextAttachment *attachment = [attributes objectForKey:NSAttachmentAttributeName];
if (attachment)
View
4 Core/Source/NSAttributedStringRunDelegates.m
@@ -18,7 +18,7 @@ CGFloat embeddedObjectGetAscentCallback(void *context)
{
if ([(__bridge id)context isKindOfClass:[DTTextAttachment class]])
{
- return [(__bridge DTTextAttachment *)context displaySize].height;
+ return [(__bridge DTTextAttachment *)context ascentForLayout];
}
return 0;
}
@@ -26,7 +26,7 @@ CGFloat embeddedObjectGetDescentCallback(void *context)
{
if ([(__bridge id)context isKindOfClass:[DTTextAttachment class]])
{
- return 0;
+ return [(__bridge DTTextAttachment *)context descentForLayout];
}
return 0;
}
View
4 Core/Source/NSMutableAttributedString+HTML.m
@@ -27,11 +27,11 @@ - (void)appendString:(NSString *)string
}
// need to remove image placeholder to prevent duplication
- if ([previousAttributes objectForKey:@"DTTextAttachment"])
+ if ([previousAttributes objectForKey:NSAttachmentAttributeName])
{
NSMutableDictionary *tmpDict = [previousAttributes mutableCopy];
- [tmpDict removeObjectForKey:@"DTTextAttachment"];
+ [tmpDict removeObjectForKey:NSAttachmentAttributeName];
[tmpDict removeObjectForKey:(id)kCTRunDelegateAttributeName];
previousAttributes = tmpDict;
View
13 Demo/Resources/CurrentTest.html
@@ -1,5 +1,8 @@
-<h2>Examples</h2>
-
-<p style="text-align:right">Right</p> <center>Center</center> <p align="left">Left</p>
-
-<h2>Non-Justified</h2>
+<p style="font-size:20px">Baseline: <img style="vertical-align:baseline;" height=40 width=40 src="icon_smile.gif">
+</p>
+ <p style="font-size:20px">text-top: <img style="vertical-align:text-top;" height=40 width=40 src="icon_smile.gif">
+ </p>
+ <p style="font-size:20px">text-bottom: <img style="vertical-align:text-bottom;" height=40 width=40 src="icon_smile.gif">
+ </p>
+ <p style="font-size:20px">middle: <img style="vertical-align:middle;" height=40 width=40 src="icon_smile.gif">
+ </p>

0 comments on commit b091443

Please sign in to comment.
Something went wrong with that request. Please try again.