vrshoot

annotate libs/drawtext/utf8.c @ 3:c179c72369be

rename candy->vr
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 03 Feb 2014 08:52:13 +0200
parents
children
rev   line source
nuclear@0 1 /*
nuclear@0 2 libdrawtext - a simple library for fast text rendering in OpenGL
nuclear@0 3 Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org>
nuclear@0 4
nuclear@0 5 This program is free software: you can redistribute it and/or modify
nuclear@0 6 it under the terms of the GNU Lesser General Public License as published by
nuclear@0 7 the Free Software Foundation, either version 3 of the License, or
nuclear@0 8 (at your option) any later version.
nuclear@0 9
nuclear@0 10 This program is distributed in the hope that it will be useful,
nuclear@0 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@0 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@0 13 GNU Lesser General Public License for more details.
nuclear@0 14
nuclear@0 15 You should have received a copy of the GNU Lesser General Public License
nuclear@0 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
nuclear@0 17 */
nuclear@0 18 #include "drawtext.h"
nuclear@0 19
nuclear@0 20 #define U8_IS_FIRST(x) (((((x) >> 7) & 1) == 0) || ((((x) >> 6) & 3) == 3))
nuclear@0 21
nuclear@0 22 static const char first_mask[] = {
nuclear@0 23 0,
nuclear@0 24 0x7f, /* single byte, 7 bits valid */
nuclear@0 25 0x1f, /* two-bytes, 5 bits valid */
nuclear@0 26 0xf, /* three-bytes, 4 bits valid */
nuclear@0 27 0x7 /* four-bytes, 3 bits valid */
nuclear@0 28 };
nuclear@0 29 static const char first_shift[] = { 0, 7, 5, 4, 3 }; /* see above */
nuclear@0 30
nuclear@0 31 #define CONT_PREFIX 0x80
nuclear@0 32 #define CONT_MASK 0x3f
nuclear@0 33 #define CONT_SHIFT 6
nuclear@0 34
nuclear@0 35 /* last charcodes for 1, 2, 3 or 4-byte utf8 chars */
nuclear@0 36 static const int utf8_lastcode[] = { 0x7f, 0x7ff, 0xfff, 0x1fffff };
nuclear@0 37
nuclear@0 38 #define prefix_mask(x) (~first_mask[x])
nuclear@0 39 #define prefix(x) ((prefix_mask(x) << 1) & 0xff)
nuclear@0 40
nuclear@0 41
nuclear@0 42 char *dtx_utf8_next_char(char *str)
nuclear@0 43 {
nuclear@0 44 return str + dtx_utf8_nbytes(str);
nuclear@0 45 }
nuclear@0 46
nuclear@0 47 int dtx_utf8_char_code(const char *str)
nuclear@0 48 {
nuclear@0 49 int i, nbytes, shift, code = 0;
nuclear@0 50 int mask;
nuclear@0 51
nuclear@0 52 if(!U8_IS_FIRST(*str)) {
nuclear@0 53 return -1;
nuclear@0 54 }
nuclear@0 55
nuclear@0 56 nbytes = dtx_utf8_nbytes(str);
nuclear@0 57 mask = first_mask[nbytes];
nuclear@0 58 shift = 0;
nuclear@0 59
nuclear@0 60 for(i=0; i<nbytes; i++) {
nuclear@0 61 if(!*str) {
nuclear@0 62 break;
nuclear@0 63 }
nuclear@0 64
nuclear@0 65 code = (code << shift) | (*str++ & mask);
nuclear@0 66 mask = 0x3f;
nuclear@0 67 shift = 6;
nuclear@0 68 }
nuclear@0 69 return code;
nuclear@0 70 }
nuclear@0 71
nuclear@0 72 int dtx_utf8_nbytes(const char *str)
nuclear@0 73 {
nuclear@0 74 int i, numset = 0;
nuclear@0 75 int c = *str;
nuclear@0 76
nuclear@0 77 if(!U8_IS_FIRST(c)) {
nuclear@0 78 for(i=0; !U8_IS_FIRST(str[i]); i++);
nuclear@0 79 return i;
nuclear@0 80 }
nuclear@0 81
nuclear@0 82 /* count the leading 1s */
nuclear@0 83 for(i=0; i<4; i++) {
nuclear@0 84 if(((c >> (7 - i)) & 1) == 0) {
nuclear@0 85 break;
nuclear@0 86 }
nuclear@0 87 numset++;
nuclear@0 88 }
nuclear@0 89
nuclear@0 90 if(!numset) {
nuclear@0 91 return 1;
nuclear@0 92 }
nuclear@0 93 return numset;
nuclear@0 94 }
nuclear@0 95
nuclear@0 96 int dtx_utf8_char_count(const char *str)
nuclear@0 97 {
nuclear@0 98 int n = 0;
nuclear@0 99
nuclear@0 100 while(*str) {
nuclear@0 101 n++;
nuclear@0 102 str = dtx_utf8_next_char((char*)str);
nuclear@0 103 }
nuclear@0 104 return n;
nuclear@0 105 }
nuclear@0 106
nuclear@0 107 size_t dtx_utf8_from_char_code(int code, char *buf)
nuclear@0 108 {
nuclear@0 109 size_t nbytes = 0;
nuclear@0 110 int i;
nuclear@0 111
nuclear@0 112 for(i=0; i<4; i++) {
nuclear@0 113 if(code <= utf8_lastcode[i]) {
nuclear@0 114 nbytes = i + 1;
nuclear@0 115 break;
nuclear@0 116 }
nuclear@0 117 }
nuclear@0 118
nuclear@0 119 if(!nbytes && buf) {
nuclear@0 120 for(i=0; i<(int)nbytes; i++) {
nuclear@0 121 int idx = nbytes - i - 1;
nuclear@0 122 int mask, shift, prefix;
nuclear@0 123
nuclear@0 124 if(idx > 0) {
nuclear@0 125 mask = CONT_MASK;
nuclear@0 126 shift = CONT_SHIFT;
nuclear@0 127 prefix = CONT_PREFIX;
nuclear@0 128 } else {
nuclear@0 129 mask = first_mask[nbytes];
nuclear@0 130 shift = first_shift[nbytes];
nuclear@0 131 prefix = prefix(nbytes);
nuclear@0 132 }
nuclear@0 133
nuclear@0 134 buf[idx] = (code & mask) | (prefix & ~mask);
nuclear@0 135 code >>= shift;
nuclear@0 136 }
nuclear@0 137 }
nuclear@0 138 return nbytes;
nuclear@0 139 }
nuclear@0 140
nuclear@0 141 size_t dtx_utf8_from_string(const wchar_t *str, char *buf)
nuclear@0 142 {
nuclear@0 143 size_t nbytes = 0;
nuclear@0 144 char *ptr = buf;
nuclear@0 145
nuclear@0 146 while(*str) {
nuclear@0 147 int cbytes = dtx_utf8_from_char_code(*str++, ptr);
nuclear@0 148 if(ptr) {
nuclear@0 149 ptr += cbytes;
nuclear@0 150 }
nuclear@0 151 nbytes += cbytes;
nuclear@0 152 }
nuclear@0 153 return nbytes;
nuclear@0 154 }