Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/ui/ui-drv/cocoa/grlib_cocoa.m
blob: 3fedb0c647ebed205412a8886b574c11c08f669a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <config.h>
#ifdef PLATFORM_TEXT_RENDERING

#import <Cocoa/Cocoa.h>

#include <fconfig.h>
#include <filter.h>
#include <fractal.h>
#include <ui_helper.h>
#include <grlib.h>

NSMutableDictionary *textAttributes(int fgcolor, int bgcolor) {
    
    float red, green, blue;
    
    NSMutableDictionary *attrsDictionary = [NSMutableDictionary 
					    dictionaryWithCapacity:5];
    
    [attrsDictionary setValue:[NSColor whiteColor] 
		       forKey:NSForegroundColorAttributeName];
    
    [attrsDictionary setValue:[NSFont 
			       boldSystemFontOfSize:[NSFont systemFontSize]] 
		       forKey:NSFontAttributeName];
    
    //NSLog(@"%x", fgcolor);
    red   = (fgcolor & RMASK) / 255.0;
    green = (fgcolor & GMASK) / 255.0;
    blue  = (fgcolor & BMASK) / 255.0;
    [attrsDictionary setValue:[NSColor colorWithDeviceRed:red 
						    green:green 
						     blue:blue 
						    alpha:1.0] 
		       forKey:NSForegroundColorAttributeName];
    
    red   = (bgcolor & RMASK) / 255.0;
    green = (bgcolor & GMASK) / 255.0;
    blue  = (bgcolor & BMASK) / 255.0;
    NSShadow *textShadow = [[NSShadow alloc] init];
    [textShadow setShadowColor:[NSColor colorWithDeviceRed:red 
						     green:green 
						      blue:blue 
						     alpha:1.0]];
    [textShadow setShadowOffset:NSMakeSize(2, -2)];
    [textShadow setShadowBlurRadius:1];
    
    [attrsDictionary setValue:textShadow forKey:NSShadowAttributeName];
    [textShadow autorelease];
    
    return attrsDictionary;
}


int
xprint(struct image *image, CONST struct xfont *current, int x, int y,
       CONST char *text, int fgcolor, int bgcolor, int mode)
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] 
				  initWithBitmapDataPlanes:image->currlines
				  pixelsWide:image->width
				  pixelsHigh:image->height
				  bitsPerSample:8
				  samplesPerPixel:3
				  hasAlpha:NO
				  isPlanar:NO
				  colorSpaceName:NSDeviceRGBColorSpace
				  bytesPerRow:0
				  bitsPerPixel:32];
    
    [NSGraphicsContext saveGraphicsState];
    NSGraphicsContext *context = [NSGraphicsContext 
				  graphicsContextWithBitmapImageRep:imageRep];
    [NSGraphicsContext setCurrentContext:context];
    
    NSString *messageText = [[[NSString stringWithUTF8String:text] 
			      componentsSeparatedByString:@"\n"] 
			     objectAtIndex:0];
    //NSLog(messageText);
    
    NSMutableDictionary *attrsDictionary = textAttributes(fgcolor, bgcolor);
    
    NSSize textSize = [messageText sizeWithAttributes:attrsDictionary];
    [messageText drawAtPoint:NSMakePoint(x, image->height - y - textSize.height) 
	      withAttributes:attrsDictionary];
    
    [NSGraphicsContext restoreGraphicsState];
    
    /* 
     * The calling functions expect the return value to be the number of
     * bytes in the string, not the number of characters (this is not always
     * equal for UTF-8 strings).  Therefore, we don't use [messageText length].
     */
    int bytesUsed = [messageText 
			 lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
    
    [imageRep release];
    [pool release];
    
    return bytesUsed;
}

int xtextwidth(CONST struct xfont *font, CONST char *text)
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    NSString *messageText = [[[NSString stringWithUTF8String:text] 
			      componentsSeparatedByString:@"\n"] 
			     objectAtIndex:0];
    
    NSMutableDictionary *attrsDictionary = textAttributes(0, 0);
    
    NSSize textSize = [messageText sizeWithAttributes:attrsDictionary];
    
    [pool release];
    return ceil(textSize.width) + 2;
}

int xtextheight(CONST struct xfont *font) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    NSString *messageText = @"Test String";
    
    NSMutableDictionary *attrsDictionary = textAttributes(0, 0);
    NSSize textSize = [messageText sizeWithAttributes:attrsDictionary];
    [pool release];
    return ceil(textSize.height) + 2;
}

int xtextcharw(CONST struct xfont *font, CONST char c)
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    NSString *messageText = [NSString stringWithFormat:@"%c", c];
    
    NSMutableDictionary *attrsDictionary = textAttributes(0, 0);
    NSSize textSize = [messageText sizeWithAttributes:attrsDictionary];
    [pool release];
    return ceil(textSize.width) + 2;
}
#endif