12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469 |
- /*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
- /* $Id$ */
-
- package org.apache.fop.complexscripts.fonts;
-
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
-
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
-
- import org.apache.fop.complexscripts.scripts.ScriptProcessor;
- import org.apache.fop.complexscripts.util.CharAssociation;
- import org.apache.fop.complexscripts.util.GlyphSequence;
- import org.apache.fop.complexscripts.util.GlyphTester;
-
- // CSOFF: LineLengthCheck
-
- /**
- * <p>The <code>GlyphSubstitutionTable</code> class is a glyph table that implements
- * <code>GlyphSubstitution</code> functionality.</p>
- *
- * <p>This work was originally authored by Glenn Adams (gadams@apache.org).</p>
- */
- public class GlyphSubstitutionTable extends GlyphTable {
-
- /** logging instance */
- private static final Log log = LogFactory.getLog(GlyphSubstitutionTable.class);
-
- /** single substitution subtable type */
- public static final int GSUB_LOOKUP_TYPE_SINGLE = 1;
- /** multiple substitution subtable type */
- public static final int GSUB_LOOKUP_TYPE_MULTIPLE = 2;
- /** alternate substitution subtable type */
- public static final int GSUB_LOOKUP_TYPE_ALTERNATE = 3;
- /** ligature substitution subtable type */
- public static final int GSUB_LOOKUP_TYPE_LIGATURE = 4;
- /** contextual substitution subtable type */
- public static final int GSUB_LOOKUP_TYPE_CONTEXTUAL = 5;
- /** chained contextual substitution subtable type */
- public static final int GSUB_LOOKUP_TYPE_CHAINED_CONTEXTUAL = 6;
- /** extension substitution substitution subtable type */
- public static final int GSUB_LOOKUP_TYPE_EXTENSION_SUBSTITUTION = 7;
- /** reverse chained contextual single substitution subtable type */
- public static final int GSUB_LOOKUP_TYPE_REVERSE_CHAINED_SINGLE = 8;
-
- /**
- * Instantiate a <code>GlyphSubstitutionTable</code> object using the specified lookups
- * and subtables.
- * @param gdef glyph definition table that applies
- * @param lookups a map of lookup specifications to subtable identifier strings
- * @param subtables a list of identified subtables
- */
- public GlyphSubstitutionTable(GlyphDefinitionTable gdef, Map lookups, List subtables) {
- super(gdef, lookups);
- if ((subtables == null) || (subtables.size() == 0)) {
- throw new AdvancedTypographicTableFormatException("subtables must be non-empty");
- } else {
- for (Object o : subtables) {
- if (o instanceof GlyphSubstitutionSubtable) {
- addSubtable((GlyphSubtable) o);
- } else {
- throw new AdvancedTypographicTableFormatException("subtable must be a glyph substitution subtable");
- }
- }
- freezeSubtables();
- }
- }
-
- /**
- * Perform substitution processing using all matching lookups.
- * @param gs an input glyph sequence
- * @param script a script identifier
- * @param language a language identifier
- * @return the substituted (output) glyph sequence
- */
- public GlyphSequence substitute(GlyphSequence gs, String script, String language) {
- GlyphSequence ogs;
- Map/*<LookupSpec,List<LookupTable>>*/ lookups = matchLookups(script, language, "*");
- if ((lookups != null) && (lookups.size() > 0)) {
- ScriptProcessor sp = ScriptProcessor.getInstance(script);
- ogs = sp.substitute(this, gs, script, language, lookups);
- } else {
- ogs = gs;
- }
- return ogs;
- }
-
- /**
- * Map a lookup type name to its constant (integer) value.
- * @param name lookup type name
- * @return lookup type
- */
- public static int getLookupTypeFromName(String name) {
- int t;
- String s = name.toLowerCase();
- if ("single".equals(s)) {
- t = GSUB_LOOKUP_TYPE_SINGLE;
- } else if ("multiple".equals(s)) {
- t = GSUB_LOOKUP_TYPE_MULTIPLE;
- } else if ("alternate".equals(s)) {
- t = GSUB_LOOKUP_TYPE_ALTERNATE;
- } else if ("ligature".equals(s)) {
- t = GSUB_LOOKUP_TYPE_LIGATURE;
- } else if ("contextual".equals(s)) {
- t = GSUB_LOOKUP_TYPE_CONTEXTUAL;
- } else if ("chainedcontextual".equals(s)) {
- t = GSUB_LOOKUP_TYPE_CHAINED_CONTEXTUAL;
- } else if ("extensionsubstitution".equals(s)) {
- t = GSUB_LOOKUP_TYPE_EXTENSION_SUBSTITUTION;
- } else if ("reversechainiingcontextualsingle".equals(s)) {
- t = GSUB_LOOKUP_TYPE_REVERSE_CHAINED_SINGLE;
- } else {
- t = -1;
- }
- return t;
- }
-
- /**
- * Map a lookup type constant (integer) value to its name.
- * @param type lookup type
- * @return lookup type name
- */
- public static String getLookupTypeName(int type) {
- String tn = null;
- switch (type) {
- case GSUB_LOOKUP_TYPE_SINGLE:
- tn = "single";
- break;
- case GSUB_LOOKUP_TYPE_MULTIPLE:
- tn = "multiple";
- break;
- case GSUB_LOOKUP_TYPE_ALTERNATE:
- tn = "alternate";
- break;
- case GSUB_LOOKUP_TYPE_LIGATURE:
- tn = "ligature";
- break;
- case GSUB_LOOKUP_TYPE_CONTEXTUAL:
- tn = "contextual";
- break;
- case GSUB_LOOKUP_TYPE_CHAINED_CONTEXTUAL:
- tn = "chainedcontextual";
- break;
- case GSUB_LOOKUP_TYPE_EXTENSION_SUBSTITUTION:
- tn = "extensionsubstitution";
- break;
- case GSUB_LOOKUP_TYPE_REVERSE_CHAINED_SINGLE:
- tn = "reversechainiingcontextualsingle";
- break;
- default:
- tn = "unknown";
- break;
- }
- return tn;
- }
-
- /**
- * Create a substitution subtable according to the specified arguments.
- * @param type subtable type
- * @param id subtable identifier
- * @param sequence subtable sequence
- * @param flags subtable flags
- * @param format subtable format
- * @param coverage subtable coverage table
- * @param entries subtable entries
- * @return a glyph subtable instance
- */
- public static GlyphSubtable createSubtable(int type, String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- GlyphSubtable st = null;
- switch (type) {
- case GSUB_LOOKUP_TYPE_SINGLE:
- st = SingleSubtable.create(id, sequence, flags, format, coverage, entries);
- break;
- case GSUB_LOOKUP_TYPE_MULTIPLE:
- st = MultipleSubtable.create(id, sequence, flags, format, coverage, entries);
- break;
- case GSUB_LOOKUP_TYPE_ALTERNATE:
- st = AlternateSubtable.create(id, sequence, flags, format, coverage, entries);
- break;
- case GSUB_LOOKUP_TYPE_LIGATURE:
- st = LigatureSubtable.create(id, sequence, flags, format, coverage, entries);
- break;
- case GSUB_LOOKUP_TYPE_CONTEXTUAL:
- st = ContextualSubtable.create(id, sequence, flags, format, coverage, entries);
- break;
- case GSUB_LOOKUP_TYPE_CHAINED_CONTEXTUAL:
- st = ChainedContextualSubtable.create(id, sequence, flags, format, coverage, entries);
- break;
- case GSUB_LOOKUP_TYPE_REVERSE_CHAINED_SINGLE:
- st = ReverseChainedSingleSubtable.create(id, sequence, flags, format, coverage, entries);
- break;
- default:
- break;
- }
- return st;
- }
-
- /**
- * Create a substitution subtable according to the specified arguments.
- * @param type subtable type
- * @param id subtable identifier
- * @param sequence subtable sequence
- * @param flags subtable flags
- * @param format subtable format
- * @param coverage list of coverage table entries
- * @param entries subtable entries
- * @return a glyph subtable instance
- */
- public static GlyphSubtable createSubtable(int type, String id, int sequence, int flags, int format, List coverage, List entries) {
- return createSubtable(type, id, sequence, flags, format, GlyphCoverageTable.createCoverageTable(coverage), entries);
- }
-
- private abstract static class SingleSubtable extends GlyphSubstitutionSubtable {
- SingleSubtable(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- super(id, sequence, flags, format, coverage);
- }
- /** {@inheritDoc} */
- public int getType() {
- return GSUB_LOOKUP_TYPE_SINGLE;
- }
- /** {@inheritDoc} */
- public boolean isCompatible(GlyphSubtable subtable) {
- return subtable instanceof SingleSubtable;
- }
- /** {@inheritDoc} */
- public boolean substitute(GlyphSubstitutionState ss) {
- int gi = ss.getGlyph();
- int ci;
- if ((ci = getCoverageIndex(gi)) < 0) {
- return false;
- } else {
- int go = getGlyphForCoverageIndex(ci, gi);
- if ((go < 0) || (go > 65535)) {
- go = 65535;
- }
- ss.putGlyph(go, ss.getAssociation(), Boolean.TRUE);
- ss.consume(1);
- return true;
- }
- }
- /**
- * Obtain glyph for coverage index.
- * @param ci coverage index
- * @param gi original glyph index
- * @return substituted glyph value
- * @throws IllegalArgumentException if coverage index is not valid
- */
- public abstract int getGlyphForCoverageIndex(int ci, int gi) throws IllegalArgumentException;
- static GlyphSubstitutionSubtable create(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- if (format == 1) {
- return new SingleSubtableFormat1(id, sequence, flags, format, coverage, entries);
- } else if (format == 2) {
- return new SingleSubtableFormat2(id, sequence, flags, format, coverage, entries);
- } else {
- throw new UnsupportedOperationException();
- }
- }
- }
-
- private static class SingleSubtableFormat1 extends SingleSubtable {
- private int delta;
- private int ciMax;
- SingleSubtableFormat1(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- super(id, sequence, flags, format, coverage, entries);
- populate(entries);
- }
- /** {@inheritDoc} */
- public List getEntries() {
- List entries = new ArrayList(1);
- entries.add(delta);
- return entries;
- }
- /** {@inheritDoc} */
- public int getGlyphForCoverageIndex(int ci, int gi) throws IllegalArgumentException {
- if (ci <= ciMax) {
- return gi + delta;
- } else {
- throw new IllegalArgumentException("coverage index " + ci + " out of range, maximum coverage index is " + ciMax);
- }
- }
- private void populate(List entries) {
- if ((entries == null) || (entries.size() != 1)) {
- throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null and contain exactly one entry");
- } else {
- Object o = entries.get(0);
- int delta = 0;
- if (o instanceof Integer) {
- delta = (Integer) o;
- } else {
- throw new AdvancedTypographicTableFormatException("illegal entries entry, must be Integer, but is: " + o);
- }
- this.delta = delta;
- this.ciMax = getCoverageSize() - 1;
- }
- }
- }
-
- private static class SingleSubtableFormat2 extends SingleSubtable {
- private int[] glyphs;
- SingleSubtableFormat2(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- super(id, sequence, flags, format, coverage, entries);
- populate(entries);
- }
- /** {@inheritDoc} */
- public List getEntries() {
- List entries = new ArrayList(glyphs.length);
- for (int glyph : glyphs) {
- entries.add(glyph);
- }
- return entries;
- }
- /** {@inheritDoc} */
- public int getGlyphForCoverageIndex(int ci, int gi) throws IllegalArgumentException {
- if (glyphs == null) {
- return -1;
- } else if (ci >= glyphs.length) {
- throw new IllegalArgumentException("coverage index " + ci + " out of range, maximum coverage index is " + glyphs.length);
- } else {
- return glyphs [ ci ];
- }
- }
- private void populate(List entries) {
- int i = 0;
- int n = entries.size();
- int[] glyphs = new int [ n ];
- for (Object o : entries) {
- if (o instanceof Integer) {
- int gid = (Integer) o;
- if ((gid >= 0) && (gid < 65536)) {
- glyphs[i++] = gid;
- } else {
- throw new AdvancedTypographicTableFormatException("illegal glyph index: " + gid);
- }
- } else {
- throw new AdvancedTypographicTableFormatException("illegal entries entry, must be Integer: " + o);
- }
- }
- assert i == n;
- assert this.glyphs == null;
- this.glyphs = glyphs;
- }
- }
-
- private abstract static class MultipleSubtable extends GlyphSubstitutionSubtable {
- public MultipleSubtable(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- super(id, sequence, flags, format, coverage);
- }
- /** {@inheritDoc} */
- public int getType() {
- return GSUB_LOOKUP_TYPE_MULTIPLE;
- }
- /** {@inheritDoc} */
- public boolean isCompatible(GlyphSubtable subtable) {
- return subtable instanceof MultipleSubtable;
- }
- /** {@inheritDoc} */
- public boolean substitute(GlyphSubstitutionState ss) {
- int gi = ss.getGlyph();
- int ci;
- if ((ci = getCoverageIndex(gi)) < 0) {
- return false;
- } else {
- int[] ga = getGlyphsForCoverageIndex(ci, gi);
- if (ga != null) {
- ss.putGlyphs(ga, CharAssociation.replicate(ss.getAssociation(), ga.length), Boolean.TRUE);
- ss.consume(1);
- }
- return true;
- }
- }
- /**
- * Obtain glyph sequence for coverage index.
- * @param ci coverage index
- * @param gi original glyph index
- * @return sequence of glyphs to substitute for input glyph
- * @throws IllegalArgumentException if coverage index is not valid
- */
- public abstract int[] getGlyphsForCoverageIndex(int ci, int gi) throws IllegalArgumentException;
- static GlyphSubstitutionSubtable create(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- if (format == 1) {
- return new MultipleSubtableFormat1(id, sequence, flags, format, coverage, entries);
- } else {
- throw new UnsupportedOperationException();
- }
- }
- }
-
- private static class MultipleSubtableFormat1 extends MultipleSubtable {
- private int[][] gsa; // glyph sequence array, ordered by coverage index
- MultipleSubtableFormat1(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- super(id, sequence, flags, format, coverage, entries);
- populate(entries);
- }
- /** {@inheritDoc} */
- public List getEntries() {
- if (gsa != null) {
- List entries = new ArrayList(1);
- entries.add(gsa);
- return entries;
- } else {
- return null;
- }
- }
- /** {@inheritDoc} */
- public int[] getGlyphsForCoverageIndex(int ci, int gi) throws IllegalArgumentException {
- if (gsa == null) {
- return null;
- } else if (ci >= gsa.length) {
- throw new IllegalArgumentException("coverage index " + ci + " out of range, maximum coverage index is " + gsa.length);
- } else {
- return gsa [ ci ];
- }
- }
- private void populate(List entries) {
- if (entries == null) {
- throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
- } else if (entries.size() != 1) {
- throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 1 entry");
- } else {
- Object o;
- if (((o = entries.get(0)) == null) || !(o instanceof int[][])) {
- throw new AdvancedTypographicTableFormatException("illegal entries, first entry must be an int[][], but is: " + ((o != null) ? o.getClass() : null));
- } else {
- gsa = (int[][]) o;
- }
- }
- }
- }
-
- private abstract static class AlternateSubtable extends GlyphSubstitutionSubtable {
- public AlternateSubtable(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- super(id, sequence, flags, format, coverage);
- }
- /** {@inheritDoc} */
- public int getType() {
- return GSUB_LOOKUP_TYPE_ALTERNATE;
- }
- /** {@inheritDoc} */
- public boolean isCompatible(GlyphSubtable subtable) {
- return subtable instanceof AlternateSubtable;
- }
- /** {@inheritDoc} */
- public boolean substitute(GlyphSubstitutionState ss) {
- int gi = ss.getGlyph();
- int ci;
- if ((ci = getCoverageIndex(gi)) < 0) {
- return false;
- } else {
- int[] ga = getAlternatesForCoverageIndex(ci, gi);
- int ai = ss.getAlternatesIndex(ci);
- int go;
- if ((ai < 0) || (ai >= ga.length)) {
- go = gi;
- } else {
- go = ga [ ai ];
- }
- if ((go < 0) || (go > 65535)) {
- go = 65535;
- }
- ss.putGlyph(go, ss.getAssociation(), Boolean.TRUE);
- ss.consume(1);
- return true;
- }
- }
- /**
- * Obtain glyph alternates for coverage index.
- * @param ci coverage index
- * @param gi original glyph index
- * @return sequence of glyphs to substitute for input glyph
- * @throws IllegalArgumentException if coverage index is not valid
- */
- public abstract int[] getAlternatesForCoverageIndex(int ci, int gi) throws IllegalArgumentException;
- static GlyphSubstitutionSubtable create(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- if (format == 1) {
- return new AlternateSubtableFormat1(id, sequence, flags, format, coverage, entries);
- } else {
- throw new UnsupportedOperationException();
- }
- }
- }
-
- private static class AlternateSubtableFormat1 extends AlternateSubtable {
- private int[][] gaa; // glyph alternates array, ordered by coverage index
- AlternateSubtableFormat1(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- super(id, sequence, flags, format, coverage, entries);
- populate(entries);
- }
- /** {@inheritDoc} */
- public List getEntries() {
- List entries = new ArrayList(gaa.length);
- for (int[] aGaa : gaa) {
- entries.add(aGaa);
- }
- return entries;
- }
- /** {@inheritDoc} */
- public int[] getAlternatesForCoverageIndex(int ci, int gi) throws IllegalArgumentException {
- if (gaa == null) {
- return null;
- } else if (ci >= gaa.length) {
- throw new IllegalArgumentException("coverage index " + ci + " out of range, maximum coverage index is " + gaa.length);
- } else {
- return gaa [ ci ];
- }
- }
- private void populate(List entries) {
- int i = 0;
- int n = entries.size();
- int[][] gaa = new int [ n ][];
- for (Object o : entries) {
- if (o instanceof int[]) {
- gaa[i++] = (int[]) o;
- } else {
- throw new AdvancedTypographicTableFormatException("illegal entries entry, must be int[]: " + o);
- }
- }
- assert i == n;
- assert this.gaa == null;
- this.gaa = gaa;
- }
- }
-
- private abstract static class LigatureSubtable extends GlyphSubstitutionSubtable {
- public LigatureSubtable(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- super(id, sequence, flags, format, coverage);
- }
- /** {@inheritDoc} */
- public int getType() {
- return GSUB_LOOKUP_TYPE_LIGATURE;
- }
- /** {@inheritDoc} */
- public boolean isCompatible(GlyphSubtable subtable) {
- return subtable instanceof LigatureSubtable;
- }
- /** {@inheritDoc} */
- public boolean substitute(GlyphSubstitutionState ss) {
- int gi = ss.getGlyph();
- int ci;
- if ((ci = getCoverageIndex(gi)) < 0) {
- return false;
- } else {
- LigatureSet ls = getLigatureSetForCoverageIndex(ci, gi);
- if (ls != null) {
- boolean reverse = false;
- GlyphTester ignores = ss.getIgnoreDefault();
- int[] counts = ss.getGlyphsAvailable(0, reverse, ignores);
- int nga = counts[0];
- int ngi;
- if (nga > 1) {
- int[] iga = ss.getGlyphs(0, nga, reverse, ignores, null, counts);
- Ligature l = findLigature(ls, iga);
- if (l != null) {
- int go = l.getLigature();
- if ((go < 0) || (go > 65535)) {
- go = 65535;
- }
- int nmg = 1 + l.getNumComponents();
- // fetch matched number of component glyphs to determine matched and ignored count
- ss.getGlyphs(0, nmg, reverse, ignores, null, counts);
- nga = counts[0];
- ngi = counts[1];
- // fetch associations of matched component glyphs
- CharAssociation[] laa = ss.getAssociations(0, nga);
- // output ligature glyph and its association
- ss.putGlyph(go, CharAssociation.join(laa), Boolean.TRUE);
- // fetch and output ignored glyphs (if necessary)
- if (ngi > 0) {
- ss.putGlyphs(ss.getIgnoredGlyphs(0, ngi), ss.getIgnoredAssociations(0, ngi), null);
- }
- ss.consume(nga + ngi);
- }
- }
- }
- return true;
- }
- }
- private Ligature findLigature(LigatureSet ls, int[] glyphs) {
- Ligature[] la = ls.getLigatures();
- int k = -1;
- int maxComponents = -1;
- for (int i = 0, n = la.length; i < n; i++) {
- Ligature l = la [ i ];
- if (l.matchesComponents(glyphs)) {
- int nc = l.getNumComponents();
- if (nc > maxComponents) {
- maxComponents = nc;
- k = i;
- }
- }
- }
- if (k >= 0) {
- return la [ k ];
- } else {
- return null;
- }
- }
- /**
- * Obtain ligature set for coverage index.
- * @param ci coverage index
- * @param gi original glyph index
- * @return ligature set (or null if none defined)
- * @throws IllegalArgumentException if coverage index is not valid
- */
- public abstract LigatureSet getLigatureSetForCoverageIndex(int ci, int gi) throws IllegalArgumentException;
- static GlyphSubstitutionSubtable create(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- if (format == 1) {
- return new LigatureSubtableFormat1(id, sequence, flags, format, coverage, entries);
- } else {
- throw new UnsupportedOperationException();
- }
- }
- }
-
- private static class LigatureSubtableFormat1 extends LigatureSubtable {
- private LigatureSet[] ligatureSets;
- public LigatureSubtableFormat1(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- super(id, sequence, flags, format, coverage, entries);
- populate(entries);
- }
- /** {@inheritDoc} */
- public List getEntries() {
- List entries = new ArrayList(ligatureSets.length);
- for (LigatureSet ligatureSet : ligatureSets) {
- entries.add(ligatureSet);
- }
- return entries;
- }
- /** {@inheritDoc} */
- public LigatureSet getLigatureSetForCoverageIndex(int ci, int gi) throws IllegalArgumentException {
- if (ligatureSets == null) {
- return null;
- } else if (ci >= ligatureSets.length) {
- throw new IllegalArgumentException("coverage index " + ci + " out of range, maximum coverage index is " + ligatureSets.length);
- } else {
- return ligatureSets [ ci ];
- }
- }
- private void populate(List entries) {
- int i = 0;
- int n = entries.size();
- LigatureSet[] ligatureSets = new LigatureSet [ n ];
- for (Object o : entries) {
- if (o instanceof LigatureSet) {
- ligatureSets[i++] = (LigatureSet) o;
- } else {
- throw new AdvancedTypographicTableFormatException("illegal ligatures entry, must be LigatureSet: " + o);
- }
- }
- assert i == n;
- assert this.ligatureSets == null;
- this.ligatureSets = ligatureSets;
- }
- }
-
- private abstract static class ContextualSubtable extends GlyphSubstitutionSubtable {
- public ContextualSubtable(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- super(id, sequence, flags, format, coverage);
- }
- /** {@inheritDoc} */
- public int getType() {
- return GSUB_LOOKUP_TYPE_CONTEXTUAL;
- }
- /** {@inheritDoc} */
- public boolean isCompatible(GlyphSubtable subtable) {
- return subtable instanceof ContextualSubtable;
- }
- /** {@inheritDoc} */
- public boolean substitute(GlyphSubstitutionState ss) {
- int gi = ss.getGlyph();
- int ci;
- if ((ci = getCoverageIndex(gi)) < 0) {
- return false;
- } else {
- int[] rv = new int[1];
- RuleLookup[] la = getLookups(ci, gi, ss, rv);
- if (la != null) {
- ss.apply(la, rv[0]);
- }
- return true;
- }
- }
- /**
- * Obtain rule lookups set associated current input glyph context.
- * @param ci coverage index of glyph at current position
- * @param gi glyph index of glyph at current position
- * @param ss glyph substitution state
- * @param rv array of ints used to receive multiple return values, must be of length 1 or greater,
- * where the first entry is used to return the input sequence length of the matched rule
- * @return array of rule lookups or null if none applies
- */
- public abstract RuleLookup[] getLookups(int ci, int gi, GlyphSubstitutionState ss, int[] rv);
- static GlyphSubstitutionSubtable create(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- if (format == 1) {
- return new ContextualSubtableFormat1(id, sequence, flags, format, coverage, entries);
- } else if (format == 2) {
- return new ContextualSubtableFormat2(id, sequence, flags, format, coverage, entries);
- } else if (format == 3) {
- return new ContextualSubtableFormat3(id, sequence, flags, format, coverage, entries);
- } else {
- throw new UnsupportedOperationException();
- }
- }
- }
-
- private static class ContextualSubtableFormat1 extends ContextualSubtable {
- private RuleSet[] rsa; // rule set array, ordered by glyph coverage index
- ContextualSubtableFormat1(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- super(id, sequence, flags, format, coverage, entries);
- populate(entries);
- }
- /** {@inheritDoc} */
- public List getEntries() {
- if (rsa != null) {
- List entries = new ArrayList(1);
- entries.add(rsa);
- return entries;
- } else {
- return null;
- }
- }
- /** {@inheritDoc} */
- public void resolveLookupReferences(Map/*<String,LookupTable>*/ lookupTables) {
- GlyphTable.resolveLookupReferences(rsa, lookupTables);
- }
- /** {@inheritDoc} */
- public RuleLookup[] getLookups(int ci, int gi, GlyphSubstitutionState ss, int[] rv) {
- assert ss != null;
- assert (rv != null) && (rv.length > 0);
- assert rsa != null;
- if (rsa.length > 0) {
- RuleSet rs = rsa [ 0 ];
- if (rs != null) {
- Rule[] ra = rs.getRules();
- for (Rule r : ra) {
- if ((r != null) && (r instanceof ChainedGlyphSequenceRule)) {
- ChainedGlyphSequenceRule cr = (ChainedGlyphSequenceRule) r;
- int[] iga = cr.getGlyphs(gi);
- if (matches(ss, iga, 0, rv)) {
- return r.getLookups();
- }
- }
- }
- }
- }
- return null;
- }
- static boolean matches(GlyphSubstitutionState ss, int[] glyphs, int offset, int[] rv) {
- if ((glyphs == null) || (glyphs.length == 0)) {
- return true; // match null or empty glyph sequence
- } else {
- boolean reverse = offset < 0;
- GlyphTester ignores = ss.getIgnoreDefault();
- int[] counts = ss.getGlyphsAvailable(offset, reverse, ignores);
- int nga = counts[0];
- int ngm = glyphs.length;
- if (nga < ngm) {
- return false; // insufficient glyphs available to match
- } else {
- int[] ga = ss.getGlyphs(offset, ngm, reverse, ignores, null, counts);
- for (int k = 0; k < ngm; k++) {
- if (ga [ k ] != glyphs [ k ]) {
- return false; // match fails at ga [ k ]
- }
- }
- if (rv != null) {
- rv[0] = counts[0] + counts[1];
- }
- return true; // all glyphs match
- }
- }
- }
- private void populate(List entries) {
- if (entries == null) {
- throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
- } else if (entries.size() != 1) {
- throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 1 entry");
- } else {
- Object o;
- if (((o = entries.get(0)) == null) || !(o instanceof RuleSet[])) {
- throw new AdvancedTypographicTableFormatException("illegal entries, first entry must be an RuleSet[], but is: " + ((o != null) ? o.getClass() : null));
- } else {
- rsa = (RuleSet[]) o;
- }
- }
- }
- }
-
- private static class ContextualSubtableFormat2 extends ContextualSubtable {
- private GlyphClassTable cdt; // class def table
- private int ngc; // class set count
- private RuleSet[] rsa; // rule set array, ordered by class number [0...ngc - 1]
- ContextualSubtableFormat2(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- super(id, sequence, flags, format, coverage, entries);
- populate(entries);
- }
- /** {@inheritDoc} */
- public List getEntries() {
- if (rsa != null) {
- List entries = new ArrayList(3);
- entries.add(cdt);
- entries.add(ngc);
- entries.add(rsa);
- return entries;
- } else {
- return null;
- }
- }
- /** {@inheritDoc} */
- public void resolveLookupReferences(Map/*<String,LookupTable>*/ lookupTables) {
- GlyphTable.resolveLookupReferences(rsa, lookupTables);
- }
- /** {@inheritDoc} */
- public RuleLookup[] getLookups(int ci, int gi, GlyphSubstitutionState ss, int[] rv) {
- assert ss != null;
- assert (rv != null) && (rv.length > 0);
- assert rsa != null;
- if (rsa.length > 0) {
- RuleSet rs = rsa [ 0 ];
- if (rs != null) {
- Rule[] ra = rs.getRules();
- for (Rule r : ra) {
- if ((r != null) && (r instanceof ChainedClassSequenceRule)) {
- ChainedClassSequenceRule cr = (ChainedClassSequenceRule) r;
- int[] ca = cr.getClasses(cdt.getClassIndex(gi, ss.getClassMatchSet(gi)));
- if (matches(ss, cdt, ca, 0, rv)) {
- return r.getLookups();
- }
- }
- }
- }
- }
- return null;
- }
- static boolean matches(GlyphSubstitutionState ss, GlyphClassTable cdt, int[] classes, int offset, int[] rv) {
- if ((cdt == null) || (classes == null) || (classes.length == 0)) {
- return true; // match null class definitions, null or empty class sequence
- } else {
- boolean reverse = offset < 0;
- GlyphTester ignores = ss.getIgnoreDefault();
- int[] counts = ss.getGlyphsAvailable(offset, reverse, ignores);
- int nga = counts[0];
- int ngm = classes.length;
- if (nga < ngm) {
- return false; // insufficient glyphs available to match
- } else {
- int[] ga = ss.getGlyphs(offset, ngm, reverse, ignores, null, counts);
- for (int k = 0; k < ngm; k++) {
- int gi = ga [ k ];
- int ms = ss.getClassMatchSet(gi);
- int gc = cdt.getClassIndex(gi, ms);
- if ((gc < 0) || (gc >= cdt.getClassSize(ms))) {
- return false; // none or invalid class fails mat ch
- } else if (gc != classes [ k ]) {
- return false; // match fails at ga [ k ]
- }
- }
- if (rv != null) {
- rv[0] = counts[0] + counts[1];
- }
- return true; // all glyphs match
- }
- }
- }
- private void populate(List entries) {
- if (entries == null) {
- throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
- } else if (entries.size() != 3) {
- throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 3 entries");
- } else {
- Object o;
- if (((o = entries.get(0)) == null) || !(o instanceof GlyphClassTable)) {
- throw new AdvancedTypographicTableFormatException("illegal entries, first entry must be an GlyphClassTable, but is: " + ((o != null) ? o.getClass() : null));
- } else {
- cdt = (GlyphClassTable) o;
- }
- if (((o = entries.get(1)) == null) || !(o instanceof Integer)) {
- throw new AdvancedTypographicTableFormatException("illegal entries, second entry must be an Integer, but is: " + ((o != null) ? o.getClass() : null));
- } else {
- ngc = (Integer) (o);
- }
- if (((o = entries.get(2)) == null) || !(o instanceof RuleSet[])) {
- throw new AdvancedTypographicTableFormatException("illegal entries, third entry must be an RuleSet[], but is: " + ((o != null) ? o.getClass() : null));
- } else {
- rsa = (RuleSet[]) o;
- if (rsa.length != ngc) {
- throw new AdvancedTypographicTableFormatException("illegal entries, RuleSet[] length is " + rsa.length + ", but expected " + ngc + " glyph classes");
- }
- }
- }
- }
- }
-
- private static class ContextualSubtableFormat3 extends ContextualSubtable {
- private RuleSet[] rsa; // rule set array, containing a single rule set
- ContextualSubtableFormat3(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- super(id, sequence, flags, format, coverage, entries);
- populate(entries);
- }
- /** {@inheritDoc} */
- public List getEntries() {
- if (rsa != null) {
- List entries = new ArrayList(1);
- entries.add(rsa);
- return entries;
- } else {
- return null;
- }
- }
- /** {@inheritDoc} */
- public void resolveLookupReferences(Map/*<String,LookupTable>*/ lookupTables) {
- GlyphTable.resolveLookupReferences(rsa, lookupTables);
- }
- /** {@inheritDoc} */
- public RuleLookup[] getLookups(int ci, int gi, GlyphSubstitutionState ss, int[] rv) {
- assert ss != null;
- assert (rv != null) && (rv.length > 0);
- assert rsa != null;
- if (rsa.length > 0) {
- RuleSet rs = rsa [ 0 ];
- if (rs != null) {
- Rule[] ra = rs.getRules();
- for (Rule r : ra) {
- if ((r != null) && (r instanceof ChainedCoverageSequenceRule)) {
- ChainedCoverageSequenceRule cr = (ChainedCoverageSequenceRule) r;
- GlyphCoverageTable[] gca = cr.getCoverages();
- if (matches(ss, gca, 0, rv)) {
- return r.getLookups();
- }
- }
- }
- }
- }
- return null;
- }
- static boolean matches(GlyphSubstitutionState ss, GlyphCoverageTable[] gca, int offset, int[] rv) {
- if ((gca == null) || (gca.length == 0)) {
- return true; // match null or empty coverage array
- } else {
- boolean reverse = offset < 0;
- GlyphTester ignores = ss.getIgnoreDefault();
- int[] counts = ss.getGlyphsAvailable(offset, reverse, ignores);
- int nga = counts[0];
- int ngm = gca.length;
- if (nga < ngm) {
- return false; // insufficient glyphs available to match
- } else {
- int[] ga = ss.getGlyphs(offset, ngm, reverse, ignores, null, counts);
- for (int k = 0; k < ngm; k++) {
- GlyphCoverageTable ct = gca [ k ];
- if (ct != null) {
- if (ct.getCoverageIndex(ga [ k ]) < 0) {
- return false; // match fails at ga [ k ]
- }
- }
- }
- if (rv != null) {
- rv[0] = counts[0] + counts[1];
- }
- return true; // all glyphs match
- }
- }
- }
- private void populate(List entries) {
- if (entries == null) {
- throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
- } else if (entries.size() != 1) {
- throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 1 entry");
- } else {
- Object o;
- if (((o = entries.get(0)) == null) || !(o instanceof RuleSet[])) {
- throw new AdvancedTypographicTableFormatException("illegal entries, first entry must be an RuleSet[], but is: " + ((o != null) ? o.getClass() : null));
- } else {
- rsa = (RuleSet[]) o;
- }
- }
- }
- }
-
- private abstract static class ChainedContextualSubtable extends GlyphSubstitutionSubtable {
- public ChainedContextualSubtable(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- super(id, sequence, flags, format, coverage);
- }
- /** {@inheritDoc} */
- public int getType() {
- return GSUB_LOOKUP_TYPE_CHAINED_CONTEXTUAL;
- }
- /** {@inheritDoc} */
- public boolean isCompatible(GlyphSubtable subtable) {
- return subtable instanceof ChainedContextualSubtable;
- }
- /** {@inheritDoc} */
- public boolean substitute(GlyphSubstitutionState ss) {
- int gi = ss.getGlyph();
- int ci;
- if ((ci = getCoverageIndex(gi)) < 0) {
- return false;
- } else {
- int[] rv = new int[1];
- RuleLookup[] la = getLookups(ci, gi, ss, rv);
- if (la != null) {
- ss.apply(la, rv[0]);
- return true;
- } else {
- return false;
- }
- }
- }
- /**
- * Obtain rule lookups set associated current input glyph context.
- * @param ci coverage index of glyph at current position
- * @param gi glyph index of glyph at current position
- * @param ss glyph substitution state
- * @param rv array of ints used to receive multiple return values, must be of length 1 or greater
- * @return array of rule lookups or null if none applies
- */
- public abstract RuleLookup[] getLookups(int ci, int gi, GlyphSubstitutionState ss, int[] rv);
- static GlyphSubstitutionSubtable create(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- if (format == 1) {
- return new ChainedContextualSubtableFormat1(id, sequence, flags, format, coverage, entries);
- } else if (format == 2) {
- return new ChainedContextualSubtableFormat2(id, sequence, flags, format, coverage, entries);
- } else if (format == 3) {
- return new ChainedContextualSubtableFormat3(id, sequence, flags, format, coverage, entries);
- } else {
- throw new UnsupportedOperationException();
- }
- }
- }
-
- private static class ChainedContextualSubtableFormat1 extends ChainedContextualSubtable {
- private RuleSet[] rsa; // rule set array, ordered by glyph coverage index
- ChainedContextualSubtableFormat1(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- super(id, sequence, flags, format, coverage, entries);
- populate(entries);
- }
- /** {@inheritDoc} */
- public List getEntries() {
- if (rsa != null) {
- List entries = new ArrayList(1);
- entries.add(rsa);
- return entries;
- } else {
- return null;
- }
- }
- /** {@inheritDoc} */
- public void resolveLookupReferences(Map/*<String,LookupTable>*/ lookupTables) {
- GlyphTable.resolveLookupReferences(rsa, lookupTables);
- }
- /** {@inheritDoc} */
- public RuleLookup[] getLookups(int ci, int gi, GlyphSubstitutionState ss, int[] rv) {
- assert ss != null;
- assert (rv != null) && (rv.length > 0);
- assert rsa != null;
- if (rsa.length > 0) {
- RuleSet rs = rsa [ 0 ];
- if (rs != null) {
- Rule[] ra = rs.getRules();
- for (Rule r : ra) {
- if ((r != null) && (r instanceof ChainedGlyphSequenceRule)) {
- ChainedGlyphSequenceRule cr = (ChainedGlyphSequenceRule) r;
- int[] iga = cr.getGlyphs(gi);
- if (matches(ss, iga, 0, rv)) {
- int[] bga = cr.getBacktrackGlyphs();
- if (matches(ss, bga, -1, null)) {
- int[] lga = cr.getLookaheadGlyphs();
- if (matches(ss, lga, rv[0], null)) {
- return r.getLookups();
- }
- }
- }
- }
- }
- }
- }
- return null;
- }
- private boolean matches(GlyphSubstitutionState ss, int[] glyphs, int offset, int[] rv) {
- return ContextualSubtableFormat1.matches(ss, glyphs, offset, rv);
- }
- private void populate(List entries) {
- if (entries == null) {
- throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
- } else if (entries.size() != 1) {
- throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 1 entry");
- } else {
- Object o;
- if (((o = entries.get(0)) == null) || !(o instanceof RuleSet[])) {
- throw new AdvancedTypographicTableFormatException("illegal entries, first entry must be an RuleSet[], but is: " + ((o != null) ? o.getClass() : null));
- } else {
- rsa = (RuleSet[]) o;
- }
- }
- }
- }
-
- private static class ChainedContextualSubtableFormat2 extends ChainedContextualSubtable {
- private GlyphClassTable icdt; // input class def table
- private GlyphClassTable bcdt; // backtrack class def table
- private GlyphClassTable lcdt; // lookahead class def table
- private int ngc; // class set count
- private RuleSet[] rsa; // rule set array, ordered by class number [0...ngc - 1]
- ChainedContextualSubtableFormat2(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- super(id, sequence, flags, format, coverage, entries);
- populate(entries);
- }
- /** {@inheritDoc} */
- public List getEntries() {
- if (rsa != null) {
- List entries = new ArrayList(5);
- entries.add(icdt);
- entries.add(bcdt);
- entries.add(lcdt);
- entries.add(ngc);
- entries.add(rsa);
- return entries;
- } else {
- return null;
- }
- }
- /** {@inheritDoc} */
- public RuleLookup[] getLookups(int ci, int gi, GlyphSubstitutionState ss, int[] rv) {
- assert ss != null;
- assert (rv != null) && (rv.length > 0);
- assert rsa != null;
- if (rsa.length > 0) {
- RuleSet rs = rsa [ 0 ];
- if (rs != null) {
- Rule[] ra = rs.getRules();
- for (Rule r : ra) {
- if ((r != null) && (r instanceof ChainedClassSequenceRule)) {
- ChainedClassSequenceRule cr = (ChainedClassSequenceRule) r;
- int[] ica = cr.getClasses(icdt.getClassIndex(gi, ss.getClassMatchSet(gi)));
- if (matches(ss, icdt, ica, 0, rv)) {
- int[] bca = cr.getBacktrackClasses();
- if (matches(ss, bcdt, bca, -1, null)) {
- int[] lca = cr.getLookaheadClasses();
- if (matches(ss, lcdt, lca, rv[0], null)) {
- return r.getLookups();
- }
- }
- }
- }
- }
- }
- }
- return null;
- }
- private boolean matches(GlyphSubstitutionState ss, GlyphClassTable cdt, int[] classes, int offset, int[] rv) {
- return ContextualSubtableFormat2.matches(ss, cdt, classes, offset, rv);
- }
- /** {@inheritDoc} */
- public void resolveLookupReferences(Map/*<String,LookupTable>*/ lookupTables) {
- GlyphTable.resolveLookupReferences(rsa, lookupTables);
- }
- private void populate(List entries) {
- if (entries == null) {
- throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
- } else if (entries.size() != 5) {
- throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 5 entries");
- } else {
- Object o;
- if (((o = entries.get(0)) == null) || !(o instanceof GlyphClassTable)) {
- throw new AdvancedTypographicTableFormatException("illegal entries, first entry must be an GlyphClassTable, but is: " + ((o != null) ? o.getClass() : null));
- } else {
- icdt = (GlyphClassTable) o;
- }
- if (((o = entries.get(1)) != null) && !(o instanceof GlyphClassTable)) {
- throw new AdvancedTypographicTableFormatException("illegal entries, second entry must be an GlyphClassTable, but is: " + o.getClass());
- } else {
- bcdt = (GlyphClassTable) o;
- }
- if (((o = entries.get(2)) != null) && !(o instanceof GlyphClassTable)) {
- throw new AdvancedTypographicTableFormatException("illegal entries, third entry must be an GlyphClassTable, but is: " + o.getClass());
- } else {
- lcdt = (GlyphClassTable) o;
- }
- if (((o = entries.get(3)) == null) || !(o instanceof Integer)) {
- throw new AdvancedTypographicTableFormatException("illegal entries, fourth entry must be an Integer, but is: " + ((o != null) ? o.getClass() : null));
- } else {
- ngc = (Integer) (o);
- }
- if (((o = entries.get(4)) == null) || !(o instanceof RuleSet[])) {
- throw new AdvancedTypographicTableFormatException("illegal entries, fifth entry must be an RuleSet[], but is: " + ((o != null) ? o.getClass() : null));
- } else {
- rsa = (RuleSet[]) o;
- if (rsa.length != ngc) {
- throw new AdvancedTypographicTableFormatException("illegal entries, RuleSet[] length is " + rsa.length + ", but expected " + ngc + " glyph classes");
- }
- }
- }
- }
- }
-
- private static class ChainedContextualSubtableFormat3 extends ChainedContextualSubtable {
- private RuleSet[] rsa; // rule set array, containing a single rule set
- ChainedContextualSubtableFormat3(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- super(id, sequence, flags, format, coverage, entries);
- populate(entries);
- }
- /** {@inheritDoc} */
- public List getEntries() {
- if (rsa != null) {
- List entries = new ArrayList(1);
- entries.add(rsa);
- return entries;
- } else {
- return null;
- }
- }
- /** {@inheritDoc} */
- public void resolveLookupReferences(Map/*<String,LookupTable>*/ lookupTables) {
- GlyphTable.resolveLookupReferences(rsa, lookupTables);
- }
- /** {@inheritDoc} */
- public RuleLookup[] getLookups(int ci, int gi, GlyphSubstitutionState ss, int[] rv) {
- assert ss != null;
- assert (rv != null) && (rv.length > 0);
- assert rsa != null;
- if (rsa.length > 0) {
- RuleSet rs = rsa [ 0 ];
- if (rs != null) {
- Rule[] ra = rs.getRules();
- for (Rule r : ra) {
- if ((r != null) && (r instanceof ChainedCoverageSequenceRule)) {
- ChainedCoverageSequenceRule cr = (ChainedCoverageSequenceRule) r;
- GlyphCoverageTable[] igca = cr.getCoverages();
- if (matches(ss, igca, 0, rv)) {
- GlyphCoverageTable[] bgca = cr.getBacktrackCoverages();
- if (matches(ss, bgca, -1, null)) {
- GlyphCoverageTable[] lgca = cr.getLookaheadCoverages();
- if (matches(ss, lgca, rv[0], null)) {
- return r.getLookups();
- }
- }
- }
- }
- }
- }
- }
- return null;
- }
- private boolean matches(GlyphSubstitutionState ss, GlyphCoverageTable[] gca, int offset, int[] rv) {
- return ContextualSubtableFormat3.matches(ss, gca, offset, rv);
- }
- private void populate(List entries) {
- if (entries == null) {
- throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
- } else if (entries.size() != 1) {
- throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 1 entry");
- } else {
- Object o;
- if (((o = entries.get(0)) == null) || !(o instanceof RuleSet[])) {
- throw new AdvancedTypographicTableFormatException("illegal entries, first entry must be an RuleSet[], but is: " + ((o != null) ? o.getClass() : null));
- } else {
- rsa = (RuleSet[]) o;
- }
- }
- }
- }
-
- private abstract static class ReverseChainedSingleSubtable extends GlyphSubstitutionSubtable {
- public ReverseChainedSingleSubtable(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- super(id, sequence, flags, format, coverage);
- }
- /** {@inheritDoc} */
- public int getType() {
- return GSUB_LOOKUP_TYPE_REVERSE_CHAINED_SINGLE;
- }
- /** {@inheritDoc} */
- public boolean isCompatible(GlyphSubtable subtable) {
- return subtable instanceof ReverseChainedSingleSubtable;
- }
- /** {@inheritDoc} */
- public boolean usesReverseScan() {
- return true;
- }
- static GlyphSubstitutionSubtable create(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- if (format == 1) {
- return new ReverseChainedSingleSubtableFormat1(id, sequence, flags, format, coverage, entries);
- } else {
- throw new UnsupportedOperationException();
- }
- }
- }
-
- private static class ReverseChainedSingleSubtableFormat1 extends ReverseChainedSingleSubtable {
- ReverseChainedSingleSubtableFormat1(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
- super(id, sequence, flags, format, coverage, entries);
- populate(entries);
- }
- /** {@inheritDoc} */
- public List getEntries() {
- return null;
- }
- private void populate(List entries) {
- }
- }
-
- /**
- * The <code>Ligature</code> class implements a ligature lookup result in terms of
- * a ligature glyph (code) and the <emph>N+1...</emph> components that comprise the ligature,
- * where the <emph>Nth</emph> component was consumed in the coverage table lookup mapping to
- * this ligature instance.
- */
- public static class Ligature {
-
- private final int ligature; // (resulting) ligature glyph
- private final int[] components; // component glyph codes (note that first component is implied)
-
- /**
- * Instantiate a ligature.
- * @param ligature glyph id
- * @param components sequence of <emph>N+1...</emph> component glyph (or character) identifiers
- */
- public Ligature(int ligature, int[] components) {
- if ((ligature < 0) || (ligature > 65535)) {
- throw new AdvancedTypographicTableFormatException("invalid ligature glyph index: " + ligature);
- } else if (components == null) {
- throw new AdvancedTypographicTableFormatException("invalid ligature components, must be non-null array");
- } else {
- for (int gc : components) {
- if ((gc < 0) || (gc > 65535)) {
- throw new AdvancedTypographicTableFormatException("invalid component glyph index: " + gc);
- }
- }
- this.ligature = ligature;
- this.components = components;
- }
- }
-
- /** @return ligature glyph id */
- public int getLigature() {
- return ligature;
- }
-
- /** @return array of <emph>N+1...</emph> components */
- public int[] getComponents() {
- return components;
- }
-
- /** @return components count */
- public int getNumComponents() {
- return components.length;
- }
-
- /**
- * Determine if input sequence at offset matches ligature's components.
- * @param glyphs array of glyph components to match (including first, implied glyph)
- * @return true if matches
- */
- public boolean matchesComponents(int[] glyphs) {
- if (glyphs.length < (components.length + 1)) {
- return false;
- } else {
- for (int i = 0, n = components.length; i < n; i++) {
- if (glyphs [ i + 1 ] != components [ i ]) {
- return false;
- }
- }
- return true;
- }
- }
-
- /** {@inheritDoc} */
- public String toString() {
- StringBuffer sb = new StringBuffer();
- sb.append("{components={");
- for (int i = 0, n = components.length; i < n; i++) {
- if (i > 0) {
- sb.append(',');
- }
- sb.append(Integer.toString(components[i]));
- }
- sb.append("},ligature=");
- sb.append(Integer.toString(ligature));
- sb.append("}");
- return sb.toString();
- }
-
- }
-
- /**
- * The <code>LigatureSet</code> class implements a set of ligatures.
- */
- public static class LigatureSet {
-
- private final Ligature[] ligatures; // set of ligatures all of which share the first (implied) component
- private final int maxComponents; // maximum number of components (including first)
-
- /**
- * Instantiate a set of ligatures.
- * @param ligatures collection of ligatures
- */
- public LigatureSet(List ligatures) {
- this ((Ligature[]) ligatures.toArray(new Ligature [ ligatures.size() ]));
- }
-
- /**
- * Instantiate a set of ligatures.
- * @param ligatures array of ligatures
- */
- public LigatureSet(Ligature[] ligatures) {
- if (ligatures == null) {
- throw new AdvancedTypographicTableFormatException("invalid ligatures, must be non-null array");
- } else {
- this.ligatures = ligatures;
- int ncMax = -1;
- for (Ligature l : ligatures) {
- int nc = l.getNumComponents() + 1;
- if (nc > ncMax) {
- ncMax = nc;
- }
- }
- maxComponents = ncMax;
- }
- }
-
- /** @return array of ligatures in this ligature set */
- public Ligature[] getLigatures() {
- return ligatures;
- }
-
- /** @return count of ligatures in this ligature set */
- public int getNumLigatures() {
- return ligatures.length;
- }
-
- /** @return maximum number of components in one ligature (including first component) */
- public int getMaxComponents() {
- return maxComponents;
- }
-
- /** {@inheritDoc} */
- public String toString() {
- StringBuffer sb = new StringBuffer();
- sb.append("{ligs={");
- for (int i = 0, n = ligatures.length; i < n; i++) {
- if (i > 0) {
- sb.append(',');
- }
- sb.append(ligatures[i]);
- }
- sb.append("}}");
- return sb.toString();
- }
-
- }
-
- }
|