PageRenderTime 25ms CodeModel.GetById 16ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/AppKit/CPGraphics.j

http://github.com/cacaodev/cappuccino
Unknown | 121 lines | 102 code | 19 blank | 0 comment | 0 complexity | e78c47a6b9e8e1480d4a0975c51efb48 MD5 | raw file
  1/*
  2 * CPGraphics.j
  3 * AppKit
  4 *
  5 * Created by Francisco Tolmasky.
  6 * Copyright 2010, 280 North, Inc.
  7 *
  8 * This library is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU Lesser General Public
 10 * License as published by the Free Software Foundation; either
 11 * version 2.1 of the License, or (at your option) any later version.
 12 *
 13 * This library is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 16 * Lesser General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU Lesser General Public
 19 * License along with this library; if not, write to the Free Software
 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 21 */
 22
 23@import "CPColor.j"
 24@import "CPGraphicsContext.j"
 25
 26CPCalibratedWhiteColorSpace = @"CalibratedWhiteColorSpace";
 27CPCalibratedBlackColorSpace = @"CalibratedBlackColorSpace";
 28CPCalibratedRGBColorSpace   = @"CalibratedRGBColorSpace";
 29CPDeviceWhiteColorSpace     = @"DeviceWhiteColorSpace";
 30CPDeviceBlackColorSpace     = @"DeviceBlackColorSpace";
 31CPDeviceRGBColorSpace       = @"DeviceRGBColorSpace";
 32CPDeviceCMYKColorSpace      = @"DeviceCMYKColorSpace";
 33CPNamedColorSpace           = @"NamedColorSpace";
 34CPPatternColorSpace         = @"PatternColorSpace";
 35CPCustomColorSpace          = @"CustomColorSpace";
 36
 37function CPDrawTiledRects(
 38   /* CGRect */ boundsRect,
 39   /* CGRect */ clipRect,
 40   /* CPRectEdge[] */ sides,
 41   /* float[] */ grays)
 42{
 43    if (sides.length != grays.length)
 44        [CPException raise:CPInvalidArgumentException reason:@"sides (length: " + sides.length + ") and grays (length: " + grays.length + ") must have the same length."];
 45
 46    var colors = [];
 47
 48    for (var i = 0; i < grays.length; ++i)
 49        colors.push([CPColor colorWithCalibratedWhite:grays[i] alpha:1.0]);
 50
 51    return CPDrawColorTiledRects(boundsRect, clipRect, sides, colors);
 52}
 53
 54function CPDrawColorTiledRects(
 55   /* CGRect */ boundsRect,
 56   /* CGRect */ clipRect,
 57   /* CPRectEdge[] */ sides,
 58   /* CPColor[] */ colors)
 59{
 60    if (sides.length != colors.length)
 61        [CPException raise:CPInvalidArgumentException reason:@"sides (length: " + sides.length + ") and colors (length: " + colors.length + ") must have the same length."];
 62
 63    var resultRect = CGRectMakeCopy(boundsRect),
 64        slice = CGRectMakeZero(),
 65        remainder = CGRectMakeZero(),
 66        context = [[CPGraphicsContext currentContext] graphicsPort];
 67
 68    CGContextSaveGState(context);
 69    CGContextSetLineWidth(context, 1.0);
 70
 71    for (var sideIndex = 0; sideIndex < sides.length; ++sideIndex)
 72    {
 73        var side = sides[sideIndex];
 74
 75        CGRectDivide(resultRect, slice, remainder, 1.0, side);
 76        resultRect = remainder;
 77        slice = CGRectIntersection(slice, clipRect);
 78
 79        // Cocoa docs say that only slices that are within the clipRect are actually drawn
 80        if (CGRectIsEmpty(slice))
 81            continue;
 82
 83        var minX,
 84            maxX,
 85            minY,
 86            maxY;
 87
 88        if (side == CPMinXEdge || side == CPMaxXEdge)
 89        {
 90            // Make sure we have at least 1 pixel to draw a line
 91            if (CGRectGetWidth(slice) < 1.0)
 92                continue;
 93
 94            minX = CGRectGetMinX(slice) + 0.5;
 95            maxX = minX;
 96            minY = CGRectGetMinY(slice);
 97            maxY = CGRectGetMaxY(slice);
 98        }
 99        else // CPMinYEdge || CPMaxYEdge
100        {
101            // Make sure we have at least 1 pixel to draw a line
102            if (CGRectGetHeight(slice) < 1.0)
103                continue;
104
105            minX = CGRectGetMinX(slice);
106            maxX = CGRectGetMaxX(slice);
107            minY = CGRectGetMinY(slice) + 0.5;
108            maxY = minY;
109        }
110
111        CGContextBeginPath(context);
112        CGContextMoveToPoint(context, minX, minY);
113        CGContextAddLineToPoint(context, maxX, maxY);
114        CGContextSetStrokeColor(context, colors[sideIndex]);
115        CGContextStrokePath(context);
116    }
117
118    CGContextRestoreGState(context);
119
120    return resultRect;
121}