summaryrefslogtreecommitdiff
path: root/tikzit/src/gtk/Node+Render.m
blob: 907d818851153c4fb3f3824cf8f3fa65adde162c (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * Copyright 2011  Alex Merry <dev@randomguy3.me.uk>
 * Copyright 2010  Chris Heunen
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#import "Node+Render.h"
#import "Shape.h"
#import "Shape+Render.h"
#import "ShapeNames.h"

#define MAX_LABEL_LENGTH 10
#define LABEL_PADDING_X 2
#define LABEL_PADDING_Y 2

@implementation Node (Render)

- (Shape*) shapeToRender {
    if (style) {
        return [Shape shapeForName:[style shapeName]];
    } else {
        return [Shape shapeForName:SHAPE_CIRCLE];
    }
}

- (Transformer*) shapeTransformerForSurface:(id<Surface>)surface {
    return [self shapeTransformerFromTransformer:[surface transformer]];
}

- (NSRect) renderBoundsUsingShapeTransform:(Transformer*)shapeTrans {
    float strokeThickness = style ? [style strokeThickness] : [NodeStyle defaultStrokeThickness];
    NSRect screenBounds = [shapeTrans rectToScreen:[[self shapeToRender] boundingRect]];
    screenBounds = NSInsetRect(screenBounds, -strokeThickness, -strokeThickness);
    return screenBounds;
}

- (NSRect) renderBoundsForSurface:(id<Surface>)surface {
    return [self renderBoundsUsingShapeTransform:[self shapeTransformerForSurface:surface]];
}

- (NSRect) renderBoundsWithLabelForSurface:(id<Surface>)surface {
    NSRect nodeBounds = [self renderBoundsForSurface:surface];
    NSRect labelRect = NSZeroRect;
    if (![label isEqual:@""]) {
        id<RenderContext> cr = [surface createRenderContext];
        labelRect.size = [self renderedLabelSizeInContext:cr];
        NSPoint nodePos = [[surface transformer] toScreen:point];
        labelRect.origin.x = nodePos.x - (labelRect.size.width / 2);
        labelRect.origin.y = nodePos.y - (labelRect.size.height / 2);
    }
    return NSUnionRect(nodeBounds, labelRect);
}

- (RColor) strokeColor {
    if (style) {
        return [[style strokeColorRGB] rColor];
    } else {
        return MakeRColor (0.4, 0.4, 0.7, 0.8);
    }
}

- (RColor) fillColor {
    if (style) {
        return [[style fillColorRGB] rColor];
    } else {
        return MakeRColor (0.4, 0.4, 0.7, 0.3);
    }
}

- (NSString*) renderedLabel {
    NSString *r_label = [label stringByExpandingLatexConstants];
    if ([r_label length] > MAX_LABEL_LENGTH) {
        r_label = [[[r_label substringToIndex:MAX_LABEL_LENGTH-1] stringByTrimmingSpaces] stringByAppendingString:@"..."];
    } else {
        r_label = [r_label stringByTrimmingSpaces];
    }
    return r_label;
}

- (NSSize) renderedLabelSizeInContext:(id<RenderContext>)context {
    NSSize result = {0, 0};
    if (![label isEqual:@""]) {
        NSString *r_label = [self renderedLabel];

        id<TextLayout> layout = [context layoutText:r_label withSize:9];

        result = [layout size];
        result.width += LABEL_PADDING_X;
        result.height += LABEL_PADDING_Y;
    }
    return result;
}

- (void) renderLabelToSurface:(id <Surface>)surface withContext:(id<RenderContext>)context {
    [self renderLabelAt:[[surface transformer] toScreen:point] withContext:context];
}

- (void) renderLabelAt:(NSPoint)p withContext:(id<RenderContext>)context {
    // draw latex code overlayed on node
    if (![label isEqual:@""]) {
        [context saveState];

        NSString *r_label = [self renderedLabel];
        id<TextLayout> layout = [context layoutText:r_label withSize:9];

        NSSize labelSize = [layout size];

        NSRect textBounds = NSMakeRect (p.x - labelSize.width/2,
                p.y - labelSize.height/2,
                labelSize.width,
                labelSize.height);
        NSRect backRect = NSInsetRect (textBounds, -LABEL_PADDING_X, -LABEL_PADDING_Y);

        [context startPath];
        [context setLineWidth:1.0];
        [context rect:backRect];
        RColor fColor = MakeRColor (1.0, 1.0, 0.5, 0.7);
        RColor sColor = MakeRColor (0.5, 0.0, 0.0, 0.7);
        [context strokePathWithColor:sColor andFillWithColor:fColor];

        [layout showTextAt:textBounds.origin withColor:BlackRColor];

        [context restoreState];
    }
}

- (void) renderToSurface:(id <Surface>)surface withContext:(id<RenderContext>)context state:(enum NodeState)state {
    Transformer *shapeTrans = [self shapeTransformerForSurface:surface];
    float strokeThickness = style ? [style strokeThickness] : [NodeStyle defaultStrokeThickness];

    [context saveState];

    [[self shapeToRender] drawPathWithTransform:shapeTrans andContext:context];

    [context setLineWidth:strokeThickness];
    if (!style) {
        [context setLineDash:3.0];
    }
    [context strokePathWithColor:[self strokeColor] andFillWithColor:[self fillColor]];

    if (state != NodeNormal) {
        [context setLineWidth:strokeThickness + 4.0];
        [context setLineDash:0.0];
        float alpha = 0.0f;
        if (state == NodeSelected)
            alpha = 0.5f;
        else if (state == NodeHighlighted)
            alpha = 0.25f;
        RColor selectionColor = MakeSolidRColor(0.61f, 0.735f, 1.0f);

        [[self shapeToRender] drawPathWithTransform:shapeTrans andContext:context];
        [context strokePathWithColor:selectionColor andFillWithColor:selectionColor usingAlpha:alpha];
    }

    [context restoreState];
    [self renderLabelToSurface:surface withContext:context];
}

- (BOOL) hitByPoint:(NSPoint)p onSurface:(id<Surface>)surface {
    Transformer *shapeTrans = [self shapeTransformerForSurface:surface];

    NSRect screenBounds = [self renderBoundsUsingShapeTransform:shapeTrans];
    if (!NSPointInRect(p, screenBounds)) {
        return NO;
    }

    float strokeThickness = style ? [style strokeThickness] : [NodeStyle defaultStrokeThickness];
    id<RenderContext> ctx = [surface createRenderContext];
    [ctx setLineWidth:strokeThickness];
    [[self shapeToRender] drawPathWithTransform:shapeTrans andContext:ctx];
    return [ctx strokeIncludesPoint:p] || [ctx fillIncludesPoint:p];
}

@end

// vim:ft=objc:ts=8:et:sts=4:sw=4