1 /* 2 * FTGL - OpenGL font library 3 * 4 * Copyright (c) 2001-2004 Henry Maddocks <ftgl@opengl.geek.nz> 5 * Copyright (c) 2008 Sam Hocevar <sam@zoy.org> 6 * Copyright (c) 2008 Sean Morrison <learner@brlcad.org> 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining 9 * a copy of this software and associated documentation files (the 10 * "Software"), to deal in the Software without restriction, including 11 * without limitation the rights to use, copy, modify, merge, publish, 12 * distribute, sublicense, and/or sell copies of the Software, and to 13 * permit persons to whom the Software is furnished to do so, subject to 14 * the following conditions: 15 * 16 * The above copyright notice and this permission notice shall be 17 * included in all copies or substantial portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 22 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 23 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 */ 27 28 #ifndef __ftgl__ 29 # warning This header is deprecated. Please use <FTGL/ftgl.h> from now. 30 # include <FTGL/ftgl.h> 31 #endif 32 33 #ifndef __FTBBox__ 34 #define __FTBBox__ 35 36 #ifdef __cplusplus 37 38 39 /** 40 * FTBBox is a convenience class for handling bounding boxes. 41 */ 42 class FTGL_EXPORT FTBBox 43 { 44 public: 45 /** 46 * Default constructor. Bounding box is set to zero. 47 */ FTBBox()48 FTBBox() 49 : lower(0.0f, 0.0f, 0.0f), 50 upper(0.0f, 0.0f, 0.0f) 51 {} 52 53 /** 54 * Constructor. 55 */ FTBBox(float lx,float ly,float lz,float ux,float uy,float uz)56 FTBBox(float lx, float ly, float lz, float ux, float uy, float uz) 57 : lower(lx, ly, lz), 58 upper(ux, uy, uz) 59 {} 60 61 /** 62 * Constructor. 63 */ FTBBox(FTPoint l,FTPoint u)64 FTBBox(FTPoint l, FTPoint u) 65 : lower(l), 66 upper(u) 67 {} 68 69 /** 70 * Constructor. Extracts a bounding box from a freetype glyph. Uses 71 * the control box for the glyph. <code>FT_Glyph_Get_CBox()</code> 72 * 73 * @param glyph A freetype glyph 74 */ FTBBox(FT_GlyphSlot glyph)75 FTBBox(FT_GlyphSlot glyph) 76 : lower(0.0f, 0.0f, 0.0f), 77 upper(0.0f, 0.0f, 0.0f) 78 { 79 FT_BBox bbox; 80 FT_Outline_Get_CBox(&(glyph->outline), &bbox); 81 82 lower.X(static_cast<float>(bbox.xMin) / 64.0f); 83 lower.Y(static_cast<float>(bbox.yMin) / 64.0f); 84 lower.Z(0.0f); 85 upper.X(static_cast<float>(bbox.xMax) / 64.0f); 86 upper.Y(static_cast<float>(bbox.yMax) / 64.0f); 87 upper.Z(0.0f); 88 } 89 90 /** 91 * Destructor 92 */ ~FTBBox()93 ~FTBBox() 94 {} 95 96 /** 97 * Mark the bounds invalid by setting all lower dimensions greater 98 * than the upper dimensions. 99 */ Invalidate()100 void Invalidate() 101 { 102 lower = FTPoint(1.0f, 1.0f, 1.0f); 103 upper = FTPoint(-1.0f, -1.0f, -1.0f); 104 } 105 106 /** 107 * Determines if this bounding box is valid. 108 * 109 * @return True if all lower values are <= the corresponding 110 * upper values. 111 */ IsValid()112 bool IsValid() 113 { 114 return lower.X() <= upper.X() 115 && lower.Y() <= upper.Y() 116 && lower.Z() <= upper.Z(); 117 } 118 119 /** 120 * Move the Bounding Box by a vector. 121 * 122 * @param vector The vector to move the bbox in 3D space. 123 */ 124 FTBBox& operator += (const FTPoint vector) 125 { 126 lower += vector; 127 upper += vector; 128 129 return *this; 130 } 131 132 /** 133 * Combine two bounding boxes. The result is the smallest bounding 134 * box containing the two original boxes. 135 * 136 * @param bbox The bounding box to merge with the second one. 137 */ 138 FTBBox& operator |= (const FTBBox& bbox) 139 { 140 if(bbox.lower.X() < lower.X()) lower.X(bbox.lower.X()); 141 if(bbox.lower.Y() < lower.Y()) lower.Y(bbox.lower.Y()); 142 if(bbox.lower.Z() < lower.Z()) lower.Z(bbox.lower.Z()); 143 if(bbox.upper.X() > upper.X()) upper.X(bbox.upper.X()); 144 if(bbox.upper.Y() > upper.Y()) upper.Y(bbox.upper.Y()); 145 if(bbox.upper.Z() > upper.Z()) upper.Z(bbox.upper.Z()); 146 147 return *this; 148 } 149 SetDepth(float depth)150 void SetDepth(float depth) 151 { 152 if(depth > 0) 153 upper.Z(lower.Z() + depth); 154 else 155 lower.Z(upper.Z() + depth); 156 } 157 158 Upper()159 inline FTPoint const Upper() const 160 { 161 return upper; 162 } 163 164 Lower()165 inline FTPoint const Lower() const 166 { 167 return lower; 168 } 169 170 private: 171 /** 172 * The bounds of the box 173 */ 174 FTPoint lower, upper; 175 }; 176 177 #endif //__cplusplus 178 179 #endif // __FTBBox__ 180 181