Skip to content

Commit 64ef13e

Browse files
committed
Thumbnail adjusts size as needed. Removed file name from preview.
1 parent 4570e53 commit 64ef13e

File tree

6 files changed

+426
-923
lines changed

6 files changed

+426
-923
lines changed

GeneratePreviewForURL.m

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview,
6969
NSString *css = [[NSString alloc] initWithContentsOfFile:cssPath encoding:NSUTF8StringEncoding error:NULL];
7070

7171
NSString *path = [myURL path];
72-
NSString *fileName = [[path componentsSeparatedByString:@"/"] lastObject];
7372
NSDictionary *fileAttributes = [[NSFileManager defaultManager] fileAttributesAtPath:path traverseLink:YES];
7473

7574
// compose the html
@@ -81,9 +80,7 @@ OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview,
8180
[html appendString:css];
8281
[css release];
8382
}
84-
[html appendFormat:@"</style></head><body><h1>%@</h1>", fileName];
85-
//[html appendFormat:@"<div class=\"file_info\">%@</div>", path];
86-
[html appendFormat:@"<div class=\"file_info\"><b>%s</b>, %s, <b>%i</b> %@</div><table>",
83+
[html appendFormat:@"</style></head><body><div class=\"file_info\"><b>%s</b>, %s, <b>%i</b> %@</div><table>",
8784
formatFilesize([[fileAttributes objectForKey:NSFileSize] floatValue]),
8885
humanReadableFileEncoding(stringEncoding),
8986
[csvDoc.columnKeys count],

GenerateThumbnailForURL.m

Lines changed: 77 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#import "CSVDocument.h"
66
#import "CSVRowObject.h"
77

8+
#define MIN_WIDTH 40.0
9+
10+
static CGContextRef createRGBABitmapContext(CGSize pixelSize);
11+
812

913
/* -----------------------------------------------------------------------------
1014
Generate a thumbnail for file
@@ -52,21 +56,16 @@ OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thum
5256

5357

5458
// Draw an icon if still interested in the thumbnail
55-
if(false == QLThumbnailRequestIsCancelled(thumbnail)) {
56-
CGFloat startY = 0.0;
57-
if(gotRows < numRows) {
58-
startY = thumbnailSize - gotRows * rowHeight;
59-
}
60-
CGRect myBounds = CGRectMake(0.0, startY, thumbnailSize, thumbnailSize);
61-
CGContextRef context = QLThumbnailRequestCreateContext(thumbnail, myBounds.size, false, NULL);
59+
if((gotRows > 0) && (false == QLThumbnailRequestIsCancelled(thumbnail))) {
60+
CGRect maxBounds = CGRectMake(0.0, 0.0, thumbnailSize, thumbnailSize);
61+
CGRect usedBounds = CGRectMake(0.0, 0.0, thumbnailSize, thumbnailSize);
6262

63-
// Draw a mini table
63+
CGContextRef context = createRGBABitmapContext(maxBounds.size);
6464
if(context) {
65-
CGContextSaveGState(context);
6665

6766
// Flip CoreGraphics coordinate system
6867
CGContextScaleCTM(context, 1.0, -1.0);
69-
CGContextTranslateCTM(context, 0, -myBounds.size.height);
68+
CGContextTranslateCTM(context, 0, -maxBounds.size.height);
7069

7170
// Create colors
7271
CGColorRef borderColor = CGColorCreateGenericRGB(0.67, 0.67, 0.67, 1.0);
@@ -90,20 +89,26 @@ OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thum
9089

9190
// We loop each cell, row by row for each column
9291
for(NSString *colKey in csvDoc.columnKeys) {
93-
if(cellX > myBounds.size.width) {
92+
if(cellX > maxBounds.size.width) {
9493
break;
9594
}
9695

97-
CGRect rowRect = CGRectMake(cellX, 0.0, myBounds.size.width - cellX, rowHeight);
96+
CGRect rowRect = CGRectMake(cellX, 0.0, maxBounds.size.width - cellX, rowHeight);
9897
maxCellStringWidth = 0.0;
9998
BOOL altRow = NO;
99+
BOOL isFirstColumn = [csvDoc isFirstColumn:colKey];
100100

101101
// loop rows
102102
for(CSVRowObject *row in csvDoc.rows) {
103-
CGContextSetFillColorWithColor(context, altRow ? altRowBG : rowBG);
104-
CGContextFillRect(context, rowRect);
105103

106-
if(![csvDoc isFirstColumn:colKey]) {
104+
// Draw background
105+
if(isFirstColumn) {
106+
CGContextSetFillColorWithColor(context, altRow ? altRowBG : rowBG);
107+
CGContextFillRect(context, rowRect);
108+
}
109+
110+
// Draw border
111+
else {
107112
CGContextMoveToPoint(context, cellX + borderWidth / 2, rowRect.origin.y);
108113
CGContextAddLineToPoint(context, cellX + borderWidth / 2, rowRect.origin.y + rowRect.size.height);
109114
CGContextSetStrokeColorWithColor(context, borderColor);
@@ -128,24 +133,38 @@ OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thum
128133
cellX += maxCellStringWidth + 2 * textXPadding;
129134
}
130135

131-
// Crop the thumbnail if we didn't use the whole width
132-
if(cellX < myBounds.size.width) {
133-
myBounds.size.width -= cellX;
134-
CGRect clearRect = CGRectMake(cellX, 0.0, myBounds.size.width, myBounds.size.height);
135-
CGContextClearRect(context, clearRect);
136-
137-
// we should now center the thumbnail in our context or crop the context somehow...
136+
// adjust usedBounds.size.width...
137+
if(cellX < maxBounds.size.width) {
138+
usedBounds.size.width = (cellX < MIN_WIDTH) ? MIN_WIDTH : cellX;
139+
}
140+
141+
// ...and usedBounds.size.height
142+
if(gotRows < numRows) {
143+
usedBounds.size.height = gotRows * rowHeight;
138144
}
139145
}
140146

141147
CGColorRelease(borderColor);
142148
CGColorRelease(rowBG);
143149
CGColorRelease(altRowBG);
144150

145-
// Clean up
146-
CGContextRestoreGState(context);
147-
QLThumbnailRequestFlushContext(thumbnail, context);
151+
// Draw the image to the thumbnail request
152+
CGContextRef thumbContext = QLThumbnailRequestCreateContext(thumbnail, usedBounds.size, false, NULL);
153+
154+
CGImageRef fullImage = CGBitmapContextCreateImage(context);
155+
CGImageRef usedImage = CGImageCreateWithImageInRect(fullImage, usedBounds);
156+
CGImageRelease(fullImage);
157+
CGContextDrawImage(thumbContext, usedBounds, usedImage);
158+
CGImageRelease(usedImage);
159+
160+
// we no longer need the bitmap data; free
161+
char *bitmapData = CGBitmapContextGetData(context);
162+
if(bitmapData) {
163+
free(bitmapData);
164+
}
148165
CFRelease(context);
166+
167+
QLThumbnailRequestFlushContext(thumbnail, thumbContext);
149168
}
150169
}
151170
}
@@ -158,4 +177,37 @@ OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thum
158177
void CancelThumbnailGeneration(void* thisInterface, QLThumbnailRequestRef thumbnail)
159178
{
160179
}
180+
#pragma mark -
181+
182+
183+
184+
#pragma mark Creating a bitmap context
185+
static CGContextRef createRGBABitmapContext(CGSize pixelSize)
186+
{
187+
NSUInteger width = pixelSize.width;
188+
NSUInteger height = pixelSize.height;
189+
NSUInteger bitmapBytesPerRow = width * 4; // 1 byte per component r g b a
190+
NSUInteger bitmapBytes = bitmapBytesPerRow * height;
191+
192+
// allocate needed bytes
193+
void *bitmapData = malloc(bitmapBytes);
194+
if(NULL == bitmapData) {
195+
fprintf(stderr, "Oops, could not allocate bitmap data!");
196+
return NULL;
197+
}
198+
199+
// create the context
200+
CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
201+
CGContextRef context = CGBitmapContextCreate(bitmapData, width, height, 8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);
202+
CGColorSpaceRelease(colorSpace);
203+
204+
// context creation fail
205+
if(NULL == context) {
206+
free(bitmapData);
207+
fprintf(stderr, "Oops, could not create the context!");
208+
return NULL;
209+
}
210+
211+
return context;
212+
}
161213

INSTALL.rtf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
If the QuickLook-folder does not exist, simply create it manually.\
2121
\
2222
This is version
23-
\b 0.1
23+
\b 0.2
2424
\b0 of the plugin.\
2525
{\field{\*\fldinst{HYPERLINK "http://code.google.com/p/quicklook-csv/"}}{\fldrslt http://code.google.com/p/quicklook-csv/}}\
2626
}

0 commit comments

Comments
 (0)