~danieljabailey/inkscape/arc_node_editor

« back to all changes in this revision

Viewing changes to src/unicoderange.cpp

  • Committer: scislac
  • Date: 2009-08-12 07:57:52 UTC
  • Revision ID: scislac@users.sourceforge.net-20090812075752-3zt99jgeqr3bm16j
much better quality multi-size windows icon, thanks ChrisMorgan

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
#include <string.h>
5
5
 
6
6
static unsigned int hex2int(char* s){
7
 
    int res=0;
8
 
    int i=0, mul=1;
9
 
    while(s[i+1]!='\0') i++;
 
7
        int res=0;
 
8
        int i=0, mul=1;
 
9
        while(s[i+1]!='\0') i++;
10
10
 
11
 
    while(i>=0){
12
 
        if (s[i] >= 'A' && s[i] <= 'F') res += mul * (s[i]-'A'+10);
13
 
        if (s[i] >= 'a' && s[i] <= 'f') res += mul * (s[i]-'a'+10);
14
 
        if (s[i] >= '0' && s[i] <= '9') res += mul * (s[i]-'0');
15
 
        i--;
16
 
        mul*=16;
17
 
    }
18
 
    return res;
 
11
        while(i>=0){
 
12
                if (s[i] >= 'A' && s[i] <= 'F') res += mul * (s[i]-'A'+10);
 
13
                if (s[i] >= 'a' && s[i] <= 'f') res += mul * (s[i]-'a'+10);
 
14
                if (s[i] >= '0' && s[i] <= '9') res += mul * (s[i]-'0');
 
15
                i--;
 
16
                mul*=16;
 
17
        }
 
18
        return res;
19
19
}
20
20
 
21
21
UnicodeRange::UnicodeRange(const gchar* value){
22
 
    if (!value) return;
23
 
    gchar* val = (gchar*) value;
24
 
    while(val[0] != '\0'){
25
 
        if (val[0]=='U' && val[1]=='+'){
26
 
            val += add_range(val+2);
27
 
        } else {
28
 
            this->unichars.push_back(g_utf8_get_char(&val[0]));
29
 
            val++;
30
 
        }
31
 
        //skip spaces or commas
32
 
        while(val[0]==' ' || val[0]==',') val++;
33
 
    }
 
22
        if (!value) return;
 
23
        gchar* val = (gchar*) value;
 
24
        while(val[0] != '\0'){
 
25
                if (val[0]=='U' && val[1]=='+'){
 
26
                        val += add_range(val+2);
 
27
                } else {
 
28
                        this->unichars.push_back(g_utf8_get_char(&val[0]));
 
29
                        val++;
 
30
                }
 
31
                //skip spaces or commas
 
32
                while(val[0]==' ' || val[0]==',') val++;
 
33
        }
34
34
}
35
35
 
36
 
