Guest User

Untitled

a guest
Jul 12th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // JWTrieViewer.h
  2. #import <Foundation/Foundation.h>
  3.  
  4. @class JWTrie;
  5.  
  6. @interface JWTrieViewer : NSObject {
  7.     CGContextRef pdfContext;
  8. }
  9.  
  10. @property (retain, readwrite) JWTrie* trie;
  11. @property (retain, readwrite) NSMutableDictionary* indentLevels;
  12.  
  13. -(void)dump:(JWTrie*)trie;
  14. -(void)drawNodes:(NSDictionary*)children depth:(NSInteger)depth point:(NSPoint)parentPoint;
  15. @end
  16.  
  17.  
  18.  
  19. // JWTrieViewer.m
  20. import "JWTrieViewer.h"
  21. #import "JWTrie.h"
  22. #import "JWNode.h"
  23.  
  24. @implementation JWTrieViewer
  25.  
  26. @synthesize trie;
  27. @synthesize indentLevels;
  28.  
  29.  
  30. -(void)dump:(JWTrie*)aTrie
  31. {
  32.     self.trie = aTrie;
  33.     indentLevels = [NSMutableDictionary dictionary];
  34.    
  35.     CFStringRef path;
  36.     CFURLRef url;
  37.    
  38.     path = CFStringCreateWithCString (NULL, "/Users/james/Desktop/trie.pdf", kCFStringEncodingUTF8);
  39.     url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);
  40.     CFRelease (path);
  41.     CGRect r = CGRectMake(0, 0, 600, 300);
  42.     NSPoint p = NSMakePoint(300, 0);
  43.    
  44.     pdfContext = CGPDFContextCreateWithURL(url, &r, NULL);
  45.     CFRelease(url);
  46.    
  47.     CGPDFContextBeginPage (pdfContext, NULL);
  48.     CGContextSelectFont(pdfContext, "Helvetica", 24, kCGEncodingMacRoman);
  49.     [self drawNodes:trie.node.nodes depth:1 point:p];
  50.     CGPDFContextEndPage (pdfContext);
  51.     CGContextRelease (pdfContext);
  52. }
  53.  
  54. -(void)drawNodes:(NSDictionary*)children depth:(NSInteger)depth point:(NSPoint)parentPoint
  55. {
  56.     NSInteger indent = 5;
  57.     NSNumber* n = [indentLevels objectForKey:[NSNumber numberWithInteger:depth]];
  58.     if(n) {
  59.         indent = [n intValue];
  60.     }
  61.    
  62.     CGFloat stepX = 50;
  63.     CGFloat stepY = 50;
  64.  
  65.     for (NSString* str in children) {
  66.         NSPoint p = NSMakePoint(stepX * indent, stepY * depth);
  67.         NSPoint points[2] = {parentPoint, p};
  68.        
  69.         JWNode* n = [children objectForKey:str];
  70.         CGContextSetTextPosition(pdfContext, p.x, p.y);
  71.         CGContextShowText(pdfContext, [str UTF8String], str.length);
  72.         CGContextStrokeLineSegments(pdfContext, points, 2);
  73.         CGRect bound = CGRectMake(p.x - 7.5, p.y - 7.5, 30, 30);
  74.         if(n.isWord) {
  75.             CGContextStrokeRect(pdfContext, bound);
  76.         } else {
  77.             CGContextStrokeEllipseInRect(pdfContext, bound);
  78.            
  79.         }
  80.         ++indent;
  81.         [indentLevels setObject:[NSNumber numberWithInteger:indent] forKey:[NSNumber numberWithInteger:depth]];
  82.  
  83.         [self drawNodes:n.nodes depth:depth + 1 point:p];
  84.     }
  85. }
  86. @end
Add Comment
Please, Sign In to add comment