Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // JWTrieViewer.h
- #import <Foundation/Foundation.h>
- @class JWTrie;
- @interface JWTrieViewer : NSObject {
- CGContextRef pdfContext;
- }
- @property (retain, readwrite) JWTrie* trie;
- @property (retain, readwrite) NSMutableDictionary* indentLevels;
- -(void)dump:(JWTrie*)trie;
- -(void)drawNodes:(NSDictionary*)children depth:(NSInteger)depth point:(NSPoint)parentPoint;
- @end
- // JWTrieViewer.m
- import "JWTrieViewer.h"
- #import "JWTrie.h"
- #import "JWNode.h"
- @implementation JWTrieViewer
- @synthesize trie;
- @synthesize indentLevels;
- -(void)dump:(JWTrie*)aTrie
- {
- self.trie = aTrie;
- indentLevels = [NSMutableDictionary dictionary];
- CFStringRef path;
- CFURLRef url;
- path = CFStringCreateWithCString (NULL, "/Users/james/Desktop/trie.pdf", kCFStringEncodingUTF8);
- url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);
- CFRelease (path);
- CGRect r = CGRectMake(0, 0, 600, 300);
- NSPoint p = NSMakePoint(300, 0);
- pdfContext = CGPDFContextCreateWithURL(url, &r, NULL);
- CFRelease(url);
- CGPDFContextBeginPage (pdfContext, NULL);
- CGContextSelectFont(pdfContext, "Helvetica", 24, kCGEncodingMacRoman);
- [self drawNodes:trie.node.nodes depth:1 point:p];
- CGPDFContextEndPage (pdfContext);
- CGContextRelease (pdfContext);
- }
- -(void)drawNodes:(NSDictionary*)children depth:(NSInteger)depth point:(NSPoint)parentPoint
- {
- NSInteger indent = 5;
- NSNumber* n = [indentLevels objectForKey:[NSNumber numberWithInteger:depth]];
- if(n) {
- indent = [n intValue];
- }
- CGFloat stepX = 50;
- CGFloat stepY = 50;
- for (NSString* str in children) {
- NSPoint p = NSMakePoint(stepX * indent, stepY * depth);
- NSPoint points[2] = {parentPoint, p};
- JWNode* n = [children objectForKey:str];
- CGContextSetTextPosition(pdfContext, p.x, p.y);
- CGContextShowText(pdfContext, [str UTF8String], str.length);
- CGContextStrokeLineSegments(pdfContext, points, 2);
- CGRect bound = CGRectMake(p.x - 7.5, p.y - 7.5, 30, 30);
- if(n.isWord) {
- CGContextStrokeRect(pdfContext, bound);
- } else {
- CGContextStrokeEllipseInRect(pdfContext, bound);
- }
- ++indent;
- [indentLevels setObject:[NSNumber numberWithInteger:indent] forKey:[NSNumber numberWithInteger:depth]];
- [self drawNodes:n.nodes depth:depth + 1 point:p];
- }
- }
- @end
Add Comment
Please, Sign In to add comment