File: HFTextVisualStyleRun.m

package info (click to toggle)
sameboy 1.0.2%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 10,632 kB
  • sloc: ansic: 29,954; objc: 22,249; asm: 1,424; pascal: 1,373; makefile: 1,064; xml: 111
file content (80 lines) | stat: -rw-r--r-- 2,812 bytes parent folder | download | duplicates (2)
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
//
//  HFTextVisualStyleRun.m
//  HexFiend_2
//
//  Copyright 2009 ridiculous_fish. All rights reserved.
//

#import "HFTextVisualStyleRun.h"


@implementation HFTextVisualStyleRun

- (instancetype)init {
    self = [super init];
    _scale = 1.;
    _shouldDraw = YES;
    return self;
}

- (void)dealloc {
    [_foregroundColor release];
    [_backgroundColor release];
    [_bookmarkStarts release];
    [_bookmarkExtents release];
    [_bookmarkEnds release];
    [super dealloc];
}

- (void)set {
    [_foregroundColor set];
    if (_scale != (CGFloat)1.0) {
        CGContextRef ctx = [[NSGraphicsContext currentContext] graphicsPort];
        CGAffineTransform tm = CGContextGetTextMatrix(ctx);
        /* Huge hack - adjust downward a little bit if we are scaling */
        tm = CGAffineTransformTranslate(tm, 0, -.25 * (_scale - 1));
        tm = CGAffineTransformScale(tm, _scale, _scale);
        CGContextSetTextMatrix(ctx, tm);
    }
}

static inline NSUInteger flip(NSUInteger x) {
    return _Generic(x, unsigned: NSSwapInt, unsigned long: NSSwapLong, unsigned long long: NSSwapLongLong)(x);
}
static inline NSUInteger rol(NSUInteger x, unsigned char r) {
    r %= sizeof(NSUInteger)*8;
    return (x << r) | (x << (sizeof(NSUInteger)*8 - r));
}
- (NSUInteger)hash {
    NSUInteger A = 0;
    // All these hashes tend to have only low bits, except the double which has only high bits.
#define Q(x, r) rol(x, sizeof(NSUInteger)*r/6)
    A ^= flip([_foregroundColor hash] ^ Q([_backgroundColor hash], 2)); // skew high
    A ^= Q(_range.length ^ flip(_range.location), 2); // skew low
    A ^= flip([_bookmarkStarts hash]) ^ Q([_bookmarkEnds hash], 3) ^ Q([_bookmarkExtents hash], 4); // skew high
    A ^= _shouldDraw ? 0 : (NSUInteger)-1;
    A ^= *(NSUInteger*)&_scale; // skew high
    return A;
#undef Q
}

- (BOOL)isEqual:(HFTextVisualStyleRun *)run {
    if(![run isKindOfClass:[self class]]) return NO;
    /* Check each field for equality. */
    if(!NSEqualRanges(_range, run->_range)) return NO;
    if(_scale != run->_scale) return NO;
    if(_shouldDraw != run->_shouldDraw) return NO;
    if(!!_foregroundColor != !!run->_foregroundColor) return NO;
    if(!!_backgroundColor != !!run->_backgroundColor) return NO;
    if(!!_bookmarkStarts  != !!run->_bookmarkStarts)  return NO;
    if(!!_bookmarkExtents != !!run->_bookmarkExtents) return NO;
    if(!!_bookmarkEnds    != !!run->_bookmarkEnds)    return NO;
    if(![_foregroundColor isEqual: run->_foregroundColor]) return NO;
    if(![_backgroundColor isEqual: run->_backgroundColor]) return NO;
    if(![_bookmarkStarts  isEqual: run->_bookmarkStarts])  return NO;
    if(![_bookmarkExtents isEqual: run->_bookmarkExtents]) return NO;
    if(![_bookmarkEnds    isEqual: run->_bookmarkEnds])    return NO;
    return YES;
}

@end