Blender  V2.59
blf_util.c
Go to the documentation of this file.
00001 /*
00002  * $Id: blf_util.c 35248 2011-02-27 20:42:42Z jesterking $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version. 
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2009 Blender Foundation.
00021  * All rights reserved.
00022  *
00023  * 
00024  * Contributor(s): Blender Foundation
00025  *
00026  * ***** END GPL LICENSE BLOCK *****
00027  */
00028 
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 #include <string.h>
00037 
00038 #include "blf_internal.h"
00039 
00040 unsigned int blf_next_p2(unsigned int x)
00041 {
00042         x -= 1;
00043         x |= (x >> 16);
00044         x |= (x >> 8);
00045         x |= (x >> 4);
00046         x |= (x >> 2);
00047         x |= (x >> 1);
00048         x += 1;
00049         return(x);
00050 }
00051 
00052 unsigned int blf_hash(unsigned int val)
00053 {
00054         unsigned int key;
00055 
00056         key= val;
00057         key += ~(key << 16);
00058         key ^= (key >> 5);
00059         key += (key << 3);
00060         key ^= (key >> 13);
00061         key += ~(key << 9);
00062         key ^= (key >> 17);
00063         return(key % 257);
00064 }
00065 
00066 /*
00067  * This function is from Imlib2 library (font_main.c), a
00068  * library that does image file loading and saving as well
00069  * as rendering, manipulation, arbitrary polygon support, etc.
00070  *
00071  * Copyright (C) 2000 Carsten Haitzler and various contributors
00072  * The original name: imlib_font_utf8_get_next
00073  * more info here: http://docs.enlightenment.org/api/imlib2/html/
00074  */
00075 int blf_utf8_next(unsigned char *buf, int *iindex)
00076 {
00077         /* Reads UTF8 bytes from 'buf', starting at 'index' and
00078          * returns the code point of the next valid code point.
00079          * 'index' is updated ready for the next call.
00080          *
00081          * Returns 0 to indicate an error (e.g. invalid UTF8)
00082          */
00083         int index= *iindex, len, r;
00084         unsigned char d, d2, d3, d4;
00085 
00086         d= buf[index++];
00087         if (!d)
00088                 return(0);
00089 
00090         while (buf[index] && ((buf[index] & 0xc0) == 0x80))
00091                 index++;
00092 
00093         len= index - *iindex;
00094         if (len == 1)
00095                 r= d;
00096         else if (len == 2) {
00097                 /* 2 byte */
00098                 d2= buf[*iindex + 1];
00099                 r= d & 0x1f; /* copy lower 5 */
00100                 r <<= 6;
00101                 r |= (d2 & 0x3f); /* copy lower 6 */
00102         }
00103         else if (len == 3) {
00104                 /* 3 byte */
00105                 d2= buf[*iindex + 1];
00106                 d3= buf[*iindex + 2];
00107                 r= d & 0x0f; /* copy lower 4 */
00108                 r <<= 6;
00109                 r |= (d2 & 0x3f);
00110                 r <<= 6;
00111                 r |= (d3 & 0x3f);
00112         }
00113         else {
00114                 /* 4 byte */
00115                 d2= buf[*iindex + 1];
00116                 d3= buf[*iindex + 2];
00117                 d4= buf[*iindex + 3];
00118                 r= d & 0x0f; /* copy lower 4 */
00119                 r <<= 6;
00120                 r |= (d2 & 0x3f);
00121                 r <<= 6;
00122                 r |= (d3 & 0x3f);
00123                 r <<= 6;
00124                 r |= (d4 & 0x3f);
00125         }
00126         *iindex= index;
00127         return(r);
00128 }