1 /*
2  * Copyright 2011 Google Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SFNTLY_CPP_SRC_SFNTLY_MATH_FONT_MATH_H_
18 #define SFNTLY_CPP_SRC_SFNTLY_MATH_FONT_MATH_H_
19 
20 #include "sfntly/port/type.h"
21 
22 namespace sfntly {
23 
24 class FontMath {
25  public:
Log2(int32_t a)26   static int32_t Log2(int32_t a) {
27     int r = 0;  // r will be lg(a)
28     while (a != 0) {
29       a >>= 1;
30       r++;
31     }
32     return r - 1;
33   }
34 
35   // Calculates the amount of padding needed. The values provided need to be in
36   // the same units. So, if the size is given as the number of bytes then the
37   // alignment size must also be specified as byte size to align to.
38   // @param size the size of the data that may need padding
39   // @param alignmentSize the number of units to align to
40   // @return the number of units needing to be added for alignment
PaddingRequired(int32_t size,int32_t alignment_size)41   static int32_t PaddingRequired(int32_t size, int32_t alignment_size) {
42     int32_t padding = alignment_size - (size % alignment_size);
43     return padding == alignment_size ? 0 : padding;
44   }
45 };
46 
47 }  // namespace sfntly
48 
49 #endif  // SFNTLY_CPP_SRC_SFNTLY_MATH_FONT_MATH_H_
50