Skip to content

Commit

Permalink
refactored contentType of DTTextAttachment into a class cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
odrobnik committed Apr 22, 2013
1 parent 4dcb0df commit 1203a25
Show file tree
Hide file tree
Showing 20 changed files with 577 additions and 335 deletions.
1 change: 1 addition & 0 deletions Core/Source/DTAttributedTextContentView.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "DTAttributedTextContentView.h"
#import "DTCoreText.h"
#import "DTDictationPlaceholderTextAttachment.h"
#import <QuartzCore/QuartzCore.h>

#if !__has_feature(objc_arc)
Expand Down
9 changes: 7 additions & 2 deletions Core/Source/DTCoreText.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#import "DTCSSStylesheet.h"
#import "DTCoreTextFontDescriptor.h"
#import "DTHTMLElement.h"
#import "DTTextAttachment.h"
#import "NSCharacterSet+HTML.h"
#import "NSScanner+HTML.h"
#import "NSMutableString+HTML.h"
Expand All @@ -41,6 +40,13 @@
#import "DTHTMLParserNode.h"
#import "DTHTMLParserTextNode.h"

// text attachment cluster
#import "DTTextAttachment.h"
#import "DTDictationPlaceholderTextAttachment.h"
#import "DTTextAttachmentIframe.h"
#import "DTTextAttachmentImage.h"
#import "DTTextAttachmentObject.h"
#import "DTTextAttachmentVideo.h"

// These classes only work with UIKit on iOS
#if TARGET_OS_IPHONE
Expand All @@ -60,7 +66,6 @@
#import "DTCoreTextLayoutLine.h"
#import "DTCoreTextLayouter.h"

#import "DTDictationPlaceholderTextAttachment.h"
#import "DTDictationPlaceholderView.h"

