Skip to content

Commit

Permalink
Added setDisplaySize:withMaxDisplaySize: to automatically calculate m…
Browse files Browse the repository at this point in the history
…issing dimensions

Also for #338
  • Loading branch information
odrobnik committed Mar 13, 2013
1 parent 99da4df commit 48aad8d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 22 deletions.
9 changes: 9 additions & 0 deletions Core/Source/DTTextAttachment.h
Expand Up @@ -101,6 +101,15 @@ typedef enum
*/
@property (nonatomic, assign) CGSize displaySize;

/**
Updates the display size optionally passing a maximum size that it should not exceed.
This method in contrast to using the displaySize property will use the originalSize and max display size to calculate missing dimensions.
@param displaySize The new size to display the content with
@param maxDisplaySize the maximum size that the content should be scaled to fit
*/
- (void)setDisplaySize:(CGSize)displaySize withMaxDisplaySize:(CGSize)maxDisplaySize;

/**
The contents of the receiver
*/
Expand Down
62 changes: 40 additions & 22 deletions Core/Source/DTTextAttachment.m
Expand Up @@ -180,38 +180,22 @@ + (DTTextAttachment *)textAttachmentWithElement:(DTHTMLElement *)element options
if ([contentURL isFileURL])
{
DTImage *image = [[DTTextAttachment sharedImageCache] objectForKey:[contentURL path]];
if (!image) {
if (!image)
{
image = [[DTImage alloc] initWithContentsOfFile:[contentURL path]];
[[DTTextAttachment sharedImageCache] setObject:image forKey:[contentURL path]];
}

originalSize = image.size;

// width and/or height missing
if (displaySize.width==0 && displaySize.height==0)
{
displaySize = originalSize;
}
else if (!displaySize.width && displaySize.height)
{
CGFloat factor = image.size.height / displaySize.height;
displaySize.width = roundf(image.size.width / factor);
}
else if (displaySize.width>0 && displaySize.height==0)
{
CGFloat factor = image.size.width / displaySize.width;
displaySize.height = roundf(image.size.height / factor);
}
}
else
{
// remote image, we have to relayout once this size is known
displaySize = CGSizeMake(1, 1); // one pixel so that loading is triggered
// initial display size matches original
displaySize = originalSize;
}
}
}



// if you have no display size we assume original size
if (CGSizeEqualToSize(displaySize, CGSizeZero))
{
Expand Down Expand Up @@ -327,7 +311,41 @@ - (void)setOriginalSize:(CGSize)originalSize
self.displaySize = _originalSize;
}

/**
- (void)setDisplaySize:(CGSize)displaySize withMaxDisplaySize:(CGSize)maxDisplaySize
{
if (_originalSize.width && _originalSize.height)
{
// width and/or height missing
if (displaySize.width==0 && displaySize.height==0)
{
displaySize = _originalSize;
}
else if (!displaySize.width && displaySize.height)
{
// width missing, calculate it
CGFloat factor = _originalSize.height / displaySize.height;
displaySize.width = roundf(_originalSize.width / factor);
}
else if (displaySize.width>0 && displaySize.height==0)
{
// height missing, calculate it
CGFloat factor = _originalSize.width / displaySize.width;
displaySize.height = roundf(_originalSize.height / factor);
}
}

if (maxDisplaySize.width>0 && maxDisplaySize.height>0)
{
if (maxDisplaySize.width < displaySize.width || maxDisplaySize.height < displaySize.height)
{
displaySize = sizeThatFitsKeepingAspectRatio(displaySize, maxDisplaySize);
}
}

_displaySize = displaySize;
}

/**
Accessor for the contents instance variable. If the content type is DTTextAttachmentTypeImage this returns a DTImage instance of the contents.
@returns Contents. If it is an image, a DTImage instance is returned. Otherwise it is returned as is.
*/
Expand Down

0 comments on commit 48aad8d

Please sign in to comment.