Code

c167d71472db46d110ef098b3689e7b9874d05ec
[inkscape.git] / src / unicoderange.cpp
1 #include "unicoderange.h"
3 #include <malloc.h>
4 #include <string.h>
6 static unsigned int hex2int(char* s){
7         int res=0;
8         int i=0, mul=1;
9         while(s[i+1]!='\0') i++;
11         while(i>=0){
12                 if (s[i] > '9') res += mul * (s[i]-'A'+10);
13                 else res += mul * (s[i]-'0');
14                 i--;
15                 mul*=16;
16         }
17         return res;
18 }
20 UnicodeRange::UnicodeRange(const gchar* value){
21         gchar* val = (gchar*) value;
22         while(val[0] != '\0'){
23                 if (val[0]=='U' && val[1]=='+'){
24                         val += add_range(val);
25                 } else {
26 //                      g_warning("adding unichar. unichar=%c", g_utf8_get_char(&val[0]));
27                         this->unichars.push_back(g_utf8_get_char(&val[0]));
28                         val++;
29                 }
30                 //skip spaces or commas
31                 while(val[0]==' ' || val[0]==',') val++;
32         }
33 }
35 int
36 UnicodeRange::add_range(gchar* val){
37                 Urange r;
38                 //U+
39                 val+=2;
40                 int i=0, count=2;
41                 while(val[i]!='\0' && val[i]!='-' && val[i]!=' ' && val[i]!=',') i++;
42                 r.start = (gchar*) malloc((i+1)*sizeof(gchar*));
43                 strncpy(r.start, val, i);
44                 r.start[i] = '\0';
45                 val+=i;
46                 count+=i;
47                 i=0;
48                 if (val[0]=='-'){
49                         val++;
50                         while(val[i]!='\0' && val[i]!='-' && val[i]!=' ' && val[i]!=',') i++;
51                         r.end = (gchar*) malloc((i+1)*sizeof(gchar*));
52                         strncpy(r.end, val, i);
53                         r.end[i] = '\0';
54                         val+=i;
55                         count+=i;
56                 } else {
57                         r.end=NULL;
58                 }
59 //              g_warning("adding range. from %s to %s", r.start, r.end);
60                 this->range.push_back(r);
61                 return count+1;
62 }
64 bool UnicodeRange::contains(gchar unicode){
65         for(unsigned int i=0;i<this->unichars.size();i++){
66                 if ((gunichar) unicode == this->unichars[i]) return true;
67         }
69         unsigned int unival;
70         unival = g_utf8_get_char (&unicode);
71         g_warning("unival=%d", unival);
72         char uni[9] = "00000000";
73         uni[8]= '\0';
74         unsigned char val;
75         for (unsigned int i=7; unival>0; i--){
76                 val = unival & 0xf;
77                 unival = unival >> 4;
78                 if (val < 10) uni[i] = '0' + val;
79                 else uni[i] = 'A'+ val - 10;
80         }
81 //      g_warning("uni=%s", uni);
83         bool found;
84         for(unsigned int i=0;i<this->range.size();i++){
85                 Urange r = this->range[i];
86                 if (r.end){
87 //                      g_warning("hex2int: start=%d", hex2int(r.start));
88 //                      g_warning("hex2int: end=%d", hex2int(r.end));
89                         if (unival >= hex2int(r.start) && unival <= hex2int(r.end)) return true;
90                 } else {
91                         found = true;
92                         for (int pos=0;pos<8;pos++){
93                                 if (uni[pos]!='?' && uni[pos]!=r.start[pos]) found = false;
94                         }
95                         if (found) return true;
96                 }
97         }
98         return false;
99 }