~ubuntu-branches/ubuntu/gutsy/icu/gutsy-updates

« back to all changes in this revision

Viewing changes to source/samples/layout/FontMap.cpp

  • Committer: Package Import Robot
  • Author(s): Jay Berkenbilt
  • Date: 2005-11-19 11:29:31 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20051119112931-vcizkrp10tli4enw
Tags: 3.4-3
Explicitly build with g++ 3.4.  The current ICU fails its test suite
with 4.0 but not with 3.4.  Future versions should work properly with
4.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 ******************************************************************************
3
 
 * Copyright (C) 1998-2001, International Business Machines Corporation and   *
4
 
 * others. All Rights Reserved.                                               *
5
 
 ******************************************************************************
6
 
 */
7
 
 
8
 
#include <stdio.h>
9
 
#include <string.h>
10
 
#include <ctype.h>
11
 
 
12
 
#include "unicode/utypes.h"
13
 
#include "unicode/uscript.h"
14
 
 
15
 
#include "layout/LETypes.h"
16
 
#include "layout/LEScripts.h"
17
 
 
18
 
#include "RenderingFontInstance.h"
19
 
#include "GUISupport.h"
20
 
#include "FontMap.h"
21
 
 
22
 
FontMap::FontMap(const char *fileName, le_int16 pointSize, GUISupport *guiSupport, RFIErrorCode &status)
23
 
    : fPointSize(pointSize), fFontCount(0), fGUISupport(guiSupport)
24
 
{
25
 
    le_int32 i, script;
26
 
 
27
 
    for (i = 0; i < scriptCodeCount; i += 1) {
28
 
        fFontIndices[i] = -1;
29
 
        fFontNames[i] = NULL;
30
 
        fFontInstances[i] = NULL;
31
 
    }
32
 
 
33
 
    if (LE_FAILURE(status)) {
34
 
        return;
35
 
    }
36
 
 
37
 
    char *c, *s, *line, buffer[BUFFER_SIZE];
38
 
    FILE *file;
39
 
 
40
 
    file = fopen(fileName, "r");
41
 
 
42
 
    if (file == NULL) {
43
 
        sprintf(errorMessage, "Could not open the font map file: %s.", fileName);
44
 
        fGUISupport->postErrorMessage(errorMessage, "Font Map Error");
45
 
        status = RFI_FONT_FILE_NOT_FOUND_ERROR;
46
 
        return;
47
 
    }
48
 
 
49
 
    while (fgets(buffer, BUFFER_SIZE, file) != NULL) {
50
 
        UScriptCode scriptCode;
51
 
        UErrorCode scriptStatus = U_ZERO_ERROR;
52
 
 
53
 
        line = strip(buffer);
54
 
        if (line[0] == '#' || line[0] == 0) {
55
 
            continue;
56
 
        }
57
 
 
58
 
        c = strchr(line, ':');
59
 
        c[0] = 0;
60
 
        s = strip(&c[1]);
61
 
 
62
 
        uscript_getCode(strip(line), &scriptCode, 1, &scriptStatus);
63
 
 
64
 
        if (U_FAILURE(scriptStatus) || scriptStatus == U_USING_FALLBACK_WARNING ||
65
 
            scriptStatus == U_USING_DEFAULT_WARNING) {
66
 
            sprintf(errorMessage, "The script name %s is invalid.", line);
67
 
            fGUISupport->postErrorMessage(errorMessage, "Font Map Error");
68
 
            status = RFI_ILLEGAL_ARGUMENT_ERROR;
69
 
            fclose(file);
70
 
            return;
71
 
        }
72
 
 
73
 
        script = (le_int32) scriptCode;
74
 
 
75
 
        if (fFontIndices[script] >= 0) {
76
 
            // FIXME: complain that this is a duplicate entry and bail (?)
77
 
            fFontIndices[script] = -1;
78
 
        }
79
 
 
80
 
        fFontIndices[script] = getFontIndex(s);
81
 
    }
82
 
 
83
 
    fclose(file);
84
 
}
85
 
 
86
 
FontMap::~FontMap()
87
 
{
88
 
    le_int32 font;
89
 
 
90
 
    for (font = 0; font < fFontCount; font += 1) {
91
 
        if (fFontNames[font] != NULL) {
92
 
            delete[] (char *) fFontNames[font];
93
 
        }
94
 
    }
95
 
 
96
 
    for (font = 0; font < fFontCount; font += 1) {
97
 
        if (fFontInstances[font] != NULL) {
98
 
            delete fFontInstances[font];
99
 
        }
100
 
    }
101
 
}
102
 
 
103
 
le_int32 FontMap::getFontIndex(const char *fontName)
104
 
{
105
 
    le_int32 index;
106
 
 
107
 
    for (index = 0; index < fFontCount; index += 1) {
108
 
        if (strcmp(fontName, fFontNames[index]) == 0) {
109
 
            return index;
110
 
        }
111
 
    }
112
 
 
113
 
    if (fFontCount < (le_int32) scriptCodeCount) {
114
 
        index = fFontCount++;
115
 
    } else {
116
 
        // The font name table is full. Since there can
117
 
        // only be scriptCodeCount fonts in use at once,
118
 
        // there should be at least one that's not being
119
 
        // reference; find it and resue it's index.
120
 
 
121
 
        for (index = 0; index < fFontCount; index += 1) {
122
 
            le_int32 script;
123
 
 
124
 
            for (script = 0; script < scriptCodeCount; script += 1) {
125
 
                if (fFontIndices[script] == index) {
126
 
                    break;
127
 
                }
128
 
            }
129
 
 
130
 
            if (script >= scriptCodeCount) {
131
 
                break;
132
 
            }
133
 
        }
134
 
    }
135
 
 
136
 
    if (index >= scriptCodeCount) {
137
 
        return -1;
138
 
    }
139
 
 
140
 
    le_int32 len = strlen(fontName);
141
 
    char *s = new char[len + 1];
142
 
 
143
 
    fFontNames[index] = strcpy(s, fontName);
144
 
    return index;
145
 
}
146
 
 
147
 
char *FontMap::strip(char *s)
148
 
{
149
 
    le_int32 start, end, len;
150
 
 
151
 
    start = 0;
152
 
    len = strlen(s);
153
 
 
154
 
    while (start < len && isspace(s[start])) {
155
 
        start += 1;
156
 
    }
157
 
 
158
 
    end = len - 1;
159
 
 
160
 
    while (end > start && isspace(s[end])) {
161
 
        end -= 1;
162
 
    }
163
 
 
164
 
    if (end < len) {
165
 
        s[end + 1] = '\0';
166
 
    }
167
 
 
168
 
    return &s[start];
169
 
}
170
 
 
171
 
const RenderingFontInstance *FontMap::getScriptFont(le_int32 scriptCode, RFIErrorCode &status)
172
 
{
173
 
    if (LE_FAILURE(status)) {
174
 
        return NULL;
175
 
    }
176
 
 
177
 
    if (scriptCode <= -1 || scriptCode >= scriptCodeCount) {
178
 
        status = RFI_ILLEGAL_ARGUMENT_ERROR;
179
 
        return NULL;
180
 
    }
181
 
 
182
 
 
183
 
    le_int32 fontIndex = fFontIndices[scriptCode];
184
 
 
185
 
    if (fontIndex < 0) {
186
 
        sprintf(errorMessage, "No font was set for script %s", uscript_getName((UScriptCode) scriptCode));
187
 
        fGUISupport->postErrorMessage(errorMessage, "Font Map Error");
188
 
        status = RFI_FONT_FILE_NOT_FOUND_ERROR;
189
 
        return NULL;
190
 
    }
191
 
 
192
 
    if (fFontInstances[fontIndex] == NULL) {
193
 
        fFontInstances[fontIndex] = openFont(fFontNames[fontIndex], fPointSize, status);
194
 
 
195
 
        if (LE_FAILURE(status)) {
196
 
            sprintf(errorMessage, "Could not open font file %s", fFontNames[fontIndex]);
197
 
            fGUISupport->postErrorMessage(errorMessage, "Font Map Error");
198
 
            return NULL;
199
 
        }
200
 
    }
201
 
 
202
 
    return fFontInstances[fontIndex];
203
 
}
204
 
 
205
 
 
206