PageRenderTime 32ms CodeModel.GetById 1ms app.highlight 27ms RepoModel.GetById 1ms app.codeStats 1ms

/core/externals/update-engine/externals/google-toolbox-for-mac/Foundation/GTMGeometryUtils.m

http://macfuse.googlecode.com/
Objective C | 108 lines | 68 code | 15 blank | 25 comment | 7 complexity | 1adbe89913c87a8e54e1b3af0bad628a MD5 | raw file
  1//
  2//  GTMGeometryUtils.m
  3//
  4//  Copyright 2006-2008 Google Inc.
  5//
  6//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7//  use this file except in compliance with the License.  You may obtain a copy
  8//  of the License at
  9// 
 10//  http://www.apache.org/licenses/LICENSE-2.0
 11// 
 12//  Unless required by applicable law or agreed to in writing, software
 13//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 14//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 15//  License for the specific language governing permissions and limitations under
 16//  the License.
 17//
 18
 19#import "GTMGeometryUtils.h"
 20
 21/// Align rectangles
 22//
 23//  Args:
 24//    alignee - rect to be aligned
 25//    aligner - rect to be aligned to
 26//    alignment - alignment to be applied to alignee based on aligner
 27
 28CGRect GTMCGAlignRectangles(CGRect alignee, CGRect aligner, GTMRectAlignment alignment) {
 29  switch (alignment) {
 30    case GTMRectAlignTop:
 31      alignee.origin.x = aligner.origin.x + (CGRectGetWidth(aligner) * .5f - CGRectGetWidth(alignee) * .5f);
 32      alignee.origin.y = aligner.origin.y + CGRectGetHeight(aligner) - CGRectGetHeight(alignee);
 33      break;
 34      
 35    case GTMRectAlignTopLeft:
 36      alignee.origin.x = aligner.origin.x;
 37      alignee.origin.y = aligner.origin.y + CGRectGetHeight(aligner) - CGRectGetHeight(alignee);
 38    break;
 39    
 40    case GTMRectAlignTopRight:
 41      alignee.origin.x = aligner.origin.x + CGRectGetWidth(aligner) - CGRectGetWidth(alignee);
 42      alignee.origin.y = aligner.origin.y + CGRectGetHeight(aligner) - CGRectGetHeight(alignee);
 43      break;
 44
 45    case GTMRectAlignLeft:
 46      alignee.origin.x = aligner.origin.x;
 47      alignee.origin.y = aligner.origin.y + (CGRectGetHeight(aligner) * .5f - CGRectGetHeight(alignee) * .5f);
 48      break;
 49      
 50    case GTMRectAlignBottomLeft:
 51      alignee.origin.x = aligner.origin.x;
 52      alignee.origin.y = aligner.origin.y;
 53      break;
 54
 55    case GTMRectAlignBottom:
 56      alignee.origin.x = aligner.origin.x + (CGRectGetWidth(aligner) * .5f - CGRectGetWidth(alignee) * .5f);
 57      alignee.origin.y = aligner.origin.y;
 58      break;
 59
 60    case GTMRectAlignBottomRight:
 61      alignee.origin.x = aligner.origin.x + CGRectGetWidth(aligner) - CGRectGetWidth(alignee);
 62      alignee.origin.y = aligner.origin.y;
 63      break;
 64      
 65    case GTMRectAlignRight:
 66      alignee.origin.x = aligner.origin.x + CGRectGetWidth(aligner) - CGRectGetWidth(alignee);
 67      alignee.origin.y = aligner.origin.y + (CGRectGetHeight(aligner) * .5f - CGRectGetHeight(alignee) * .5f);
 68      break;
 69      
 70    default:
 71    case GTMRectAlignCenter:
 72      alignee.origin.x = aligner.origin.x + (CGRectGetWidth(aligner) * .5f - CGRectGetWidth(alignee) * .5f);
 73      alignee.origin.y = aligner.origin.y + (CGRectGetHeight(aligner) * .5f - CGRectGetHeight(alignee) * .5f);
 74      break;
 75  }
 76  return alignee;
 77}
 78
 79CGRect GTMCGScaleRectangleToSize(CGRect scalee, CGSize size, GTMScaling scaling) {
 80  switch (scaling) {
 81      
 82    case GTMScaleToFillProportionally:
 83    case GTMScaleProportionally: {
 84      CGFloat height = CGRectGetHeight(scalee);
 85      CGFloat width = CGRectGetWidth(scalee);
 86      if (isnormal(height) && isnormal(width) && 
 87          (height > size.height || width > size.width)) {
 88        CGFloat horiz = size.width / width;
 89        CGFloat vert = size.height / height;
 90        BOOL expand = (scaling == GTMScaleToFillProportionally);
 91        // We use the smaller scale unless expand is true. In that case, larger.
 92        CGFloat newScale = ((horiz < vert) ^ expand) ? horiz : vert;
 93        scalee = GTMCGRectScale(scalee, newScale, newScale);
 94      }
 95      break;
 96    }
 97    
 98    case GTMScaleToFit:
 99      scalee.size = size;
100      break;
101      
102    case GTMScaleNone:
103    default:
104      // Do nothing
105      break;
106  }
107  return scalee;
108}