Revision: 3270
http://skim-app.svn.sourceforge.net/skim-app/?rev=3270&view=rev
Author: hofman
Date: 2007-11-26 10:28:27 -0800 (Mon, 26 Nov 2007)
Log Message:
-----------
Use html again for quicklook preview, as RTFD is not supported. Use HTML
escapes for the text.
Modified Paths:
--------------
trunk/QuickLook-Skim/GeneratePreviewForURL.m
trunk/QuickLook-Skim/GenerateThumbnailForURL.m
trunk/QuickLook-Skim/QuickLook-Skim.xcodeproj/project.pbxproj
trunk/QuickLook-Skim/SKQLConverter.h
trunk/QuickLook-Skim/SKQLConverter.m
Modified: trunk/QuickLook-Skim/GeneratePreviewForURL.m
===================================================================
--- trunk/QuickLook-Skim/GeneratePreviewForURL.m 2007-11-26 17:13:11 UTC
(rev 3269)
+++ trunk/QuickLook-Skim/GeneratePreviewForURL.m 2007-11-26 18:28:27 UTC
(rev 3270)
@@ -40,6 +40,26 @@
static const CGFloat _fontSize = 12.0;
+static NSDictionary *imageAttachments()
+{
+ NSMutableDictionary *attachments = [NSMutableDictionary dictionary];
+ NSBundle *bundle = SKQLGetMainBundle();
+ NSArray *allImageNames = [NSArray arrayWithObjects:@"FreeText", @"Note",
@"Circle", @"Square", @"Highlight", @"Underline", @"StrikeOut", @"Line", nil];
+ NSString *imageName;
+ NSMutableDictionary *imgProps;
+ NSData *imgData;
+
+ for (imageName in allImageNames) {
+ if (imgData = [NSData dataWithContentsOfFile:[bundle
pathForResource:imageName ofType:@"png"]]) {
+ imgProps = [[NSMutableDictionary alloc] init];
+ [imgProps setObject:imgData forKey:(NSString
*)kQLPreviewPropertyAttachmentDataKey];
+ [attachments setObject:imgProps forKey:[imageName
stringByAppendingPathExtension:@"png"]];
+ [imgProps release];
+ }
+ }
+ return attachments;
+}
+
/*
-----------------------------------------------------------------------------
Generate a preview for file
@@ -66,11 +86,14 @@
NSData *data = [[NSData alloc] initWithContentsOfURL:(NSURL *)url
options:NSUncachedRead error:NULL];
if (data) {
- NSAttributedString *attrString = [SKQLConverter
attributedStringWithNotes:[NSKeyedUnarchiver unarchiveObjectWithData:data]
fontSize:_fontSize];
+ NSString *htmlString = [SKQLConverter
htmlStringWithNotes:[NSKeyedUnarchiver unarchiveObjectWithData:data]
fontSize:_fontSize];
[data release];
-#warning kUTTypeRTFD not supported
- if (attrString && (data = [attrString RTFFromRange:NSMakeRange(0,
[attrString length]) documentAttributes:nil])) {
- QLPreviewRequestSetDataRepresentation(preview,
(CFDataRef)data, kUTTypeRTF, NULL);
+ if (data = [htmlString dataUsingEncoding:NSUTF8StringEncoding]) {
+ NSDictionary *props = [NSDictionary
dictionaryWithObjectsAndKeys:
+ @"UTF-8", (NSString
*)kQLPreviewPropertyTextEncodingNameKey,
+ @"text/html", (NSString
*)kQLPreviewPropertyMIMETypeKey,
+ imageAttachments(), (NSString
*)kQLPreviewPropertyAttachmentsKey, nil];
+ QLPreviewRequestSetDataRepresentation(preview,
(CFDataRef)data, kUTTypeHTML, (CFDictionaryRef)props);
err = noErr;
}
}
Modified: trunk/QuickLook-Skim/GenerateThumbnailForURL.m
===================================================================
--- trunk/QuickLook-Skim/GenerateThumbnailForURL.m 2007-11-26 17:13:11 UTC
(rev 3269)
+++ trunk/QuickLook-Skim/GenerateThumbnailForURL.m 2007-11-26 18:28:27 UTC
(rev 3270)
@@ -49,9 +49,6 @@
static const NSSize _containerSize = (NSSize) { 572, 752 };
static const NSRect _iconRect = (NSRect) { 50, 140, 512, 512 };
-// readable in Cover Flow view, and distinguishable as text in icon view
-static const CGFloat _fontSize = 20.0;
-
// wash the app icon over a white page background
static void drawBackgroundAndApplicationIconInCurrentContext()
{
@@ -171,7 +168,7 @@
NSData *data = [[NSData alloc] initWithContentsOfURL:(NSURL *)url
options:NSUncachedRead error:NULL];
if (data) {
- NSAttributedString *attrString = [SKQLConverter
attributedStringWithNotes:[NSKeyedUnarchiver unarchiveObjectWithData:data]
fontSize:_fontSize];
+ NSAttributedString *attrString = [SKQLConverter
attributedStringWithNotes:[NSKeyedUnarchiver unarchiveObjectWithData:data]];
[data release];
if (attrString) {
Modified: trunk/QuickLook-Skim/QuickLook-Skim.xcodeproj/project.pbxproj
===================================================================
--- trunk/QuickLook-Skim/QuickLook-Skim.xcodeproj/project.pbxproj
2007-11-26 17:13:11 UTC (rev 3269)
+++ trunk/QuickLook-Skim/QuickLook-Skim.xcodeproj/project.pbxproj
2007-11-26 18:28:27 UTC (rev 3270)
@@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
- objectVersion = 44;
+ objectVersion = 42;
objects = {
/* Begin PBXBuildFile section */
Modified: trunk/QuickLook-Skim/SKQLConverter.h
===================================================================
--- trunk/QuickLook-Skim/SKQLConverter.h 2007-11-26 17:13:11 UTC (rev
3269)
+++ trunk/QuickLook-Skim/SKQLConverter.h 2007-11-26 18:28:27 UTC (rev
3270)
@@ -37,5 +37,6 @@
extern NSString *SKQLPDFPathForPDFBundleURL(NSURL *url);
@interface SKQLConverter : NSObject
-+ (NSAttributedString *)attributedStringWithNotes:(NSArray *)notes
fontSize:(CGFloat)fontSize;
++ (NSAttributedString *)attributedStringWithNotes:(NSArray *)notes;
++ (NSString *)htmlStringWithNotes:(NSArray *)notes;
@end
Modified: trunk/QuickLook-Skim/SKQLConverter.m
===================================================================
--- trunk/QuickLook-Skim/SKQLConverter.m 2007-11-26 17:13:11 UTC (rev
3269)
+++ trunk/QuickLook-Skim/SKQLConverter.m 2007-11-26 18:28:27 UTC (rev
3270)
@@ -33,9 +33,10 @@
#import "SKQLConverter.h"
-static NSString *_noteFont = @"LucidaHandwriting-Italic";
+static NSString *_noteFontName = @"LucidaHandwriting-Italic";
+// readable in Cover Flow view, and distinguishable as text in icon view
static const CGFloat _fontSize = 20.0;
-static const CGFloat _noteIndent = 20.0;
+static const CGFloat _smallFontSize = 10.0;
NSBundle *SKQLGetMainBundle() { return [NSBundle
bundleWithIdentifier:@"net.sourceforge.skim-app.quicklookgenerator"]; }
@@ -71,15 +72,76 @@
return attrString;
}
+static NSString *hexStringWithColor(NSColor *color)
+{
+ static char hexChars[16] = "0123456789abcdef";
+ if ([color alphaComponent] < 1.0)
+ color = [[NSColor controlBackgroundColor]
blendedColorWithFraction:[color alphaComponent] ofColor:[color
colorWithAlphaComponent:1.0]];
+ int red = (int)roundf(255 * [color redComponent]);
+ int green = (int)roundf(255 * [color greenComponent]);
+ int blue = (int)roundf(255 * [color blueComponent]);
+ return [NSString stringWithFormat:@"%C%C%C%C%C%C", hexChars[red / 16],
hexChars[red % 16], hexChars[green / 16], hexChars[green % 16], hexChars[blue /
16], hexChars[blue % 16]];
+}
+// Stolen from OmniFoundation
+static NSString *HTMLEscapeString(NSString *string)
+{
+ unichar *ptr, *begin, *end;
+ NSMutableString *result;
+ NSString *string;
+ int length;
+
+#define APPEND_PREVIOUS() \
+ string = [[NSString alloc] initWithCharacters:begin length:(ptr - begin)];
\
+ [result appendString:string]; \
+ [string release]; \
+ begin = ptr + 1;
+
+ length = [self length];
+ ptr = alloca(length * sizeof(unichar));
+ end = ptr + length;
+ [self getCharacters:ptr];
+ result = [NSMutableString stringWithCapacity:length];
+
+ begin = ptr;
+ while (ptr < end) {
+ if (*ptr > 127) {
+ APPEND_PREVIOUS();
+ [result appendFormat:@"&#%d;", (int)*ptr];
+ } else if (*ptr == '&') {
+ APPEND_PREVIOUS();
+ [result appendString:@"&"];
+ } else if (*ptr == '\"') {
+ APPEND_PREVIOUS();
+ [result appendString:@"""];
+ } else if (*ptr == '<') {
+ APPEND_PREVIOUS();
+ [result appendString:@"<"];
+ } else if (*ptr == '>') {
+ APPEND_PREVIOUS();
+ [result appendString:@">"];
+ } else if (*ptr == '\r') {
+ APPEND_PREVIOUS();
+ if (ptr+1 == end || *(ptr+1) != '\n')
+ [result appendString:@"<br />"];
+ } else if (*ptr == '\n') {
+ APPEND_PREVIOUS();
+ [result appendString:@"<br />"];
+ }
+ ptr++;
+ }
+ APPEND_PREVIOUS();
+ return result;
+}
+
@implementation SKQLConverter
-+ (NSAttributedString *)attributedStringWithNotes:(NSArray *)notes
fontSize:(CGFloat)fontSize;
++ (NSAttributedString *)attributedStringWithNotes:(NSArray *)notes;
{
NSMutableAttributedString *attrString = [[[NSMutableAttributedString
alloc] init] autorelease];
NSFont *font = [NSFont userFontOfSize:_fontSize];
- NSFont *noteFont = [NSFont fontWithName:_noteFont size:fontSize];
- NSFont *noteTextFont = [NSFont fontWithName:_noteFont size:MAX(fontSize /
2, 10.0)];
+ NSFont *noteFont = [NSFont fontWithName:_noteFontName size:_fontSize];
+ NSFont *noteTextFont = [NSFont fontWithName:_noteFontName
size:_smallFontSize];
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:font,
NSFontAttributeName, nil];
NSDictionary *noteAttrs = [NSDictionary
dictionaryWithObjectsAndKeys:noteFont, NSFontAttributeName, [NSParagraphStyle
defaultParagraphStyle], NSParagraphStyleAttributeName, nil];
NSDictionary *noteTextAttrs = [NSDictionary
dictionaryWithObjectsAndKeys:noteTextFont, NSFontAttributeName,
[NSParagraphStyle defaultParagraphStyle], NSParagraphStyleAttributeName, nil];
@@ -117,4 +179,36 @@
return attrString;
}
++ (NSString *)htmlStringWithNotes:(NSArray *)notes;
+{
+ NSMutableString *htmlString = [NSMutableString string];
+ [htmlString appendString:@"<html><head><style type=\"text/css\">"];
+ [htmlString appendString:@"body {font-family:Helvetica} "];
+ [htmlString appendString:@"dd {font-family:LucidaHandwriting-Italic,
Helvetica;font-style:italic} "];
+ [htmlString appendString:@".note-text {font-size:smaller} "];
+ [htmlString appendString:@"</style></head><body><dl>"];
+
+ if (notes) {
+ NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc]
initWithKey:@"pageIndex" ascending:YES] autorelease];
+ NSEnumerator *noteEnum = [[notes sortedArrayUsingDescriptors:[NSArray
arrayWithObject:sortDescriptor]] objectEnumerator];
+ NSDictionary *note;
+ while (note = [noteEnum nextObject]) {
+ NSString *type = [note objectForKey:@"type"];
+ NSString *contents = [note objectForKey:@"contents"];
+ NSString *text = [[note objectForKey:@"text"] string];
+ NSColor *color = [note objectForKey:@"color"];
+ unsigned int pageIndex = [[note objectForKey:@"pageIndex"]
unsignedIntValue];
+ [htmlString appendFormat:@"<dt><img src=\"cid:%@.png\"
style=\"background-color:[EMAIL PROTECTED]" />%@ (page %i)</dt>", type,
hexStringWithColor(color), type, pageIndex+1];
+ [htmlString appendFormat:@"<dd>%@", HTMLEscapeString(contents)];
+ if (text)
+ [htmlString appendFormat:@"<div class=\"note-text\">%@</div>",
HTMLEscapeString(text)];
+ [htmlString appendString:@"</dd>"];
+ }
+ }
+
+ [htmlString appendString:@"</dl></body></html>"];
+
+ return htmlString;
+}
+
@end
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit