~ubuntu-branches/ubuntu/quantal/icu/quantal

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Yves Arrouye
  • Date: 2002-03-03 15:31:13 UTC
  • Revision ID: package-import@ubuntu.com-20020303153113-3ssceqlq45xbmbnc
Tags: upstream-2.0-2.1pre20020303
ImportĀ upstreamĀ versionĀ 2.0-2.1pre20020303

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *******************************************************************************
 
3
 *
 
4
 *   Copyright (C) 1999-2001, International Business Machines
 
5
 *   Corporation and others.  All Rights Reserved.
 
6
 *
 
7
 *******************************************************************************
 
8
 *   file name:  RenderingFontInstance.cpp
 
9
 *
 
10
 *   created on: 10/22/2001
 
11
 *   created by: Eric R. Mader
 
12
 */
 
13
 
 
14
#include "LETypes.h"
 
15
#include "LEFontInstance.h"
 
16
#include "RenderingFontInstance.h"
 
17
#include "LESwaps.h"
 
18
#include "sfnt.h"
 
19
#include "cmaps.h"
 
20
 
 
21
#include <string.h>
 
22
 
 
23
RenderingFontInstance::RenderingFontInstance(void *surface, le_int16 pointSize)
 
24
  : fSurface(surface), fPointSize(pointSize), fUnitsPerEM(0), fAscent(0), fDescent(), fLeading(0),
 
25
    fTableCache(NULL), fTableCacheCurr(0), fTableCacheSize(0), fMapper(NULL)
 
26
{
 
27
    // we expect the subclass to call
 
28
    // initMapper() and initFontTableCache
 
29
}
 
30
 
 
31
RenderingFontInstance::~RenderingFontInstance()
 
32
{
 
33
    flushFontTableCache();
 
34
    delete[] fTableCache;
 
35
 
 
36
    delete fMapper;
 
37
}
 
38
 
 
39
void RenderingFontInstance::mapCharsToGlyphs(const LEUnicode chars[], le_int32 offset, le_int32 count,
 
40
                                       le_bool reverse, const LECharMapper *mapper, LEGlyphID glyphs[]) const
 
41
{
 
42
    le_int32 i, out = 0, dir = 1;
 
43
 
 
44
    if (reverse) {
 
45
        out = count - 1;
 
46
        dir = -1;
 
47
    }
 
48
 
 
49
    for (i = offset; i < offset + count; i += 1, out += dir) {
 
50
        LEUnicode16 high = chars[i];
 
51
        LEUnicode32 code = high;
 
52
 
 
53
        if (i < offset + count - 1 && high >= 0xD800 && high <= 0xDBFF) {
 
54
            LEUnicode16 low = chars[i + 1];
 
55
 
 
56
            if (low >= 0xDC00 && low <= 0xDFFF) {
 
57
                code = (high - 0xD800) * 0x400 + low - 0xDC00 + 0x10000;
 
58
            }
 
59
        }
 
60
 
 
61
        glyphs[out] = mapCharToGlyph(code, mapper);
 
62
 
 
63
        if (code >= 0x10000) {
 
64
            i += 1;
 
65
            glyphs[out += dir] = 0xFFFF;
 
66
        }
 
67
    }
 
68
}
 
69
 
 
70
LEGlyphID RenderingFontInstance::mapCharToGlyph(LEUnicode32 ch, const LECharMapper *mapper) const
 
71
{
 
72
    LEUnicode32 mappedChar = mapper->mapChar(ch);
 
73
 
 
74
    if (mappedChar == 0xFFFE || mappedChar == 0xFFFF) {
 
75
        return 0xFFFF;
 
76
    }
 
77
 
 
78
    if (mappedChar == 0x200C || mappedChar == 0x200D) {
 
79
        return 1;
 
80
    }
 
81
 
 
82
    return fMapper->unicodeToGlyph(mappedChar);
 
83
}
 
84
 
 
85
const void *RenderingFontInstance::getFontTable(LETag tableTag) const
 
86
{
 
87
    for (int i = 0; i < fTableCacheCurr; i += 1) {
 
88
        if (fTableCache[i].tag == tableTag) {
 
89
            return fTableCache[i].table;
 
90
        }
 
91
    }
 
92
 
 
93
    RenderingFontInstance *realThis = (RenderingFontInstance *) this;
 
94
 
 
95
    if (realThis->fTableCacheCurr >= realThis->fTableCacheSize) {
 
96
        le_int32 newSize = realThis->fTableCacheSize + TABLE_CACHE_GROW;
 
97
        TableCacheEntry *newTable = new TableCacheEntry[newSize];
 
98
 
 
99
        // FIXME: need a better strategy than this...
 
100
        if (newTable == NULL) {
 
101
            return NULL;
 
102
        }
 
103
 
 
104
        memcpy(newTable, realThis->fTableCache, realThis->fTableCacheSize * sizeof realThis->fTableCache[0]);
 
105
        delete[] realThis->fTableCache;
 
106
 
 
107
        for (int i = realThis->fTableCacheSize; i < newSize; i += 1) {
 
108
            newTable[i].tag = 0;
 
109
            newTable[i].table = NULL;
 
110
        }
 
111
 
 
112
        realThis->fTableCache = newTable;
 
113
        realThis->fTableCacheSize = newSize;
 
114
    }
 
115
 
 
116
    realThis->fTableCache[realThis->fTableCacheCurr].tag = tableTag;
 
117
    realThis->fTableCache[realThis->fTableCacheCurr].table = (void *) realThis->readFontTable(tableTag);
 
118
 
 
119
    return fTableCache[realThis->fTableCacheCurr++].table;
 
120
};
 
121
 
 
122
RFIErrorCode RenderingFontInstance::initMapper()
 
123
{
 
124
    LETag cmapTag = 0x636D6170; // 'cmap'
 
125
    const CMAPTable *cmap = (const CMAPTable *) readFontTable(cmapTag);
 
126
 
 
127
    if (cmap == NULL) {
 
128
        return RFI_MISSING_FONT_TABLE_ERROR;
 
129
    }
 
130
 
 
131
    fMapper = CMAPMapper::createUnicodeMapper(cmap);
 
132
 
 
133
    if (fMapper == NULL) {
 
134
        return RFI_MISSING_FONT_TABLE_ERROR;
 
135
    }
 
136
 
 
137
    return RFI_NO_ERROR;
 
138
}
 
139
 
 
140
RFIErrorCode RenderingFontInstance::initFontTableCache()
 
141
{
 
142
    fTableCacheSize = TABLE_CACHE_INIT;
 
143
    fTableCache = new TableCacheEntry[fTableCacheSize];
 
144
 
 
145
    if (fTableCache == 0) {
 
146
        return RFI_OUT_OF_MEMORY_ERROR;
 
147
    }
 
148
 
 
149
    for (int i = 0; i < fTableCacheSize; i += 1) {
 
150
        fTableCache[i].tag = 0;
 
151
        fTableCache[i].table = NULL;
 
152
    }
 
153
 
 
154
    return RFI_NO_ERROR;
 
155
}
 
156
 
 
157
void RenderingFontInstance::flushFontTableCache()
 
158
{
 
159
    for (int i = fTableCacheCurr - 1; i >= 0; i -= 1) {
 
160
        delete[] (char *) fTableCache[i].table;
 
161
    }
 
162
 
 
163
    fTableCacheCurr = 0;
 
164
}