diff options
Diffstat (limited to 'Build/source/texk/web2c/luatexdir/luafontloader/fontforge/Unicode/ustring.c')
-rw-r--r-- | Build/source/texk/web2c/luatexdir/luafontloader/fontforge/Unicode/ustring.c | 343 |
1 files changed, 209 insertions, 134 deletions
diff --git a/Build/source/texk/web2c/luatexdir/luafontloader/fontforge/Unicode/ustring.c b/Build/source/texk/web2c/luatexdir/luafontloader/fontforge/Unicode/ustring.c index b11b8400635..e8bb2bd936a 100644 --- a/Build/source/texk/web2c/luatexdir/luafontloader/fontforge/Unicode/ustring.c +++ b/Build/source/texk/web2c/luatexdir/luafontloader/fontforge/Unicode/ustring.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2000-2008 by George Williams */ +/* Copyright (C) 2000-2012 by George Williams */ /* * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -284,7 +284,19 @@ unichar_t *u_copyn(const unichar_t *pt, long n) { if ( n*sizeof(unichar_t)>=MEMORY_MASK ) n = MEMORY_MASK/sizeof(unichar_t)-1; #endif - res = galloc((n+1)*sizeof(unichar_t)); + res = (unichar_t *) galloc((n+1)*sizeof(unichar_t)); + memcpy(res,pt,n*sizeof(unichar_t)); + res[n]='\0'; +return(res); +} + +unichar_t *u_copynallocm(const unichar_t *pt, long n, long m) { + unichar_t *res; +#ifdef MEMORY_MASK + if ( n*sizeof(unichar_t)>=MEMORY_MASK ) + n = MEMORY_MASK/sizeof(unichar_t)-1; +#endif + res = galloc((m+1)*sizeof(unichar_t)); memcpy(res,pt,n*sizeof(unichar_t)); res[n]='\0'; return(res); @@ -306,7 +318,7 @@ return( u_copy( s2 )); else if ( s2==NULL ) return( u_copy( s1 )); len1 = u_strlen(s1); len2 = u_strlen(s2); - pt = galloc((len1+len2+1)*sizeof(unichar_t)); + pt = (unichar_t *) galloc((len1+len2+1)*sizeof(unichar_t)); u_strcpy(pt,s1); u_strcpy(pt+len1,s2); return( pt ); @@ -322,7 +334,7 @@ return((unichar_t *)0); if ( (len+1)*sizeof(unichar_t)>=MEMORY_MASK ) len = MEMORY_MASK/sizeof(unichar_t)-1; #endif - res = galloc((len+1)*sizeof(unichar_t)); + res = (unichar_t *) galloc((len+1)*sizeof(unichar_t)); for ( rpt=res; --len>=0 ; *rpt++ = *(unsigned char *) pt++ ); *rpt = '\0'; return(res); @@ -340,7 +352,7 @@ return((unichar_t *)0); if ( (n+1)*sizeof(unichar_t)>=MEMORY_MASK ) n = MEMORY_MASK/sizeof(unichar_t)-1; #endif - res = galloc((n+1)*sizeof(unichar_t)); + res = (unichar_t *) galloc((n+1)*sizeof(unichar_t)); for ( rpt=res; --n>=0 ; *rpt++ = *(unsigned char *) pt++ ); *rpt = '\0'; return(res); @@ -356,7 +368,7 @@ return(NULL); if ( (len+1)>=MEMORY_MASK ) len = MEMORY_MASK-1; #endif - res = galloc(len+1); + res = (char *) galloc(len+1); for ( rpt=res; --len>=0 ; *rpt++ = *pt++ ); *rpt = '\0'; return(res); @@ -374,7 +386,7 @@ return((char *)0); if ( (n+1)>=MEMORY_MASK ) n = MEMORY_MASK/sizeof(unichar_t)-1; #endif - res = galloc(n+1); + res = (char *) galloc(n+1); for ( rpt=res; --n>=0 ; *rpt++ = *pt++ ); *rpt = '\0'; return(res); @@ -386,7 +398,7 @@ double u_strtod(const unichar_t *str, unichar_t **ptr) { double val; extern double strtod(); /* Please don't delete this, not all of us have good ansi headers */ - for ( upt=str, pt=buf; *upt<128 && *upt!='\0'; ) + for ( upt=str, pt=buf; *upt<128 && *upt!='\0' && pt-buf<sizeof(buf)-1; ) *pt++ = *upt++; *pt = '\0'; val = strtod(buf,&ret); @@ -440,7 +452,7 @@ unichar_t *cu_strstartmatch(const char *key,const unichar_t *str) { if ( key && str ) { while( *key ) { if(tolower(*key) != tolower(*str)) -return 0; +return 0; key++; str++; } @@ -487,24 +499,12 @@ unichar_t *utf82u_strncpy(unichar_t *ubuf,const char *utf8buf,int len) { } else if ( *pt<=0xef ) { *upt = ((*pt&0xf)<<12) | ((pt[1]&0x3f)<<6) | (pt[2]&0x3f); pt += 3; -#ifdef UNICHAR_16 - } else if ( upt+1<uend ) { - /* Um... I don't support surrogates */ - w = ( ((*pt&0x7)<<2) | ((pt[1]&0x30)>>4) )-1; - *upt++ = 0xd800 | (w<<6) | ((pt[1]&0xf)<<2) | ((pt[2]&0x30)>>4); - *upt = 0xdc00 | ((pt[2]&0xf)<<6) | (pt[3]&0x3f); - pt += 4; - } else { - /* no space for surrogate */ - pt += 4; -#else } else { w = ( ((*pt&0x7)<<2) | ((pt[1]&0x30)>>4) )-1; w = (w<<6) | ((pt[1]&0xf)<<2) | ((pt[2]&0x30)>>4); w2 = ((pt[2]&0xf)<<6) | (pt[3]&0x3f); *upt = w*0x400 + w2 + 0x10000; pt += 4; -#endif } ++upt; } @@ -516,88 +516,8 @@ unichar_t *utf82u_strcpy(unichar_t *ubuf,const char *utf8buf) { return( utf82u_strncpy(ubuf,utf8buf,strlen(utf8buf)+1)); } -# ifdef UNICHAR_16 -uint32 *utf82u32_strncpy(uint32 *ubuf,const char *utf8buf,int len) { - uint32 *upt=ubuf, *uend=ubuf+len-1; - const uint8 *pt = (const uint8 *) utf8buf; - int w, w2; - - while ( *pt!='\0' && upt<uend ) { - if ( *pt<=127 ) - *upt = *pt++; - else if ( *pt<=0xdf ) { - *upt = ((*pt&0x1f)<<6) | (pt[1]&0x3f); - pt += 2; - } else if ( *pt<=0xef ) { - *upt = ((*pt&0xf)<<12) | ((pt[1]&0x3f)<<6) | (pt[2]&0x3f); - pt += 3; - } else { - w = ( ((*pt&0x7)<<2) | ((pt[1]&0x30)>>4) )-1; - w = (w<<6) | ((pt[1]&0xf)<<2) | ((pt[2]&0x30)>>4); - w2 = ((pt[2]&0xf)<<6) | (pt[3]&0x3f); - *upt = w*0x400 + w2 + 0x10000; - pt += 4; - } - ++upt; - } - *upt = '\0'; -return( ubuf ); -} - -char *u322utf8_strncpy(char *utf8buf, const uint32 *ubuf,int len) { - uint8 *pt=(uint8 *) utf8buf, *end=(uint8 *) utf8buf+len-1; - const uint32 *upt = ubuf; - - while ( *upt!='\0' && pt<end ) { - if ( *upt<=127 ) - *pt++ = *upt; - else if ( *upt<=0x7ff ) { - if ( pt+1>=end ) - break; - *pt++ = 0xc0 | (*upt>>6); - *pt++ = 0x80 | (*upt&0x3f); - } else if ( *upt<=0xffff ) { - if ( pt+2>=end ) - break; - *pt++ = 0xe0 | (*upt>>12); - *pt++ = 0x80 | ((*upt>>6)&0x3f); - *pt++ = 0x80 | (*upt&0x3f); - } else { - uint32 val = *upt-0x10000; - int u = ((val&0xf0000)>>16)+1, z=(val&0x0f000)>>12, y=(val&0x00fc0)>>6, x=val&0x0003f; - if ( pt+3>=end ) - break; - *pt++ = 0xf0 | (u>>2); - *pt++ = 0x80 | ((u&3)<<4) | z; - *pt++ = 0x80 | y; - *pt++ = 0x80 | x; - } - ++upt; - } - *pt = '\0'; -return( utf8buf ); -} - -char *u322utf8_copy(const uint32 *ubuf) { - int i, len; - char *buf; - - for ( i=len=0; ubuf[i]!=0; ++i ) - if ( ubuf[i]<0x80 ) - ++len; - else if ( ubuf[i]<0x800 ) - len += 2; - else if ( ubuf[i]<0x10000 ) - len += 3; - else - len += 4; - buf = galloc(len+1); -return( u322utf8_strncpy(buf,ubuf,len+1)); -} -#endif - unichar_t *utf82u_copyn(const char *utf8buf,int len) { - unichar_t *ubuf = galloc((len+1)*sizeof(unichar_t)); + unichar_t *ubuf = (unichar_t *) galloc((len+1)*sizeof(unichar_t)); return( utf82u_strncpy(ubuf,utf8buf,len+1)); } @@ -609,7 +529,7 @@ unichar_t *utf82u_copy(const char *utf8buf) { return( NULL ); len = strlen(utf8buf); - ubuf = galloc((len+1)*sizeof(unichar_t)); + ubuf = (unichar_t *) galloc((len+1)*sizeof(unichar_t)); return( utf82u_strncpy(ubuf,utf8buf,len+1)); } @@ -617,20 +537,6 @@ void utf82u_strcat(unichar_t *to,const char *from) { utf82u_strcpy(to+u_strlen(to),from); } -#ifdef UNICHAR_16 -uint32 *utf82u32_copy(const char *utf8buf) { - int len; - uint32 *ubuf; - - if ( utf8buf==NULL ) -return( NULL ); - - len = strlen(utf8buf); - ubuf = galloc((len+1)*sizeof(uint32)); -return( utf82u32_strncpy(ubuf,utf8buf,len+1)); -} -#endif - char *u2utf8_strcpy(char *utf8buf,const unichar_t *ubuf) { char *pt = utf8buf; @@ -640,18 +546,6 @@ char *u2utf8_strcpy(char *utf8buf,const unichar_t *ubuf) { else if ( *ubuf<0x800 ) { *pt++ = 0xc0 | (*ubuf>>6); *pt++ = 0x80 | (*ubuf&0x3f); -#ifdef UNICHAR_16 - } else if ( *ubuf>=0xd800 && *ubuf<0xdc00 && ubuf[1]>=0xdc00 && ubuf[1]<0xe000 ) { - int u = ((*ubuf>>6)&0xf)+1, y = ((*ubuf&3)<<4) | ((ubuf[1]>>6)&0xf); - *pt++ = 0xf0 | (u>>2); - *pt++ = 0x80 | ((u&3)<<4) | ((*ubuf>>2)&0xf); - *pt++ = 0x80 | y; - *pt++ = 0x80 | (ubuf[1]&0x3f); - } else { - *pt++ = 0xe0 | (*ubuf>>12); - *pt++ = 0x80 | ((*ubuf>>6)&0x3f); - *pt++ = 0x80 | (*ubuf&0x3f); -#else } else if ( *ubuf < 0x10000 ) { *pt++ = 0xe0 | (*ubuf>>12); *pt++ = 0x80 | ((*ubuf>>6)&0x3f); @@ -663,7 +557,6 @@ char *u2utf8_strcpy(char *utf8buf,const unichar_t *ubuf) { *pt++ = 0x80 | ((u&3)<<4) | z; *pt++ = 0x80 | y; *pt++ = 0x80 | x; -#endif } ++ubuf; } @@ -708,7 +601,7 @@ char *latin1_2_utf8_copy(const char *lbuf) { return( NULL ); len = strlen(lbuf); - utf8buf = galloc(2*len+1); + utf8buf = (char *) galloc(2*len+1); return( latin1_2_utf8_strcpy(utf8buf,lbuf)); } @@ -721,7 +614,7 @@ char *utf8_2_latin1_copy(const char *utf8buf) { return( NULL ); len = strlen(utf8buf); - pt = lbuf = galloc(len+1); + pt = lbuf = (char *) galloc(len+1); for ( upt=utf8buf; (ch=utf8_ildb(&upt))!='\0'; ) if ( ch>=0xff ) *pt++ = '?'; @@ -739,7 +632,7 @@ char *u2utf8_copy(const unichar_t *ubuf) { return( NULL ); len = u_strlen(ubuf); - utf8buf = galloc((len+1)*3); + utf8buf = (char *) galloc((len+1)*4); return( u2utf8_strcpy(utf8buf,ubuf)); } @@ -750,7 +643,7 @@ char *u2utf8_copyn(const unichar_t *ubuf,int len) { if ( ubuf==NULL ) return( NULL ); - utf8buf = pt = galloc((len+1)*3); + utf8buf = pt = (char *) galloc((len+1)*4); for ( i=0; i<len && *ubuf!='\0'; ++i ) pt = utf8_idpb(pt, *ubuf++); *pt = '\0'; @@ -843,6 +736,24 @@ return( false ); return( true ); } +void utf8_truncatevalid(char *str) { + /* There are certain cases where we have a fixed amount of space to display */ + /* something, and if it doesn't fit in that, then we truncate it. But... */ + /* that can leave us with a half completed utf8 byte sequence. So truncate*/ + /* again, right before the start of the bad sequence */ + int ch; + char *old; + + old = str; + while ( (ch=utf8_ildb((const char **) &str))!='\0' ) { + if ( ch==-1 ) { + *old = '\0'; +return; + } + old = str; + } +} + char *utf8_db(char *utf8_text) { /* Decrement utf8 pointer */ unsigned char *pt = (unsigned char *) utf8_text; @@ -888,3 +799,167 @@ int utf82u_strlen(const char *utf8_str) { ++len; return( len ); } + +void utf8_strncpy(register char *to, const char *from, int len) { + /* copy n characters NOT bytes */ + const char *old = from; + while ( len && *old ) { + utf8_ildb(&old); + len--; + } + strncpy(to, from, old-from); + to[old-from] = 0; +} + +#include <chardata.h> +char *StripToASCII(const char *utf8_str) { + /* Remove any non-ascii characters: Special case, convert the copyright symbol to (c) */ + char *newcr, *pt, *end; + int len, ch; + const unichar_t *alt; + + len = strlen(utf8_str); + pt = newcr = (char *) galloc(len+1); + end = pt+len; + while ( (ch= utf8_ildb(&utf8_str))!='\0' ) { + if ( pt>=end ) { + int off = pt-newcr; + newcr = (char *) grealloc(newcr,(off+10)+1); + pt = newcr+off; + end = pt+10; + } + if ( (ch>=' ' && ch<'\177' ) || ch=='\n' || ch=='\t' ) + *pt++ = ch; + else if ( ch=='\r' && *utf8_str!='\n' ) + *pt++ = '\n'; + else if ( ch==0xa9 /* Copyright sign */ ) { + char *str = "(c)"; + if ( pt+strlen(str)>=end ) { + int off = pt-newcr; + newcr = (char *) grealloc(newcr,(off+10+strlen(str))+1); + pt = newcr+off; + end = pt+10; + } + while ( *str ) + *pt++ = *str++; + } else if ( unicode_alternates[ch>>8]!=NULL && + (alt = unicode_alternates[ch>>8][ch&0xff])!=NULL ) { + while ( *alt!='\0' ) { + if ( pt>=end ) { + int off = pt-newcr; + newcr = (char *) grealloc(newcr,(off+10)+1); + pt = newcr+off; + end = pt+10; + } + if ( *alt>=' ' && *alt<'\177' ) + *pt++ = *alt; + else if ( *alt==0x300 ) + *pt++ = '`'; + else if ( *alt==0x301 ) + *pt++ = '\''; + else if ( *alt==0x302 ) + *pt++ = '^'; + else if ( *alt==0x303 ) + *pt++ = '~'; + else if ( *alt==0x308 ) + *pt++ = ':'; + ++alt; + } + } + } + *pt = '\0'; +return( newcr ); +} + +int AllAscii(const char *txt) { + for ( ; *txt!='\0'; ++txt ) { + if ( *txt=='\t' || *txt=='\n' || *txt=='\r' ) + /* All right */; + else if ( *txt<' ' || *txt>='\177' ) +return( false ); + } +return( true ); +} + +int uAllAscii(const unichar_t *txt) { + for ( ; *txt!='\0'; ++txt ) { + if ( *txt=='\t' || *txt=='\n' || *txt=='\r' ) + /* All right */; + else if ( *txt<' ' || *txt>='\177' ) +return( false ); + } +return( true ); +} + +char* chomp( char* line ) { + if( !line ) + return line; + if ( line[strlen(line)-1]=='\n' ) + line[strlen(line)-1] = '\0'; + if ( line[strlen(line)-1]=='\r' ) + line[strlen(line)-1] = '\0'; + return line; +} + +char *copytolower(const char *input) +{ + char* ret = copy(input); + char* p = ret; + for( ; *p; ++p ) { + *p = tolower(*p); + } + return ret; +} + + +int endswith(const char *haystack,const char *needle) { + int haylen = strlen( haystack ); + int nedlen = strlen( needle ); + char* p; + if( haylen < nedlen ) + return 0; + p = strstr( haystack + haylen - nedlen, needle ); + return p == ( haystack + haylen - nedlen ); +} + +int endswithi(const char *haystackZ,const char *needleZ) { + char* haystack = copytolower(haystackZ); + char* needle = copytolower(needleZ); + int ret = endswith( haystack, needle ); + free( haystack ); + free( needle ); + return ret; +} + +int endswithi_partialExtension( const char *haystackZ,const char *needleZ) { + int nedlen = strlen(needleZ); + char* haystack; + char* needle; + int ret; + int i; + if( nedlen == 0 ) { + return 0; + } + haystack = copytolower(haystackZ); + needle = copytolower(needleZ); + ret = 0; + i = nedlen-1; + ret |= endswith( haystack, needle ); + for( ; i>=0 && !ret ; --i ) { + needle[i] = '\0'; + ret |= endswith( haystack, needle ); + } + free( haystack ); + free( needle ); + return ret; +} + +int u_endswith(const unichar_t *haystack,const unichar_t *needle) { + int haylen = u_strlen( haystack ); + int nedlen = u_strlen( needle ); + unichar_t* p; + if( haylen < nedlen ) + return 0; + p = u_strstr( haystack + haylen - nedlen, needle ); + return p == ( haystack + haylen - nedlen ); +} |