dungeon_crawler

annotate prototype/drawtext/utf8.c @ 79:110b2af4b9d8

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