PageRenderTime 687ms CodeModel.GetById 114ms RepoModel.GetById 8ms app.codeStats 0ms

/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
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-2.0
  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. #import "GTMGeometryUtils.h"
  19. /// Align rectangles
  20. //
  21. // Args:
  22. // alignee - rect to be aligned
  23. // aligner - rect to be aligned to
  24. // alignment - alignment to be applied to alignee based on aligner
  25. CGRect GTMCGAlignRectangles(CGRect alignee, CGRect aligner, GTMRectAlignment alignment) {
  26. switch (alignment) {
  27. case GTMRectAlignTop:
  28. alignee.origin.x = aligner.origin.x + (CGRectGetWidth(aligner) * .5f - CGRectGetWidth(alignee) * .5f);
  29. alignee.origin.y = aligner.origin.y + CGRectGetHeight(aligner) - CGRectGetHeight(alignee);
  30. break;
  31. case GTMRectAlignTopLeft:
  32. alignee.origin.x = aligner.origin.x;
  33. alignee.origin.y = aligner.origin.y + CGRectGetHeight(aligner) - CGRectGetHeight(alignee);
  34. break;
  35. case GTMRectAlignTopRight:
  36. alignee.origin.x = aligner.origin.x + CGRectGetWidth(aligner) - CGRectGetWidth(alignee);
  37. alignee.origin.y = aligner.origin.y + CGRectGetHeight(aligner) - CGRectGetHeight(alignee);
  38. break;
  39. case GTMRectAlignLeft:
  40. alignee.origin.x = aligner.origin.x;
  41. alignee.origin.y = aligner.origin.y + (CGRectGetHeight(aligner) * .5f - CGRectGetHeight(alignee) * .5f);
  42. break;
  43. case GTMRectAlignBottomLeft:
  44. alignee.origin.x = aligner.origin.x;
  45. alignee.origin.y = aligner.origin.y;
  46. break;
  47. case GTMRectAlignBottom:
  48. alignee.origin.x = aligner.origin.x + (CGRectGetWidth(aligner) * .5f - CGRectGetWidth(alignee) * .5f);
  49. alignee.origin.y = aligner.origin.y;
  50. break;
  51. case GTMRectAlignBottomRight:
  52. alignee.origin.x = aligner.origin.x + CGRectGetWidth(aligner) - CGRectGetWidth(alignee);
  53. alignee.origin.y = aligner.origin.y;
  54. break;
  55. case GTMRectAlignRight:
  56. alignee.origin.x = aligner.origin.x + CGRectGetWidth(aligner) - CGRectGetWidth(alignee);
  57. alignee.origin.y = aligner.origin.y + (CGRectGetHeight(aligner) * .5f - CGRectGetHeight(alignee) * .5f);
  58. break;
  59. default:
  60. case GTMRectAlignCenter:
  61. alignee.origin.x = aligner.origin.x + (CGRectGetWidth(aligner) * .5f - CGRectGetWidth(alignee) * .5f);
  62. alignee.origin.y = aligner.origin.y + (CGRectGetHeight(aligner) * .5f - CGRectGetHeight(alignee) * .5f);
  63. break;
  64. }
  65. return alignee;
  66. }
  67. CGRect GTMCGScaleRectangleToSize(CGRect scalee, CGSize size, GTMScaling scaling) {
  68. switch (scaling) {
  69. case GTMScaleToFillProportionally:
  70. case GTMScaleProportionally: {
  71. CGFloat height = CGRectGetHeight(scalee);
  72. CGFloat width = CGRectGetWidth(scalee);
  73. if (isnormal(height) && isnormal(width) &&
  74. (height > size.height || width > size.width)) {
  75. CGFloat horiz = size.width / width;
  76. CGFloat vert = size.height / height;
  77. BOOL expand = (scaling == GTMScaleToFillProportionally);
  78. // We use the smaller scale unless expand is true. In that case, larger.
  79. CGFloat newScale = ((horiz < vert) ^ expand) ? horiz : vert;
  80. scalee = GTMCGRectScale(scalee, newScale, newScale);
  81. }
  82. break;
  83. }
  84. case GTMScaleToFit:
  85. scalee.size = size;
  86. break;
  87. case GTMScaleNone:
  88. default:
  89. // Do nothing
  90. break;
  91. }
  92. return scalee;
  93. }