mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-08-25 13:07:02 +00:00
Previously, Cappuccino was using preprocessor macros internally for the CGPoint/Size/Rect/Inset/Affine functions, as well as for CPRange. These macros had the same name as the corresponding function, but began with _. The functions were actually defined using the macros.
The motivation behind using macros was to increase performance by reducing function calls. However, there were a number of problems with this approach:
- There was an artificial dichotomy between _CG macros and the corresponding CG functions. We never completely replaced CG function calls with _CG macros. In fact, they were often mixed up in the same file. There was an extra burden on the programmer to remember to use the macro instead of the function.
- If a method call was passed as an argument to a macro, performance could actually be significantly *worse* than a function call. For example, _CGGetRectMakeCopy([view frame]) would expand to `{ origin:{ x:[view frame].origin.x, y:[view frame].origin.y }, size:{ width:[view frame].size.width, height:[view frame].size.height } }`. So instead of a single objj_msgSend and a single simple function call, we ended up with 4 objj_msgSend calls, which are way more expensive than simple function calls.
- Because of this expansion problem, to use macros efficiently required us to remember to use variables for all macro parameters. This didn't happen, and shouldn't have to happen.
- Finally, with modern Javascript engines, function call overhead is so small that it really isn't worth using the macros.
This commit eliminates the _CGGeometry, CGAffineTransformation and CPRange macros and replaces them with function calls.
BREAKING CHANGE:
The macros are no longer available. They could only be used with compiled code, but if there is any user code that used them, they will have to be replaced with the corresponding functions.
122 lines
4.1 KiB
Plaintext
122 lines
4.1 KiB
Plaintext
/*
|
|
* CPGraphics.j
|
|
* AppKit
|
|
*
|
|
* Created by Francisco Tolmasky.
|
|
* Copyright 2010, 280 North, Inc.
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
* License as published by the Free Software Foundation; either
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
*
|
|
* This library 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
|
|
* Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with this library; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
*/
|
|
|
|
@import "CPColor.j"
|
|
@import "CPGraphicsContext.j"
|
|
|
|
CPCalibratedWhiteColorSpace = @"CalibratedWhiteColorSpace";
|
|
CPCalibratedBlackColorSpace = @"CalibratedBlackColorSpace";
|
|
CPCalibratedRGBColorSpace = @"CalibratedRGBColorSpace";
|
|
CPDeviceWhiteColorSpace = @"DeviceWhiteColorSpace";
|
|
CPDeviceBlackColorSpace = @"DeviceBlackColorSpace";
|
|
CPDeviceRGBColorSpace = @"DeviceRGBColorSpace";
|
|
CPDeviceCMYKColorSpace = @"DeviceCMYKColorSpace";
|
|
CPNamedColorSpace = @"NamedColorSpace";
|
|
CPPatternColorSpace = @"PatternColorSpace";
|
|
CPCustomColorSpace = @"CustomColorSpace";
|
|
|
|
function CPDrawTiledRects(
|
|
/* CGRect */ boundsRect,
|
|
/* CGRect */ clipRect,
|
|
/* CPRectEdge[] */ sides,
|
|
/* float[] */ grays)
|
|
{
|
|
if (sides.length != grays.length)
|
|
[CPException raise:CPInvalidArgumentException reason:@"sides (length: " + sides.length + ") and grays (length: " + grays.length + ") must have the same length."];
|
|
|
|
var colors = [];
|
|
|
|
for (var i = 0; i < grays.length; ++i)
|
|
colors.push([CPColor colorWithCalibratedWhite:grays[i] alpha:1.0]);
|
|
|
|
return CPDrawColorTiledRects(boundsRect, clipRect, sides, colors);
|
|
}
|
|
|
|
function CPDrawColorTiledRects(
|
|
/* CGRect */ boundsRect,
|
|
/* CGRect */ clipRect,
|
|
/* CPRectEdge[] */ sides,
|
|
/* CPColor[] */ colors)
|
|
{
|
|
if (sides.length != colors.length)
|
|
[CPException raise:CPInvalidArgumentException reason:@"sides (length: " + sides.length + ") and colors (length: " + colors.length + ") must have the same length."];
|
|
|
|
var resultRect = CGRectMakeCopy(boundsRect),
|
|
slice = CGRectMakeZero(),
|
|
remainder = CGRectMakeZero(),
|
|
context = [[CPGraphicsContext currentContext] graphicsPort];
|
|
|
|
CGContextSaveGState(context);
|
|
CGContextSetLineWidth(context, 1.0);
|
|
|
|
for (var sideIndex = 0; sideIndex < sides.length; ++sideIndex)
|
|
{
|
|
var side = sides[sideIndex];
|
|
|
|
CGRectDivide(resultRect, slice, remainder, 1.0, side);
|
|
resultRect = remainder;
|
|
slice = CGRectIntersection(slice, clipRect);
|
|
|
|
// Cocoa docs say that only slices that are within the clipRect are actually drawn
|
|
if (CGRectIsEmpty(slice))
|
|
continue;
|
|
|
|
var minX,
|
|
maxX,
|
|
minY,
|
|
maxY;
|
|
|
|
if (side == CPMinXEdge || side == CPMaxXEdge)
|
|
{
|
|
// Make sure we have at least 1 pixel to draw a line
|
|
if (CGRectGetWidth(slice) < 1.0)
|
|
continue;
|
|
|
|
minX = CGRectGetMinX(slice) + 0.5;
|
|
maxX = minX;
|
|
minY = CGRectGetMinY(slice);
|
|
maxY = CGRectGetMaxY(slice);
|
|
}
|
|
else // CPMinYEdge || CPMaxYEdge
|
|
{
|
|
// Make sure we have at least 1 pixel to draw a line
|
|
if (CGRectGetHeight(slice) < 1.0)
|
|
continue;
|
|
|
|
minX = CGRectGetMinX(slice);
|
|
maxX = CGRectGetMaxX(slice);
|
|
minY = CGRectGetMinY(slice) + 0.5;
|
|
maxY = minY;
|
|
}
|
|
|
|
CGContextBeginPath(context);
|
|
CGContextMoveToPoint(context, minX, minY);
|
|
CGContextAddLineToPoint(context, maxX, maxY);
|
|
CGContextSetStrokeColor(context, colors[sideIndex]);
|
|
CGContextStrokePath(context);
|
|
}
|
|
|
|
CGContextRestoreGState(context);
|
|
|
|
return resultRect;
|
|
}
|