#import "UIFont+DTCoreText.h"
Expand Down
6 changes: 3 additions & 3 deletions Core/Source/DTCoreTextLayoutFrame.m
Original file line number Diff line number Diff line change
Expand Up @@ -1117,9 +1117,9 @@ - (void)drawInContext:(CGContextRef)context options:(DTCoreTextLayoutFrameDrawin
{
if (drawImages)
{
if (attachment.contentType == DTTextAttachmentTypeImage)
if ([attachment isKindOfClass:[DTTextAttachmentImage class]])
{
DTImage *image = (id)attachment.contents;
DTTextAttachmentImage *imageAttachment = (DTTextAttachmentImage *)attachment;

// frame might be different due to image vertical alignment
CGFloat ascender = [attachment ascentForLayout];
Expand All @@ -1129,7 +1129,7 @@ - (void)drawInContext:(CGContextRef)context options:(DTCoreTextLayoutFrameDrawin
origin.y = self.frame.size.height - origin.y - ascender - descender;
CGRect flippedRect = CGRectMake(roundf(origin.x), roundf(origin.y), attachment.displaySize.width, attachment.displaySize.height);

CGContextDrawImage(context, flippedRect, image.CGImage);
CGContextDrawImage(context, flippedRect, imageAttachment.image.CGImage);
}
}
}
Expand Down
5 changes: 0 additions & 5 deletions Core/Source/DTDictationPlaceholderTextAttachment.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ - (CGSize)originalSize
return [self displaySize];
}

- (DTTextAttachmentType)contentType
{
return DTTextAttachmentTypeGeneric;
}

- (CGFloat)ascentForLayout
{
return self.displaySize.height;
Expand Down
2 changes: 1 addition & 1 deletion Core/Source/DTHTMLAttributedStringBuilder.m
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ - (void)_registerTagEndHandlers
{
if ([_currentTag isKindOfClass:[DTHTMLElementAttachment class]])
{
if (_currentTag.textAttachment.contentType == DTTextAttachmentTypeObject)
if ([_currentTag.textAttachment isKindOfClass:[DTTextAttachmentObject class]])
{
// transfer the child nodes to the attachment
_currentTag.textAttachment.childNodes = [_currentTag.childNodes copy];
Expand Down
62 changes: 26 additions & 36 deletions Core/Source/DTHTMLWriter.m
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,29 @@ - (void)_buildOutput
[self _buildOutputAsHTMLFragment:NO];
}


- (NSString *)_blockNameForAttachment:(DTTextAttachment *)attachment
{
if ([attachment isKindOfClass:[DTTextAttachmentImage class]])
{
return @"img";
}
else if ([attachment isKindOfClass:[DTTextAttachmentVideo class]])
{
return @"video";
}
else if ([attachment isKindOfClass:[DTTextAttachmentObject class]])
{
return @"object";
}
else if ([attachment isKindOfClass:[DTTextAttachmentIframe class]])
{
return @"iframe";
}

return nil;
}

- (void)_buildOutputAsHTMLFragment:(BOOL)fragment
{
// reusable styles
Expand Down Expand Up @@ -468,9 +491,9 @@ - (void)_buildOutputAsHTMLFragment:(BOOL)fragment
}
else
{
if (attachment.contentType == DTTextAttachmentTypeImage && attachment.contents)
if ([attachment isKindOfClass:[DTTextAttachmentImage class]] && [(DTTextAttachmentImage *)attachment image])
{
urlString = [attachment dataURLRepresentation];
urlString = [(DTTextAttachmentImage *)attachment dataURLRepresentation];
}
else
{
Expand All @@ -479,40 +502,7 @@ - (void)_buildOutputAsHTMLFragment:(BOOL)fragment
}
}

NSString *blockName;

switch (attachment.contentType)
{
case DTTextAttachmentTypeVideoURL:
{
blockName = @"video";
break;
}

case DTTextAttachmentTypeImage:
{
blockName = @"img";
break;
}

case DTTextAttachmentTypeObject:
{
blockName = @"object";
break;
}

case DTTextAttachmentTypeIframe:
{
blockName = @"iframe";
break;
}

default:
{
// we don't know how to output this
continue;
}
}
NSString *blockName = [self _blockNameForAttachment:attachment];

// output tag start
[retString appendFormat:@"<%@", blockName];
Expand Down
39 changes: 14 additions & 25 deletions Core/Source/DTTextAttachment.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@

@class DTHTMLElement;

typedef enum
{
DTTextAttachmentTypeImage,
DTTextAttachmentTypeVideoURL,
DTTextAttachmentTypeIframe,
DTTextAttachmentTypeObject,
DTTextAttachmentTypeGeneric
} DTTextAttachmentType;

typedef enum
{
DTTextAttachmentVerticalAlignmentBaseline = 0,
Expand All @@ -34,7 +25,14 @@ typedef enum
/**
An object to represent an attachment in an HTML/rich text view.
*/
@interface DTTextAttachment : NSObject
@interface DTTextAttachment : NSObject
{
CGSize _displaySize; // the display dimensions of the attachment
CGSize _originalSize; // the original dimensions of the attachment
CGSize _maxImageSize; // the maximum dimensions to size to
NSURL *_contentURL;
NSDictionary *_attributes; // attributes transferred from HTML element
}

/**
@name Creating Text Attachments
Expand All @@ -45,21 +43,17 @@ typedef enum
The element must have a valid tagName. The size of the returned text attachment is determined by the element, constrained by the option's key for DTMaxImageSize. Any valid image resource included in the element (denoted by the method attributeForKey: "src") is loaded and determines the text attachment size if it was not known before. If a size is too large the image is downsampled with sizeThatFitsKeepingAspectRatio() which preserves the aspect ratio.
@param element A DTHTMLElement that must have a valid tag name and should have a size. Any element attributes are copied to the text attachment's elements.
@param options An NSDictionary of options. Used to specify the max image size with the key DTMaxImageSize.
@returns Returns an initialized DTTextAttachment built using the element and options parameters.
@returns Returns the appropriate subclass of the class cluster
*/
+ (DTTextAttachment *)textAttachmentWithElement:(DTHTMLElement *)element options:(NSDictionary *)options;


/**
@name Alternate Representations
*/

/**
Retrieves a string which is in the format "data:image/png;base64,%@" with this DTTextAttachment's content's data representation encoded in Base64 string encoding. For image contents only.
@returns A Base64 encoded string of the png data representation of this text attachment's image contents.
*/
- (NSString *)dataURLRepresentation;

The designated initializer for members of the DTTextAttachment class cluster. If you need additional setup for custom subclasses then you should override this initializer.
@param element A DTHTMLElement that must have a valid tag name and should have a size. Any element attributes are copied to the text attachment's elements.
@param options An NSDictionary of options. Used to specify the max image size with the key DTMaxImageSize.
@returns Returns an initialized DTTextAttachment built using the element and options parameters. */
- (id)initWithElement:(DTHTMLElement *)element options:(NSDictionary *)options;

/**
@name Vertical Alignment
Expand Down Expand Up @@ -115,11 +109,6 @@ typedef enum
*/
@property (nonatomic, strong) id contents;

/**
The content type of the attachment
*/
@property (nonatomic, assign) DTTextAttachmentType contentType;

/**
The URL representing the content
*/
Expand Down
Loading

0 comments on commit 1203a25

Please sign in to comment.