3dphotoshoot

annotate libs/drawtext/utf8.c @ 27:3d082c566b53

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