Revision: 14584
          http://sourceforge.net/p/skim-app/code/14584
Author:   hofman
Date:     2024-10-26 16:00:29 +0000 (Sat, 26 Oct 2024)
Log Message:
-----------
Add Skim notes properties for Stamp annotation so we can convert it to Note 
annotations. Convert them in skimpdf tool.

Modified Paths:
--------------
    trunk/SkimNotes/PDFAnnotation_SKNExtensions.h
    trunk/SkimNotes/PDFAnnotation_SKNExtensions.m
    trunk/SkimNotes/skimpdf.m

Modified: trunk/SkimNotes/PDFAnnotation_SKNExtensions.h
===================================================================
--- trunk/SkimNotes/PDFAnnotation_SKNExtensions.h       2024-10-26 08:54:09 UTC 
(rev 14583)
+++ trunk/SkimNotes/PDFAnnotation_SKNExtensions.h       2024-10-26 16:00:29 UTC 
(rev 14584)
@@ -62,6 +62,10 @@
 */
 extern NSString *SKNTextString;
 /*!
+    @discussion  Global string for Stamp note type.
+*/
+extern NSString *SKNStampString;
+/*!
     @discussion  Global string for Note note type.
 */
 extern NSString *SKNNoteString;
@@ -420,6 +424,15 @@
 
 /*!
     @abstract    Provides methods to translate between dictionary 
representations of Skim notes and <code>PDFAnnotation</code> objects.
+    @discussion  Implements <code>initSkimNotesWithProperties:</code> and 
properties to take care of the extra properties of a stamp annotation.
+*/
+@interface PDFAnnotationStamp (SKNExtensions)
+@end
+
+#pragma mark -
+
+/*!
+    @abstract    Provides methods to translate between dictionary 
representations of Skim notes and <code>PDFAnnotation</code> objects.
     @discussion  Implements <code>initSkimNotesWithProperties:</code> and 
properties to take care of the extra properties of a text annotation.
 */
 @interface PDFAnnotationInk (SKNExtensions)

Modified: trunk/SkimNotes/PDFAnnotation_SKNExtensions.m
===================================================================
--- trunk/SkimNotes/PDFAnnotation_SKNExtensions.m       2024-10-26 08:54:09 UTC 
(rev 14583)
+++ trunk/SkimNotes/PDFAnnotation_SKNExtensions.m       2024-10-26 16:00:29 UTC 
(rev 14584)
@@ -80,6 +80,7 @@
 
 NSString *SKNFreeTextString = @"FreeText";
 NSString *SKNTextString = @"Text";
+NSString *SKNStampString = @"Stamp";
 NSString *SKNNoteString = @"Note";
 NSString *SKNCircleString = @"Circle";
 NSString *SKNSquareString = @"Square";
@@ -158,6 +159,65 @@
     }
 }
 
+#if defined(PDFKIT_PLATFORM_IOS)
+
+static UIImage *appearanceImageForAnnotation(PDFAnnotation *annotation) {
+    PDFPage *page = [annotation page];
+    if (page == nil)
+        return nil;
+    CGRect rect = [annotation bounds];
+    CGRect bounds = [page boundsForBox:kPDFDisplayBoxCropBox];
+    UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] 
initWithSize:rect.size];
+    UIImage *image = [renderer 
imageWithActions:^(UIGraphicsImageRendererContext *context) {
+        CGContextRef *ctxt = [context CGContext];
+        switch ([page rotation]) {
+            case 0:
+                CGContextTranslateCTM(ctxt, CGRectGetMinX(bounds) - 
CGRectGetMinX(rect), CGRectGetMinY(bounds) - CGRectGetMinY(rect));
+                break;
+            case 90:
+                CGContextTranslateCTM(ctxt, CGRectGetMaxX(bounds) - 
CGRectGetMinX(rect), CGRectGetMinY(bounds) - CGRectGetMinY(rect));
+                break;
+            case 180:
+                CGContextTranslateCTM(ctxt, CGRectGetMaxX(bounds) - 
CGRectGetMinX(rect), CGRectGetMaxY(bounds) - CGRectGetMinY(rect));
+                break;
+            case 270:
+                CGContextTranslateCTM(ctxt, CGRectGetMinX(bounds) - 
CGRectGetMinX(rect), CGRectGetMaxY(bounds) - CGRectGetMinY(rect));
+                break;
+        }
+    }];
+    return image;
+}
+
+#else
+
+static NSImage *appearanceImageForAnnotation(PDFAnnotation *annotation) {
+    PDFPage *page = [annotation page];
+    if (page == nil)
+        return nil;
+    NSRect rect = [annotation bounds];
+    NSRect bounds = [page boundsForBox:kPDFDisplayBoxCropBox];
+    NSImage *image = [[NSImage alloc] initWithSize:rect.size];
+    [image lockFocus];
+    NSAffineTransform *transform = [NSAffineTransform transform];
+    switch ([page rotation]) {
+        case 0:   [transform translateXBy:NSMinX(bounds) - NSMinX(rect) 
yBy:NSMinY(bounds) - NSMinY(rect)]; break;
+        case 90:  [transform translateXBy:NSMaxX(bounds) - NSMinX(rect) 
yBy:NSMinY(bounds) - NSMinY(rect)]; break;
+        case 180: [transform translateXBy:NSMaxX(bounds) - NSMinX(rect) 
yBy:NSMaxY(bounds) - NSMinY(rect)]; break;
+        case 270: [transform translateXBy:NSMinX(bounds) - NSMinX(rect) 
yBy:NSMaxY(bounds) - NSMinY(rect)]; break;
+    }
+    [transform rotateByDegrees:[page rotation]];
+    [transform concat];
+#if !defined(MAC_OS_X_VERSION_10_12) || MAC_OS_X_VERSION_MAX_ALLOWED < 
MAC_OS_X_VERSION_10_12
+    if ([annotation respondsToSelector:@selector(drawWithBox:inContext:)] == 
NO)
+        [annotation drawWithBox:kPDFDisplayBoxCropBox];
+    else
+#endif
+        [annotation drawWithBox:kPDFDisplayBoxCropBox 
inContext:[[NSGraphicsContext currentContext] CGContext]];
+    [image unlockFocus];
+    return image;
+}
+#endif
+
 #if defined(PDFKIT_PLATFORM_IOS) || (defined(MAC_OS_X_VERSION_10_11) && 
MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_11)
 
 static inline NSInteger SKNAlignmentFromTextAlignment(NSTextAlignment 
alignment) {
@@ -185,6 +245,7 @@
 #if !defined(MAC_OS_X_VERSION_10_12) || MAC_OS_X_VERSION_MAX_ALLOWED < 
MAC_OS_X_VERSION_10_12
 @interface PDFAnnotation (SKNSierraDeclarations)
 - (id)valueForAnnotationKey:(NSString *)key;
+- (void)drawWithBox:(PDFDisplayBox)box inContext:(CGContextRef)context;
 @end
 #endif
 
@@ -208,7 +269,7 @@
 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
     if ([type isKindOfClass:[NSString class]] == NO)
         return Nil;
-    else if ([type isEqualToString:SKNNoteString] || [type 
isEqualToString:SKNTextString])
+    else if ([type isEqualToString:SKNNoteString] || [type 
isEqualToString:SKNTextString] || [type isEqualToString:SKNStampString])
         return [SKNPDFAnnotationNote class];
     else if ([type isEqualToString:SKNFreeTextString])
         return [PDFAnnotationFreeText class];
@@ -234,7 +295,7 @@
     
 #if defined(PDFKIT_PLATFORM_IOS)
     
-    if ([type isEqualToString:SKNNoteString] || [type 
isEqualToString:SKNTextString]) {
+    if ([type isEqualToString:SKNNoteString] || [type 
isEqualToString:SKNTextString] || [type isEqualToString:SKNStampString]) {
         if ([self isMemberOfClass:[PDFAnnotation class]]) {
             // replace by our subclass
             self = [self init];
@@ -315,7 +376,7 @@
         
 #if defined(PDFKIT_PLATFORM_IOS)
     
-    if (([type isEqualToString:SKNNoteString] || [type 
isEqualToString:SKNTextString]) && [self isMemberOfClass:[PDFAnnotation 
class]]) {
+    if (([type isEqualToString:SKNNoteString] || [type 
isEqualToString:SKNTextString] || [type isEqualToString:SKNStampString]) && 
[self isMemberOfClass:[PDFAnnotation class]]) {
         // replace by our subclass
         self = [self init];
         self = [[SKNPDFAnnotationNote alloc] initSkimNoteWithProperties:dict];
@@ -437,7 +498,7 @@
             [self setQuadrilateralPoints:pointValues];
         }
         
-        if ([type isEqualToString:SKNTextString] || [type 
isEqualToString:SKNNoteString]) {
+        if ([type isEqualToString:SKNTextString] || [type 
isEqualToString:SKNStampString] || [type isEqualToString:SKNNoteString]) {
             NSNumber *iconType = [dict 
objectForKey:SKNPDFAnnotationIconTypeKey];
             if ([iconType respondsToSelector:@selector(integerValue)])
                 [self setIconType:[iconType integerValue]];
@@ -613,6 +674,14 @@
                 [dict setValue:[NSNumber 
numberWithInteger:SKNIconTypeFromAnnotationValue(value)] 
forKey:SKNPDFAnnotationIconTypeKey];
         }
             
+        if ([type isEqualToString:SKNStampString]) {
+            if ([self respondsToSelector:@selector(iconType)])
+                [dict setValue:[NSNumber numberWithInteger:[(id)self 
iconType]] forKey:SKNPDFAnnotationIconTypeKey];
+            else if ((value = [self valueForAnnotationKey:@"/Name"]))
+                [dict setValue:[NSNumber 
numberWithInteger:SKNIconTypeFromAnnotationValue(value)] 
forKey:SKNPDFAnnotationIconTypeKey];
+            [dict setValue:appearanceImageForAnnotation(self) 
forKey:SKNPDFAnnotationImageKey];
+        }
+            
         if ([type isEqualToString:SKNLineString]) {
             if ([self respondsToSelector:@selector(startLineStyle)] && [self 
respondsToSelector:@selector(endLineStyle)]) {
                 [dict setValue:[NSNumber numberWithInteger:[(id)self 
startLineStyle]] forKey:SKNPDFAnnotationStartLineStyleKey];
@@ -1249,6 +1318,37 @@
 
 #pragma mark -
 
+@implementation PDFAnnotationStamp (SKNExtensions)
+
+- (id)initSkimNoteWithProperties:(NSDictionary *)dict{
+    self = [super initSkimNoteWithProperties:dict];
+    if (self) {
+        NSNumber *iconType = [dict objectForKey:SKNPDFAnnotationIconTypeKey];
+        if ([iconType respondsToSelector:@selector(integerValue)] && [self 
respondsToSelector:@selector(setIconType:)])
+            [self setIconType:[iconType integerValue]];
+    }
+    return self;
+}
+
+- (NSDictionary *)SkimNoteProperties{
+    NSMutableDictionary *dict = [self genericSkimNoteProperties];
+#if !defined(MAC_OS_X_VERSION_10_12) || MAC_OS_X_VERSION_MAX_ALLOWED < 
MAC_OS_X_VERSION_10_12
+    if ([self respondsToSelector:@selector(iconType)] == NO) {
+        NSString *name = [self name];
+        if (name && [name hasPrefix:@"/"] == NO)
+            name = [@"/" stringByAppendingString:name];
+        [dict setValue:[NSNumber 
numberWithInteger:SKNIconTypeFromAnnotationValue(name)] 
forKey:SKNPDFAnnotationIconTypeKey];
+    } else
+#endif
+        [dict setValue:[NSNumber numberWithInteger:[self iconType]] 
forKey:SKNPDFAnnotationIconTypeKey];
+    [dict setValue:appearanceImageForAnnotation(self) 
forKey:SKNPDFAnnotationImageKey];
+    return dict;
+}
+
+@end
+
+#pragma mark -
+
 @implementation PDFAnnotationInk (SKNExtensions)
 
 - (id)initSkimNoteWithProperties:(NSDictionary *)dict{

Modified: trunk/SkimNotes/skimpdf.m
===================================================================
--- trunk/SkimNotes/skimpdf.m   2024-10-26 08:54:09 UTC (rev 14583)
+++ trunk/SkimNotes/skimpdf.m   2024-10-26 16:00:29 UTC (rev 14584)
@@ -367,7 +367,7 @@
                 NSArray *inNotes = [fm 
readSkimNotesFromExtendedAttributesAtURL:inURL error:NULL];
                 NSMutableArray *notes = [NSMutableArray 
arrayWithArray:inNotes];
                 NSUInteger i, iMax = [pdfDoc pageCount];
-                NSSet *convertibleTypes = [NSSet 
setWithObjects:SKNFreeTextString, SKNTextString, SKNNoteString, 
SKNCircleString, SKNSquareString, SKNMarkUpString, SKNHighlightString, 
SKNUnderlineString, SKNStrikeOutString, SKNLineString, SKNInkString, nil];
+                NSSet *convertibleTypes = [NSSet 
setWithObjects:SKNFreeTextString, SKNTextString, SKNStampString, SKNNoteString, 
SKNCircleString, SKNSquareString, SKNMarkUpString, SKNHighlightString, 
SKNUnderlineString, SKNStrikeOutString, SKNLineString, SKNInkString, nil];
                 
                 for (i = 0; i < iMax; i++) {
                     PDFPage *page = [pdfDoc pageAtIndex:i];
@@ -397,6 +397,23 @@
                                     }
                                 }
                                 note = mutableNote;
+                            } else if ([[annotation type] 
isEqualToString:SKNStampString]) {
+                                NSMutableDictionary *mutableNote = [note 
mutableCopy];
+                                NSString *contents = [note 
objectForKey:SKNPDFAnnotationContentsKey];
+                                [mutableNote setObject:SKNNoteString 
forKey:SKNPDFAnnotationTypeKey];
+                                [mutableNote setObject:[NSNumber 
numberWithBool:YES] forKey:SKNPDFAnnotationDrawsImageKey];
+                                if (contents) {
+                                    NSRange r = [contents rangeOfString:@"  "];
+                                    NSRange r1 = [contents 
rangeOfString:@"\n"];
+                                    if (r1.location < r.location)
+                                        r = r1;
+                                    if (NSMaxRange(r) < [contents length]) {
+                                        NSAttributedString *attrString = 
[[NSAttributedString alloc] initWithString:[contents 
substringFromIndex:NSMaxRange(r)]];
+                                        [mutableNote setObject:attrString 
forKey:SKNPDFAnnotationTextKey];
+                                        [mutableNote setObject:[contents 
substringToIndex:r.location] forKey:SKNPDFAnnotationContentsKey];
+                                    }
+                                }
+                                note = mutableNote;
                             }
                             if (NSEqualPoints(pageOrigin, NSZeroPoint) == NO) {
                                 NSMutableDictionary *mutableNote = [note 
mutableCopy];

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to