int UnicodeRange::add_range(gchar* val){
37
 
        Urange r;
38
 
        int i=0, count=0;
39
 
        while(val[i]!='\0' && val[i]!='-' && val[i]!=' ' && val[i]!=','){
40
 
            i++;
41
 
        }
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
 
        this->range.push_back(r);
60
 
        return count+1;
 
36
int
 
37
UnicodeRange::add_range(gchar* val){
 
38
                Urange r;
 
39
                int i=0, count=0;
 
40
                while(val[i]!='\0' && val[i]!='-' && val[i]!=' ' && val[i]!=',') i++;
 
41
                r.start = (gchar*) malloc((i+1)*sizeof(gchar*));
 
42
                strncpy(r.start, val, i);
 
43
                r.start[i] = '\0';
 
44
                val+=i;
 
45
                count+=i;
 
46
                i=0;
 
47
                if (val[0]=='-'){
 
48
                        val++;
 
49
                        while(val[i]!='\0' && val[i]!='-' && val[i]!=' ' && val[i]!=',') i++;
 
50
                        r.end = (gchar*) malloc((i+1)*sizeof(gchar*));
 
51
                        strncpy(r.end, val, i);
 
52
                        r.end[i] = '\0';
 
53
                        val+=i;
 
54
                        count+=i;
 
55
                } else {
 
56
                        r.end=NULL;
 
57
                }
 
58
                this->range.push_back(r);
 
59
                return count+1;
61
60
}
62
61
 
63
62
bool UnicodeRange::contains(gchar unicode){
64
 
    for(unsigned int i=0;i<this->unichars.size();i++){
65
 
        if (static_cast<gunichar>(unicode) == this->unichars[i]){
66
 
            return true;
67
 
        }
68
 
    }
69
 
 
70
 
    unsigned int unival;
71
 
    unival = g_utf8_get_char (&unicode);
72
 
    char uni[9] = "00000000";
73
 
    uni[8]= '\0';
74
 
    for (unsigned int i=7; unival>0; i--){
75
 
        unsigned char val = unival & 0xf;
76
 
        unival = unival >> 4;
77
 
        if (val < 10) uni[i] = '0' + val;
78
 
        else uni[i] = 'A'+ val - 10;
79
 
    }
80
 
 
81
 
    bool found;
82
 
    for(unsigned int i=0;i<this->range.size();i++){
83
 
        Urange r = this->range[i];
84
 
        if (r.end){
85
 
            if (unival >= hex2int(r.start) && unival <= hex2int(r.end)) return true;
86
 
        } else {
87
 
            found = true;
88
 
 
89
 
            int p=0;
90
 
            while (r.start[p]!='\0') p++;
91
 
            p--;
92
 
 
93
 
            for (int pos=8;p>=0;pos--,p--){
94
 
                if (uni[pos]!='?' && uni[pos]!=r.start[p]) found = false;
95
 
            }
96
 
            if (found) return true;
97
 
        }
98
 
    }
99
 
    return false;
 
63
        for(unsigned int i=0;i<this->unichars.size();i++){
 
64
                if ((gunichar) unicode == this->unichars[i]) return true;
 
65
        }
 
66
 
 
67
        unsigned int unival;
 
68
        unival = g_utf8_get_char (&unicode);
 
69
        char uni[9] = "00000000";
 
70
        uni[8]= '\0';
 
71
        unsigned char val;
 
72
        for (unsigned int i=7; unival>0; i--){
 
73
                val = unival & 0xf;
 
74
                unival = unival >> 4;
 
75
                if (val < 10) uni[i] = '0' + val;
 
76
                else uni[i] = 'A'+ val - 10;
 
77
        }
 
78
 
 
79
        bool found;
 
80
        for(unsigned int i=0;i<this->range.size();i++){
 
81
                Urange r = this->range[i];
 
82
                if (r.end){
 
83
                        if (unival >= hex2int(r.start) && unival <= hex2int(r.end)) return true;
 
84
                } else {
 
85
                        found = true;
 
86
 
 
87
                        int p=0;
 
88
                        while (r.start[p]!='\0') p++;
 
89
                        p--;
 
90
 
 
91
                        for (int pos=8;p>=0;pos--,p--){
 
92
                                if (uni[pos]!='?' && uni[pos]!=r.start[p]) found = false;
 
93
                        }
 
94
                        if (found) return true;
 
95
                }
 
96
        }
 
97
        return false;
100
98
}
101
99
 
102
100
Glib::ustring UnicodeRange::attribute_string(){
103
 
    Glib::ustring result;
104
 
    unsigned int i;
105
 
    for(i=0; i<this->unichars.size(); i++){
106
 
        result += this->unichars[i];
107
 
        if (i!=this->unichars.size()-1) result += ",";
108
 
    }
109
 
 
110
 
    for(i=0; i<this->range.size(); i++){
111
 
        result += "U+" + Glib::ustring(this->range[i].start);
112
 
        if (this->range[i].end) result += "-" + Glib::ustring(this->range[i].end);
113
 
        if (i!=this->range.size()-1) result += ", ";
114
 
    }
115
 
 
116
 
    return result;
 
101
        Glib::ustring result;
 
102
        unsigned int i;
 
103
        for(i=0; i<this->unichars.size(); i++){
 
104
                result += this->unichars[i];
 
105
                if (i!=this->unichars.size()-1) result += ",";
 
106
        }
 
107
 
 
108
        for(i=0; i<this->range.size(); i++){
 
109
                result += "U+" + Glib::ustring(this->range[i].start);
 
110
                if (this->range[i].end) result += "-" + Glib::ustring(this->range[i].end);
 
111
                if (i!=this->range.size()-1) result += ", ";
 
112
        }
 
113
 
 
114
        return result;
117
115
}
118
116
 
119
117
gunichar UnicodeRange::sample_glyph(){
120
 
    //This could be better
121
 
    if (!unichars.empty())
122
 
        return unichars[0];
123
 
    if (!range.empty())
124
 
        return hex2int(range[0].start);
125
 
    return (gunichar) ' ';
 
118
        //This could be better
 
119
        if (unichars.size())
 
120
                return unichars[0];
 
121
        if (range.size())
 
122
                return hex2int(range[0].start);
 
123
        return (gunichar) ' ';
126
124
}
127
125