3 * (C) Copyright IBM Corp. 1998-2007 - All Rights Reserved
8 #include "OpenTypeUtilities.h"
9 #include "LEFontInstance.h"
10 #include "OpenTypeTables.h"
13 #include "ScriptAndLanguage.h"
14 #include "GlyphDefinitionTables.h"
15 #include "GlyphIterator.h"
16 #include "LookupProcessor.h"
17 #include "LEGlyphStorage.h"
22 le_uint32
LookupProcessor::applyLookupTable(const LookupTable
*lookupTable
, GlyphIterator
*glyphIterator
,
23 const LEFontInstance
*fontInstance
) const
25 le_uint16 lookupType
= SWAPW(lookupTable
->lookupType
);
26 le_uint16 subtableCount
= SWAPW(lookupTable
->subTableCount
);
27 le_int32 startPosition
= glyphIterator
->getCurrStreamPosition();
30 for (le_uint16 subtable
= 0; subtable
< subtableCount
; subtable
+= 1) {
31 const LookupSubtable
*lookupSubtable
= lookupTable
->getLookupSubtable(subtable
);
33 delta
= applySubtable(lookupSubtable
, lookupType
, glyphIterator
, fontInstance
);
39 glyphIterator
->setCurrStreamPosition(startPosition
);
45 le_int32
LookupProcessor::process(LEGlyphStorage
&glyphStorage
, GlyphPositionAdjustments
*glyphPositionAdjustments
,
46 le_bool rightToLeft
, const GlyphDefinitionTableHeader
*glyphDefinitionTableHeader
,
47 const LEFontInstance
*fontInstance
) const
49 le_int32 glyphCount
= glyphStorage
.getGlyphCount();
51 if (lookupSelectArray
== NULL
) {
55 GlyphIterator
glyphIterator(glyphStorage
, glyphPositionAdjustments
,
56 rightToLeft
, 0, 0, glyphDefinitionTableHeader
);
57 le_int32 newGlyphCount
= glyphCount
;
59 for (le_uint16 order
= 0; order
< lookupOrderCount
; order
+= 1) {
60 le_uint16 lookup
= lookupOrderArray
[order
];
61 FeatureMask selectMask
= lookupSelectArray
[lookup
];
63 if (selectMask
!= 0) {
64 const LookupTable
*lookupTable
= lookupListTable
->getLookupTable(lookup
);
65 le_uint16 lookupFlags
= SWAPW(lookupTable
->lookupFlags
);
67 glyphIterator
.reset(lookupFlags
, selectMask
);
69 while (glyphIterator
.findFeatureTag()) {
70 applyLookupTable(lookupTable
, &glyphIterator
, fontInstance
);
73 newGlyphCount
= glyphIterator
.applyInsertions();
80 le_uint32
LookupProcessor::applySingleLookup(le_uint16 lookupTableIndex
, GlyphIterator
*glyphIterator
,
81 const LEFontInstance
*fontInstance
) const
83 const LookupTable
*lookupTable
= lookupListTable
->getLookupTable(lookupTableIndex
);
84 le_uint16 lookupFlags
= SWAPW(lookupTable
->lookupFlags
);
85 GlyphIterator
tempIterator(*glyphIterator
, lookupFlags
);
86 le_uint32 delta
= applyLookupTable(lookupTable
, &tempIterator
, fontInstance
);
91 le_int32
LookupProcessor::selectLookups(const FeatureTable
*featureTable
, FeatureMask featureMask
, le_int32 order
)
93 le_uint16 lookupCount
= featureTable
? SWAPW(featureTable
->lookupCount
) : 0;
94 le_int32 store
= order
;
96 for (le_uint16 lookup
= 0; lookup
< lookupCount
; lookup
+= 1) {
97 le_uint16 lookupListIndex
= SWAPW(featureTable
->lookupListIndexArray
[lookup
]);
99 lookupSelectArray
[lookupListIndex
] |= featureMask
;
100 lookupOrderArray
[store
++] = lookupListIndex
;
103 return store
- order
;
106 LookupProcessor::LookupProcessor(const char *baseAddress
,
107 Offset scriptListOffset
, Offset featureListOffset
, Offset lookupListOffset
,
108 LETag scriptTag
, LETag languageTag
, const FeatureMap
*featureMap
, le_int32 featureMapCount
, le_bool orderFeatures
)
109 : lookupListTable(NULL
), featureListTable(NULL
), lookupSelectArray(NULL
),
110 lookupOrderArray(NULL
), lookupOrderCount(0)
112 const ScriptListTable
*scriptListTable
= NULL
;
113 const LangSysTable
*langSysTable
= NULL
;
114 le_uint16 featureCount
= 0;
115 le_uint16 lookupListCount
= 0;
116 le_uint16 requiredFeatureIndex
;
118 if (scriptListOffset
!= 0) {
119 scriptListTable
= (const ScriptListTable
*) (baseAddress
+ scriptListOffset
);
120 langSysTable
= scriptListTable
->findLanguage(scriptTag
, languageTag
);
122 if (langSysTable
!= 0) {
123 featureCount
= SWAPW(langSysTable
->featureCount
);
127 if (featureListOffset
!= 0) {
128 featureListTable
= (const FeatureListTable
*) (baseAddress
+ featureListOffset
);
131 if (lookupListOffset
!= 0) {
132 lookupListTable
= (const LookupListTable
*) (baseAddress
+ lookupListOffset
);
133 lookupListCount
= SWAPW(lookupListTable
->lookupCount
);
136 if (langSysTable
== NULL
|| featureListTable
== NULL
|| lookupListTable
== NULL
||
137 featureCount
== 0 || lookupListCount
== 0) {
141 requiredFeatureIndex
= SWAPW(langSysTable
->reqFeatureIndex
);
143 lookupSelectArray
= LE_NEW_ARRAY(FeatureMask
, lookupListCount
);
145 for (int i
= 0; i
< lookupListCount
; i
+= 1) {
146 lookupSelectArray
[i
] = 0;
149 le_int32 count
, order
= 0;
150 le_int32 featureReferences
= 0;
151 const FeatureTable
*featureTable
= NULL
;
154 const FeatureTable
*requiredFeatureTable
= NULL
;
155 LETag requiredFeatureTag
= 0x00000000U
;
157 // Count the total number of lookups referenced by all features. This will
158 // be the maximum number of entries in the lookupOrderArray. We can't use
159 // lookupListCount because some lookups might be referenced by more than
161 for (le_int32 feature
= 0; feature
< featureCount
; feature
+= 1) {
162 le_uint16 featureIndex
= SWAPW(langSysTable
->featureIndexArray
[feature
]);
164 featureTable
= featureListTable
->getFeatureTable(featureIndex
, &featureTag
);
165 featureReferences
+= SWAPW(featureTable
->lookupCount
);
168 if (requiredFeatureIndex
!= 0xFFFF) {
169 requiredFeatureTable
= featureListTable
->getFeatureTable(requiredFeatureIndex
, &requiredFeatureTag
);
170 featureReferences
+= SWAPW(featureTable
->lookupCount
);
173 lookupOrderArray
= LE_NEW_ARRAY(le_uint16
, featureReferences
);
175 for (le_int32 f
= 0; f
< featureMapCount
; f
+= 1) {
176 FeatureMap fm
= featureMap
[f
];
179 // If this is the required feature, add its lookups
180 if (requiredFeatureTag
== fm
.tag
) {
181 count
+= selectLookups(requiredFeatureTable
, fm
.mask
, order
);
185 // If we added lookups from the required feature, sort them
187 OpenTypeUtilities::sort(lookupOrderArray
, order
);
190 for (le_uint16 feature
= 0; feature
< featureCount
; feature
+= 1) {
191 le_uint16 featureIndex
= SWAPW(langSysTable
->featureIndexArray
[feature
]);
193 // don't add the required feature to the list more than once...
194 // TODO: Do we need this check? (Spec. says required feature won't be in feature list...)
195 if (featureIndex
== requiredFeatureIndex
) {
199 featureTable
= featureListTable
->getFeatureTable(featureIndex
, &featureTag
);
201 if (featureTag
== fm
.tag
) {
202 count
+= selectLookups(featureTable
, fm
.mask
, order
+ count
);
207 OpenTypeUtilities::sort(&lookupOrderArray
[order
], count
);
212 for (le_uint16 feature
= 0; feature
< featureCount
; feature
+= 1) {
213 le_uint16 featureIndex
= SWAPW(langSysTable
->featureIndexArray
[feature
]);
215 // don't add the required feature to the list more than once...
216 // NOTE: This check is commented out because the spec. says that
217 // the required feature won't be in the feature list, and because
218 // any duplicate entries will be removed below.
220 if (featureIndex
== requiredFeatureIndex
) {
225 featureTable
= featureListTable
->getFeatureTable(featureIndex
, &featureTag
);
227 if (featureTag
== fm
.tag
) {
228 order
+= selectLookups(featureTable
, fm
.mask
, order
);
234 if (!orderFeatures
&& (order
> 1)) {
235 OpenTypeUtilities::sort(lookupOrderArray
, order
);
237 // If there's no specified feature order,
238 // we will apply the lookups in the order
239 // that they're in the font. If a particular
240 // lookup may be referenced by more than one feature,
241 // it will apprear in the lookupOrderArray more than
242 // once, so remove any duplicate entries in the sorted array.
245 for (le_int32 in
= 1; in
< order
; in
+= 1) {
246 if (lookupOrderArray
[out
- 1] != lookupOrderArray
[in
]) {
248 lookupOrderArray
[out
] = lookupOrderArray
[in
];
258 lookupOrderCount
= order
;
261 LookupProcessor::LookupProcessor()
265 LookupProcessor::~LookupProcessor()
267 LE_DELETE_ARRAY(lookupOrderArray
);
268 LE_DELETE_ARRAY(lookupSelectArray
);