istereo2

annotate libs/drawtext/utf8.c @ 7:a3c4fcc9f8f3

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