12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273 |
- /*
- * 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.Arrays;
- import java.util.Iterator;
- 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.GlyphSequence;
- import org.apache.fop.complexscripts.util.GlyphTester;
-
- // CSOFF: LineLengthCheck
- // CSOFF: InnerAssignmentCheck
- // CSOFF: NoWhitespaceAfterCheck
- // CSOFF: ParameterNumberCheck
-
- /**
- * <p>The <code>GlyphPositioningTable</code> class is a glyph table that implements
- * <code>GlyphPositioning</code> functionality.</p>
- *
- * <p>This work was originally authored by Glenn Adams (gadams@apache.org).</p>
- */
- public class GlyphPositioningTable extends GlyphTable {
-
- /** logging instance */
- private static final Log log = LogFactory.getLog(GlyphPositioningTable.class); // CSOK: ConstantNameCheck
-
- /** single positioning subtable type */
- public static final int GPOS_LOOKUP_TYPE_SINGLE = 1;
- /** multiple positioning subtable type */
- public static final int GPOS_LOOKUP_TYPE_PAIR = 2;
- /** cursive positioning subtable type */
- public static final int GPOS_LOOKUP_TYPE_CURSIVE = 3;
- /** mark to base positioning subtable type */
- public static final int GPOS_LOOKUP_TYPE_MARK_TO_BASE = 4;
- /** mark to ligature positioning subtable type */
- public static final int GPOS_LOOKUP_TYPE_MARK_TO_LIGATURE = 5;
- /** mark to mark positioning subtable type */
- public static final int GPOS_LOOKUP_TYPE_MARK_TO_MARK = 6;
- /** contextual positioning subtable type */
- public static final int GPOS_LOOKUP_TYPE_CONTEXTUAL = 7;
- /** chained contextual positioning subtable type */
- public static final int GPOS_LOOKUP_TYPE_CHAINED_CONTEXTUAL = 8;
- /** extension positioning subtable type */
- public static final int GPOS_LOOKUP_TYPE_EXTENSION_POSITIONING = 9;
-
- /**
- * Instantiate a <code>GlyphPositioningTable</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 GlyphPositioningTable ( 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 ( Iterator it = subtables.iterator(); it.hasNext();) {
- Object o = it.next();
- if ( o instanceof GlyphPositioningSubtable ) {
- addSubtable ( (GlyphSubtable) o );
- } else {
- throw new AdvancedTypographicTableFormatException ( "subtable must be a glyph positioning subtable" );
- }
- }
- freezeSubtables();
- }
- }
-
- /**
- * 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 = GPOS_LOOKUP_TYPE_SINGLE;
- } else if ( "pair".equals ( s ) ) {
- t = GPOS_LOOKUP_TYPE_PAIR;
- } else if ( "cursive".equals ( s ) ) {
- t = GPOS_LOOKUP_TYPE_CURSIVE;
- } else if ( "marktobase".equals ( s ) ) {
- t = GPOS_LOOKUP_TYPE_MARK_TO_BASE;
- } else if ( "marktoligature".equals ( s ) ) {
- t = GPOS_LOOKUP_TYPE_MARK_TO_LIGATURE;
- } else if ( "marktomark".equals ( s ) ) {
- t = GPOS_LOOKUP_TYPE_MARK_TO_MARK;
- } else if ( "contextual".equals ( s ) ) {
- t = GPOS_LOOKUP_TYPE_CONTEXTUAL;
- } else if ( "chainedcontextual".equals ( s ) ) {
- t = GPOS_LOOKUP_TYPE_CHAINED_CONTEXTUAL;
- } else if ( "extensionpositioning".equals ( s ) ) {
- t = GPOS_LOOKUP_TYPE_EXTENSION_POSITIONING;
- } 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;
- switch ( type ) {
- case GPOS_LOOKUP_TYPE_SINGLE:
- tn = "single";
- break;
- case GPOS_LOOKUP_TYPE_PAIR:
- tn = "pair";
- break;
- case GPOS_LOOKUP_TYPE_CURSIVE:
- tn = "cursive";
- break;
- case GPOS_LOOKUP_TYPE_MARK_TO_BASE:
- tn = "marktobase";
- break;
- case GPOS_LOOKUP_TYPE_MARK_TO_LIGATURE:
- tn = "marktoligature";
- break;
- case GPOS_LOOKUP_TYPE_MARK_TO_MARK:
- tn = "marktomark";
- break;
- case GPOS_LOOKUP_TYPE_CONTEXTUAL:
- tn = "contextual";
- break;
- case GPOS_LOOKUP_TYPE_CHAINED_CONTEXTUAL:
- tn = "chainedcontextual";
- break;
- case GPOS_LOOKUP_TYPE_EXTENSION_POSITIONING:
- tn = "extensionpositioning";
- break;
- default:
- tn = "unknown";
- break;
- }
- return tn;
- }
-
- /**
- * Create a positioning 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 GPOS_LOOKUP_TYPE_SINGLE:
- st = SingleSubtable.create ( id, sequence, flags, format, coverage, entries );
- break;
- case GPOS_LOOKUP_TYPE_PAIR:
- st = PairSubtable.create ( id, sequence, flags, format, coverage, entries );
- break;
- case GPOS_LOOKUP_TYPE_CURSIVE:
- st = CursiveSubtable.create ( id, sequence, flags, format, coverage, entries );
- break;
- case GPOS_LOOKUP_TYPE_MARK_TO_BASE:
- st = MarkToBaseSubtable.create ( id, sequence, flags, format, coverage, entries );
- break;
- case GPOS_LOOKUP_TYPE_MARK_TO_LIGATURE:
- st = MarkToLigatureSubtable.create ( id, sequence, flags, format, coverage, entries );
- break;
- case GPOS_LOOKUP_TYPE_MARK_TO_MARK:
- st = MarkToMarkSubtable.create ( id, sequence, flags, format, coverage, entries );
- break;
- case GPOS_LOOKUP_TYPE_CONTEXTUAL:
- st = ContextualSubtable.create ( id, sequence, flags, format, coverage, entries );
- break;
- case GPOS_LOOKUP_TYPE_CHAINED_CONTEXTUAL:
- st = ChainedContextualSubtable.create ( id, sequence, flags, format, coverage, entries );
- break;
- default:
- break;
- }
- return st;
- }
-
- /**
- * Create a positioning 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 );
- }
-
- /**
- * Perform positioning processing using all matching lookups.
- * @param gs an input glyph sequence
- * @param script a script identifier
- * @param language a language identifier
- * @param fontSize size in device units
- * @param widths array of default advancements for each glyph
- * @param adjustments accumulated adjustments array (sequence) of 4-tuples of placement [PX,PY] and advance [AX,AY] adjustments, in that order,
- * with one 4-tuple for each element of glyph sequence
- * @return true if some adjustment is not zero; otherwise, false
- */
- public boolean position ( GlyphSequence gs, String script, String language, int fontSize, int[] widths, int[][] adjustments ) {
- Map/*<LookupSpec,List<LookupTable>>*/ lookups = matchLookups ( script, language, "*" );
- if ( ( lookups != null ) && ( lookups.size() > 0 ) ) {
- ScriptProcessor sp = ScriptProcessor.getInstance ( script );
- return sp.position ( this, gs, script, language, fontSize, lookups, widths, adjustments );
- } else {
- return false;
- }
- }
-
- private abstract static class SingleSubtable extends GlyphPositioningSubtable {
- SingleSubtable ( String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries ) {
- super ( id, sequence, flags, format, coverage );
- }
- /** {@inheritDoc} */
- public int getType() {
- return GPOS_LOOKUP_TYPE_SINGLE;
- }
- /** {@inheritDoc} */
- public boolean isCompatible ( GlyphSubtable subtable ) {
- return subtable instanceof SingleSubtable;
- }
- /** {@inheritDoc} */
- public boolean position ( GlyphPositioningState ps ) {
- int gi = ps.getGlyph();
- int ci;
- if ( ( ci = getCoverageIndex ( gi ) ) < 0 ) {
- return false;
- } else {
- Value v = getValue ( ci, gi );
- if ( v != null ) {
- if ( ps.adjust(v) ) {
- ps.setAdjusted ( true );
- }
- ps.consume(1);
- }
- return true;
- }
- }
- /**
- * Obtain positioning value for coverage index.
- * @param ci coverage index
- * @param gi input glyph index
- * @return positioning value or null if none applies
- */
- public abstract Value getValue ( int ci, int gi );
- static GlyphPositioningSubtable 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 Value value;
- 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() {
- if ( value != null ) {
- List entries = new ArrayList ( 1 );
- entries.add ( value );
- return entries;
- } else {
- return null;
- }
- }
- /** {@inheritDoc} */
- public Value getValue ( int ci, int gi ) {
- if ( ( value != null ) && ( ci <= ciMax ) ) {
- return value;
- } else {
- return null;
- }
- }
- 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 {
- Value v;
- Object o = entries.get(0);
- if ( o instanceof Value ) {
- v = (Value) o;
- } else {
- throw new AdvancedTypographicTableFormatException ( "illegal entries entry, must be Value, but is: " + ( ( o != null ) ? o.getClass() : null ) );
- }
- assert this.value == null;
- this.value = v;
- this.ciMax = getCoverageSize() - 1;
- }
- }
- }
-
- private static class SingleSubtableFormat2 extends SingleSubtable {
- private Value[] values;
- 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() {
- if ( values != null ) {
- List entries = new ArrayList ( values.length );
- for ( int i = 0, n = values.length; i < n; i++ ) {
- entries.add ( values[i] );
- }
- return entries;
- } else {
- return null;
- }
- }
- /** {@inheritDoc} */
- public Value getValue ( int ci, int gi ) {
- if ( ( values != null ) && ( ci < values.length ) ) {
- return values [ ci ];
- } else {
- return null;
- }
- }
- 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 Value[] ) ) {
- throw new AdvancedTypographicTableFormatException ( "illegal entries, single entry must be a Value[], but is: " + ( ( o != null ) ? o.getClass() : null ) );
- } else {
- Value[] va = (Value[]) o;
- if ( va.length != getCoverageSize() ) {
- throw new AdvancedTypographicTableFormatException ( "illegal values array, " + entries.size() + " values present, but requires " + getCoverageSize() + " values" );
- } else {
- assert this.values == null;
- this.values = va;
- }
- }
- }
- }
- }
-
- private abstract static class PairSubtable extends GlyphPositioningSubtable {
- PairSubtable ( String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries ) {
- super ( id, sequence, flags, format, coverage );
- }
- /** {@inheritDoc} */
- public int getType() {
- return GPOS_LOOKUP_TYPE_PAIR;
- }
- /** {@inheritDoc} */
- public boolean isCompatible ( GlyphSubtable subtable ) {
- return subtable instanceof PairSubtable;
- }
- /** {@inheritDoc} */
- public boolean position ( GlyphPositioningState ps ) {
- boolean applied = false;
- int gi = ps.getGlyph(0);
- int ci;
- if ( ( ci = getCoverageIndex ( gi ) ) >= 0 ) {
- int[] counts = ps.getGlyphsAvailable ( 0 );
- int nga = counts[0];
- if ( nga > 1 ) {
- int[] iga = ps.getGlyphs ( 0, 2, null, counts );
- if ( ( iga != null ) && ( iga.length == 2 ) ) {
- PairValues pv = getPairValues ( ci, iga[0], iga[1] );
- if ( pv != null ) {
- Value v1 = pv.getValue1();
- if ( v1 != null ) {
- if ( ps.adjust(v1, 0) ) {
- ps.setAdjusted ( true );
- }
- }
- Value v2 = pv.getValue2();
- if ( v2 != null ) {
- if ( ps.adjust(v2, 1) ) {
- ps.setAdjusted ( true );
- }
- }
- ps.consume ( counts[0] + counts[1] );
- applied = true;
- }
- }
- }
- }
- return applied;
- }
- /**
- * Obtain associated pair values.
- * @param ci coverage index
- * @param gi1 first input glyph index
- * @param gi2 second input glyph index
- * @return pair values or null if none applies
- */
- public abstract PairValues getPairValues ( int ci, int gi1, int gi2 );
- static GlyphPositioningSubtable create ( String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries ) {
- if ( format == 1 ) {
- return new PairSubtableFormat1 ( id, sequence, flags, format, coverage, entries );
- } else if ( format == 2 ) {
- return new PairSubtableFormat2 ( id, sequence, flags, format, coverage, entries );
- } else {
- throw new UnsupportedOperationException();
- }
- }
- }
-
- private static class PairSubtableFormat1 extends PairSubtable {
- private PairValues[][] pvm; // pair values matrix
- PairSubtableFormat1 ( 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 ( pvm != null ) {
- List entries = new ArrayList ( 1 );
- entries.add ( pvm );
- return entries;
- } else {
- return null;
- }
- }
- /** {@inheritDoc} */
- public PairValues getPairValues ( int ci, int gi1, int gi2 ) {
- if ( ( pvm != null ) && ( ci < pvm.length ) ) {
- PairValues[] pvt = pvm [ ci ];
- for ( int i = 0, n = pvt.length; i < n; i++ ) {
- PairValues pv = pvt [ i ];
- if ( pv != null ) {
- int g = pv.getGlyph();
- if ( g < gi2 ) {
- continue;
- } else if ( g == gi2 ) {
- return pv;
- } else {
- break;
- }
- }
- }
- }
- return null;
- }
- 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 PairValues[][] ) ) {
- throw new AdvancedTypographicTableFormatException ( "illegal entries, first (and only) entry must be a PairValues[][], but is: " + ( ( o != null ) ? o.getClass() : null ) );
- } else {
- pvm = (PairValues[][]) o;
- }
- }
- }
- }
-
- private static class PairSubtableFormat2 extends PairSubtable {
- private GlyphClassTable cdt1; // class def table 1
- private GlyphClassTable cdt2; // class def table 2
- private int nc1; // class 1 count
- private int nc2; // class 2 count
- private PairValues[][] pvm; // pair values matrix
- PairSubtableFormat2 ( 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 ( pvm != null ) {
- List entries = new ArrayList ( 5 );
- entries.add ( cdt1 );
- entries.add ( cdt2 );
- entries.add ( Integer.valueOf ( nc1 ) );
- entries.add ( Integer.valueOf ( nc2 ) );
- entries.add ( pvm );
- return entries;
- } else {
- return null;
- }
- }
- /** {@inheritDoc} */
- public PairValues getPairValues ( int ci, int gi1, int gi2 ) {
- if ( pvm != null ) {
- int c1 = cdt1.getClassIndex ( gi1, 0 );
- if ( ( c1 >= 0 ) && ( c1 < nc1 ) && ( c1 < pvm.length ) ) {
- PairValues[] pvt = pvm [ c1 ];
- if ( pvt != null ) {
- int c2 = cdt2.getClassIndex ( gi2, 0 );
- if ( ( c2 >= 0 ) && ( c2 < nc2 ) && ( c2 < pvt.length ) ) {
- return pvt [ c2 ];
- }
- }
- }
- }
- return null;
- }
- 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 {
- cdt1 = (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 != null ) ? o.getClass() : null ) );
- } else {
- cdt2 = (GlyphClassTable) o;
- }
- if ( ( ( o = entries.get(2) ) == null ) || ! ( o instanceof Integer ) ) {
- throw new AdvancedTypographicTableFormatException ( "illegal entries, third entry must be an Integer, but is: " + ( ( o != null ) ? o.getClass() : null ) );
- } else {
- nc1 = ((Integer)(o)).intValue();
- }
- 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 {
- nc2 = ((Integer)(o)).intValue();
- }
- if ( ( ( o = entries.get(4) ) == null ) || ! ( o instanceof PairValues[][] ) ) {
- throw new AdvancedTypographicTableFormatException ( "illegal entries, fifth entry must be a PairValues[][], but is: " + ( ( o != null ) ? o.getClass() : null ) );
- } else {
- pvm = (PairValues[][]) o;
- }
- }
- }
- }
-
- private abstract static class CursiveSubtable extends GlyphPositioningSubtable {
- CursiveSubtable ( String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries ) {
- super ( id, sequence, flags, format, coverage );
- }
- /** {@inheritDoc} */
- public int getType() {
- return GPOS_LOOKUP_TYPE_CURSIVE;
- }
- /** {@inheritDoc} */
- public boolean isCompatible ( GlyphSubtable subtable ) {
- return subtable instanceof CursiveSubtable;
- }
- /** {@inheritDoc} */
- public boolean position ( GlyphPositioningState ps ) {
- boolean applied = false;
- int gi = ps.getGlyph(0);
- int ci;
- if ( ( ci = getCoverageIndex ( gi ) ) >= 0 ) {
- int[] counts = ps.getGlyphsAvailable ( 0 );
- int nga = counts[0];
- if ( nga > 1 ) {
- int[] iga = ps.getGlyphs ( 0, 2, null, counts );
- if ( ( iga != null ) && ( iga.length == 2 ) ) {
- // int gi1 = gi;
- int ci1 = ci;
- int gi2 = iga [ 1 ];
- int ci2 = getCoverageIndex ( gi2 );
- Anchor[] aa = getExitEntryAnchors ( ci1, ci2 );
- if ( aa != null ) {
- Anchor exa = aa [ 0 ];
- Anchor ena = aa [ 1 ];
- // int exw = ps.getWidth ( gi1 );
- int enw = ps.getWidth ( gi2 );
- if ( ( exa != null ) && ( ena != null ) ) {
- Value v = ena.getAlignmentAdjustment ( exa );
- v.adjust ( - enw, 0, 0, 0 );
- if ( ps.adjust ( v ) ) {
- ps.setAdjusted ( true );
- }
- }
- // consume only first glyph of exit/entry glyph pair
- ps.consume ( 1 );
- applied = true;
- }
- }
- }
- }
- return applied;
- }
- /**
- * Obtain exit anchor for first glyph with coverage index <code>ci1</code> and entry anchor for second
- * glyph with coverage index <code>ci2</code>.
- * @param ci1 coverage index of first glyph (may be negative)
- * @param ci2 coverage index of second glyph (may be negative)
- * @return array of two anchors or null if either coverage index is negative or corresponding anchor is
- * missing, where the first entry is the exit anchor of the first glyph and the second entry is the
- * entry anchor of the second glyph
- */
- public abstract Anchor[] getExitEntryAnchors ( int ci1, int ci2 );
- static GlyphPositioningSubtable create ( String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries ) {
- if ( format == 1 ) {
- return new CursiveSubtableFormat1 ( id, sequence, flags, format, coverage, entries );
- } else {
- throw new UnsupportedOperationException();
- }
- }
- }
-
- private static class CursiveSubtableFormat1 extends CursiveSubtable {
- private Anchor[] aa; // anchor array, where even entries are entry anchors, and odd entries are exit anchors
- CursiveSubtableFormat1 ( 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 ( aa != null ) {
- List entries = new ArrayList ( 1 );
- entries.add ( aa );
- return entries;
- } else {
- return null;
- }
- }
- /** {@inheritDoc} */
- public Anchor[] getExitEntryAnchors ( int ci1, int ci2 ) {
- if ( ( ci1 >= 0 ) && ( ci2 >= 0 ) ) {
- int ai1 = ( ci1 * 2 ) + 1; // ci1 denotes glyph with exit anchor
- int ai2 = ( ci2 * 2 ) + 0; // ci2 denotes glyph with entry anchor
- if ( ( aa != null ) && ( ai1 < aa.length ) && ( ai2 < aa.length ) ) {
- Anchor exa = aa [ ai1 ];
- Anchor ena = aa [ ai2 ];
- if ( ( exa != null ) && ( ena != null ) ) {
- return new Anchor[] { exa, ena };
- }
- }
- }
- return null;
- }
- 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 Anchor[] ) ) {
- throw new AdvancedTypographicTableFormatException ( "illegal entries, first (and only) entry must be a Anchor[], but is: " + ( ( o != null ) ? o.getClass() : null ) );
- } else if ( ( ( (Anchor[]) o ) . length % 2 ) != 0 ) {
- throw new AdvancedTypographicTableFormatException ( "illegal entries, Anchor[] array must have an even number of entries, but has: " + ( (Anchor[]) o ) . length );
- } else {
- aa = (Anchor[]) o;
- }
- }
- }
- }
-
- private abstract static class MarkToBaseSubtable extends GlyphPositioningSubtable {
- MarkToBaseSubtable ( String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries ) {
- super ( id, sequence, flags, format, coverage );
- }
- /** {@inheritDoc} */
- public int getType() {
- return GPOS_LOOKUP_TYPE_MARK_TO_BASE;
- }
- /** {@inheritDoc} */
- public boolean isCompatible ( GlyphSubtable subtable ) {
- return subtable instanceof MarkToBaseSubtable;
- }
- /** {@inheritDoc} */
- public boolean position ( GlyphPositioningState ps ) {
- boolean applied = false;
- int giMark = ps.getGlyph();
- int ciMark;
- if ( ( ciMark = getCoverageIndex ( giMark ) ) >= 0 ) {
- MarkAnchor ma = getMarkAnchor ( ciMark, giMark );
- if ( ma != null ) {
- for ( int i = 0, n = ps.getPosition(); i < n; i++ ) {
- int gi = ps.getGlyph ( - ( i + 1 ) );
- if ( ps.isMark ( gi ) ) {
- continue;
- } else {
- Anchor a = getBaseAnchor ( gi, ma.getMarkClass() );
- if ( a != null ) {
- Value v = a.getAlignmentAdjustment ( ma );
- // start experimental fix for END OF AYAH in Lateef/Scheherazade
- int[] aa = ps.getAdjustment();
- if ( aa[2] == 0 ) {
- v.adjust ( 0, 0, - ps.getWidth ( giMark ), 0 );
- }
- // end experimental fix for END OF AYAH in Lateef/Scheherazade
- if ( ps.adjust ( v ) ) {
- ps.setAdjusted ( true );
- }
- }
- ps.consume(1);
- applied = true;
- break;
- }
- }
- }
- }
- return applied;
- }
- /**
- * Obtain mark anchor associated with mark coverage index.
- * @param ciMark coverage index
- * @param giMark input glyph index of mark glyph
- * @return mark anchor or null if none applies
- */
- public abstract MarkAnchor getMarkAnchor ( int ciMark, int giMark );
- /**
- * Obtain anchor associated with base glyph index and mark class.
- * @param giBase input glyph index of base glyph
- * @param markClass class number of mark glyph
- * @return anchor or null if none applies
- */
- public abstract Anchor getBaseAnchor ( int giBase, int markClass );
- static GlyphPositioningSubtable create ( String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries ) {
- if ( format == 1 ) {
- return new MarkToBaseSubtableFormat1 ( id, sequence, flags, format, coverage, entries );
- } else {
- throw new UnsupportedOperationException();
- }
- }
- }
-
- private static class MarkToBaseSubtableFormat1 extends MarkToBaseSubtable {
- private GlyphCoverageTable bct; // base coverage table
- private int nmc; // mark class count
- private MarkAnchor[] maa; // mark anchor array, ordered by mark coverage index
- private Anchor[][] bam; // base anchor matrix, ordered by base coverage index, then by mark class
- MarkToBaseSubtableFormat1 ( 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 ( ( bct != null ) && ( maa != null ) && ( nmc > 0 ) && ( bam != null ) ) {
- List entries = new ArrayList ( 4 );
- entries.add ( bct );
- entries.add ( Integer.valueOf ( nmc ) );
- entries.add ( maa );
- entries.add ( bam );
- return entries;
- } else {
- return null;
- }
- }
- /** {@inheritDoc} */
- public MarkAnchor getMarkAnchor ( int ciMark, int giMark ) {
- if ( ( maa != null ) && ( ciMark < maa.length ) ) {
- return maa [ ciMark ];
- } else {
- return null;
- }
- }
- /** {@inheritDoc} */
- public Anchor getBaseAnchor ( int giBase, int markClass ) {
- int ciBase;
- if ( ( bct != null ) && ( ( ciBase = bct.getCoverageIndex ( giBase ) ) >= 0 ) ) {
- if ( ( bam != null ) && ( ciBase < bam.length ) ) {
- Anchor[] ba = bam [ ciBase ];
- if ( ( ba != null ) && ( markClass < ba.length ) ) {
- return ba [ markClass ];
- }
- }
- }
- return null;
- }
- private void populate ( List entries ) {
- if ( entries == null ) {
- throw new AdvancedTypographicTableFormatException ( "illegal entries, must be non-null" );
- } else if ( entries.size() != 4 ) {
- throw new AdvancedTypographicTableFormatException ( "illegal entries, " + entries.size() + " entries present, but requires 4 entries" );
- } else {
- Object o;
- if ( ( ( o = entries.get(0) ) == null ) || ! ( o instanceof GlyphCoverageTable ) ) {
- throw new AdvancedTypographicTableFormatException ( "illegal entries, first entry must be an GlyphCoverageTable, but is: " + ( ( o != null ) ? o.getClass() : null ) );
- } else {
- bct = (GlyphCoverageTable) 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 {
- nmc = ((Integer)(o)).intValue();
- }
- if ( ( ( o = entries.get(2) ) == null ) || ! ( o instanceof MarkAnchor[] ) ) {
- throw new AdvancedTypographicTableFormatException ( "illegal entries, third entry must be a MarkAnchor[], but is: " + ( ( o != null ) ? o.getClass() : null ) );
- } else {
- maa = (MarkAnchor[]) o;
- }
- if ( ( ( o = entries.get(3) ) == null ) || ! ( o instanceof Anchor[][] ) ) {
- throw new AdvancedTypographicTableFormatException ( "illegal entries, fourth entry must be a Anchor[][], but is: " + ( ( o != null ) ? o.getClass() : null ) );
- } else {
- bam = (Anchor[][]) o;
- }
- }
- }
- }
-
- private abstract static class MarkToLigatureSubtable extends GlyphPositioningSubtable {
- MarkToLigatureSubtable ( String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries ) {
- super ( id, sequence, flags, format, coverage );
- }
- /** {@inheritDoc} */
- public int getType() {
- return GPOS_LOOKUP_TYPE_MARK_TO_LIGATURE;
- }
- /** {@inheritDoc} */
- public boolean isCompatible ( GlyphSubtable subtable ) {
- return subtable instanceof MarkToLigatureSubtable;
- }
- /** {@inheritDoc} */
- public boolean position ( GlyphPositioningState ps ) {
- boolean applied = false;
- int giMark = ps.getGlyph();
- int ciMark;
- if ( ( ciMark = getCoverageIndex ( giMark ) ) >= 0 ) {
- MarkAnchor ma = getMarkAnchor ( ciMark, giMark );
- int mxc = getMaxComponentCount();
- if ( ma != null ) {
- for ( int i = 0, n = ps.getPosition(); i < n; i++ ) {
- int gi = ps.getGlyph ( - ( i + 1 ) );
- if ( ps.isMark ( gi ) ) {
- continue;
- } else {
- Anchor a = getLigatureAnchor ( gi, mxc, i, ma.getMarkClass() );
- if ( a != null ) {
- if ( ps.adjust ( a.getAlignmentAdjustment ( ma ) ) ) {
- ps.setAdjusted ( true );
- }
- }
- ps.consume(1);
- applied = true;
- break;
- }
- }
- }
- }
- return applied;
- }
- /**
- * Obtain mark anchor associated with mark coverage index.
- * @param ciMark coverage index
- * @param giMark input glyph index of mark glyph
- * @return mark anchor or null if none applies
- */
- public abstract MarkAnchor getMarkAnchor ( int ciMark, int giMark );
- /**
- * Obtain maximum component count.
- * @return maximum component count (>=0)
- */
- public abstract int getMaxComponentCount();
- /**
- * Obtain anchor associated with ligature glyph index and mark class.
- * @param giLig input glyph index of ligature glyph
- * @param maxComponents maximum component count
- * @param component component number (0...maxComponents-1)
- * @param markClass class number of mark glyph
- * @return anchor or null if none applies
- */
- public abstract Anchor getLigatureAnchor ( int giLig, int maxComponents, int component, int markClass );
- static GlyphPositioningSubtable create ( String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries ) {
- if ( format == 1 ) {
- return new MarkToLigatureSubtableFormat1 ( id, sequence, flags, format, coverage, entries );
- } else {
- throw new UnsupportedOperationException();
- }
- }
- }
-
- private static class MarkToLigatureSubtableFormat1 extends MarkToLigatureSubtable {
- private GlyphCoverageTable lct; // ligature coverage table
- private int nmc; // mark class count
- private int mxc; // maximum ligature component count
- private MarkAnchor[] maa; // mark anchor array, ordered by mark coverage index
- private Anchor[][][] lam; // ligature anchor matrix, ordered by ligature coverage index, then ligature component, then mark class
- MarkToLigatureSubtableFormat1 ( 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 ( lam != null ) {
- List entries = new ArrayList ( 5 );
- entries.add ( lct );
- entries.add ( Integer.valueOf ( nmc ) );
- entries.add ( Integer.valueOf ( mxc ) );
- entries.add ( maa );
- entries.add ( lam );
- return entries;
- } else {
- return null;
- }
- }
- /** {@inheritDoc} */
- public MarkAnchor getMarkAnchor ( int ciMark, int giMark ) {
- if ( ( maa != null ) && ( ciMark < maa.length ) ) {
- return maa [ ciMark ];
- } else {
- return null;
- }
- }
- /** {@inheritDoc} */
- public int getMaxComponentCount() {
- return mxc;
- }
- /** {@inheritDoc} */
- public Anchor getLigatureAnchor ( int giLig, int maxComponents, int component, int markClass ) {
- int ciLig;
- if ( ( lct != null ) && ( ( ciLig = lct.getCoverageIndex ( giLig ) ) >= 0 ) ) {
- if ( ( lam != null ) && ( ciLig < lam.length ) ) {
- Anchor[][] lcm = lam [ ciLig ];
- if ( component < maxComponents ) {
- Anchor[] la = lcm [ component ];
- if ( ( la != null ) && ( markClass < la.length ) ) {
- return la [ markClass ];
- }
- }
- }
- }
- return null;
- }
- 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 GlyphCoverageTable ) ) {
- throw new AdvancedTypographicTableFormatException ( "illegal entries, first entry must be an GlyphCoverageTable, but is: " + ( ( o != null ) ? o.getClass() : null ) );
- } else {
- lct = (GlyphCoverageTable) 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 {
- nmc = ((Integer)(o)).intValue();
- }
- if ( ( ( o = entries.get(2) ) == null ) || ! ( o instanceof Integer ) ) {
- throw new AdvancedTypographicTableFormatException ( "illegal entries, third entry must be an Integer, but is: " + ( ( o != null ) ? o.getClass() : null ) );
- } else {
- mxc = ((Integer)(o)).intValue();
- }
- if ( ( ( o = entries.get(3) ) == null ) || ! ( o instanceof MarkAnchor[] ) ) {
- throw new AdvancedTypographicTableFormatException ( "illegal entries, fourth entry must be a MarkAnchor[], but is: " + ( ( o != null ) ? o.getClass() : null ) );
- } else {
- maa = (MarkAnchor[]) o;
- }
- if ( ( ( o = entries.get(4) ) == null ) || ! ( o instanceof Anchor[][][] ) ) {
- throw new AdvancedTypographicTableFormatException ( "illegal entries, fifth entry must be a Anchor[][][], but is: " + ( ( o != null ) ? o.getClass() : null ) );
- } else {
- lam = (Anchor[][][]) o;
- }
- }
- }
- }
-
- private abstract static class MarkToMarkSubtable extends GlyphPositioningSubtable {
- MarkToMarkSubtable ( String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries ) {
- super ( id, sequence, flags, format, coverage );
- }
- /** {@inheritDoc} */
- public int getType() {
- return GPOS_LOOKUP_TYPE_MARK_TO_MARK;
- }
- /** {@inheritDoc} */
- public boolean isCompatible ( GlyphSubtable subtable ) {
- return subtable instanceof MarkToMarkSubtable;
- }
- /** {@inheritDoc} */
- public boolean position ( GlyphPositioningState ps ) {
- boolean applied = false;
- int giMark1 = ps.getGlyph();
- int ciMark1;
- if ( ( ciMark1 = getCoverageIndex ( giMark1 ) ) >= 0 ) {
- MarkAnchor ma = getMark1Anchor ( ciMark1, giMark1 );
- if ( ma != null ) {
- if ( ps.hasPrev() ) {
- Anchor a = getMark2Anchor ( ps.getGlyph(-1), ma.getMarkClass() );
- if ( a != null ) {
- if ( ps.adjust ( a.getAlignmentAdjustment ( ma ) ) ) {
- ps.setAdjusted ( true );
- }
- }
- ps.consume(1);
- applied = true;
- }
- }
- }
- return applied;
- }
- /**
- * Obtain mark 1 anchor associated with mark 1 coverage index.
- * @param ciMark1 mark 1 coverage index
- * @param giMark1 input glyph index of mark 1 glyph
- * @return mark 1 anchor or null if none applies
- */
- public abstract MarkAnchor getMark1Anchor ( int ciMark1, int giMark1 );
- /**
- * Obtain anchor associated with mark 2 glyph index and mark 1 class.
- * @param giMark2 input glyph index of mark 2 glyph
- * @param markClass class number of mark 1 glyph
- * @return anchor or null if none applies
- */
- public abstract Anchor getMark2Anchor ( int giBase, int markClass );
- static GlyphPositioningSubtable create ( String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries ) {
- if ( format == 1 ) {
- return new MarkToMarkSubtableFormat1 ( id, sequence, flags, format, coverage, entries );
- } else {
- throw new UnsupportedOperationException();
- }
- }
- }
-
- private static class MarkToMarkSubtableFormat1 extends MarkToMarkSubtable {
- private GlyphCoverageTable mct2; // mark 2 coverage table
- private int nmc; // mark class count
- private MarkAnchor[] maa; // mark1 anchor array, ordered by mark1 coverage index
- private Anchor[][] mam; // mark2 anchor matrix, ordered by mark2 coverage index, then by mark1 class
- MarkToMarkSubtableFormat1 ( 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 ( ( mct2 != null ) && ( maa != null ) && ( nmc > 0 ) && ( mam != null ) ) {
- List entries = new ArrayList ( 4 );
- entries.add ( mct2 );
- entries.add ( Integer.valueOf ( nmc ) );
- entries.add ( maa );
- entries.add ( mam );
- return entries;
- } else {
- return null;
- }
- }
- /** {@inheritDoc} */
- public MarkAnchor getMark1Anchor ( int ciMark1, int giMark1 ) {
- if ( ( maa != null ) && ( ciMark1 < maa.length ) ) {
- return maa [ ciMark1 ];
- } else {
- return null;
- }
- }
- /** {@inheritDoc} */
- public Anchor getMark2Anchor ( int giMark2, int markClass ) {
- int ciMark2;
- if ( ( mct2 != null ) && ( ( ciMark2 = mct2.getCoverageIndex ( giMark2 ) ) >= 0 ) ) {
- if ( ( mam != null ) && ( ciMark2 < mam.length ) ) {
- Anchor[] ma = mam [ ciMark2 ];
- if ( ( ma != null ) && ( markClass < ma.length ) ) {
- return ma [ markClass ];
- }
- }
- }
- return null;
- }
- private void populate ( List entries ) {
- if ( entries == null ) {
- throw new AdvancedTypographicTableFormatException ( "illegal entries, must be non-null" );
- } else if ( entries.size() != 4 ) {
- throw new AdvancedTypographicTableFormatException ( "illegal entries, " + entries.size() + " entries present, but requires 4 entries" );
- } else {
- Object o;
- if ( ( ( o = entries.get(0) ) == null ) || ! ( o instanceof GlyphCoverageTable ) ) {
- throw new AdvancedTypographicTableFormatException ( "illegal entries, first entry must be an GlyphCoverageTable, but is: " + ( ( o != null ) ? o.getClass() : null ) );
- } else {
- mct2 = (GlyphCoverageTable) 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 {
- nmc = ((Integer)(o)).intValue();
- }
- if ( ( ( o = entries.get(2) ) == null ) || ! ( o instanceof MarkAnchor[] ) ) {
- throw new AdvancedTypographicTableFormatException ( "illegal entries, third entry must be a MarkAnchor[], but is: " + ( ( o != null ) ? o.getClass() : null ) );
- } else {
- maa = (MarkAnchor[]) o;
- }
- if ( ( ( o = entries.get(3) ) == null ) || ! ( o instanceof Anchor[][] ) ) {
- throw new AdvancedTypographicTableFormatException ( "illegal entries, fourth entry must be a Anchor[][], but is: " + ( ( o != null ) ? o.getClass() : null ) );
- } else {
- mam = (Anchor[][]) o;
- }
- }
- }
- }
-
- private abstract static class ContextualSubtable extends GlyphPositioningSubtable {
- ContextualSubtable ( String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries ) {
- super ( id, sequence, flags, format, coverage );
- }
- /** {@inheritDoc} */
- public int getType() {
- return GPOS_LOOKUP_TYPE_CONTEXTUAL;
- }
- /** {@inheritDoc} */
- public boolean isCompatible ( GlyphSubtable subtable ) {
- return subtable instanceof ContextualSubtable;
- }
- /** {@inheritDoc} */
- public boolean position ( GlyphPositioningState ps ) {
- boolean applied = false;
- int gi = ps.getGlyph();
- int ci;
- if ( ( ci = getCoverageIndex ( gi ) ) >= 0 ) {
- int[] rv = new int[1];
- RuleLookup[] la = getLookups ( ci, gi, ps, rv );
- if ( la != null ) {
- ps.apply ( la, rv[0] );
- applied = true;
- }
- }
- return applied;
- }
- /**
- * 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 ps glyph positioning 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, GlyphPositioningState ps, int[] rv );
- static GlyphPositioningSubtable 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, GlyphPositioningState ps, int[] rv ) {
- assert ps != 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 ( int i = 0, n = ra.length; i < n; i++ ) {
- Rule r = ra [ i ];
- if ( ( r != null ) && ( r instanceof ChainedGlyphSequenceRule ) ) {
- ChainedGlyphSequenceRule cr = (ChainedGlyphSequenceRule) r;
- int[] iga = cr.getGlyphs ( gi );
- if ( matches ( ps, iga, 0, rv ) ) {
- return r.getLookups();
- }
- }
- }
- }
- }
- return null;
- }
- static boolean matches ( GlyphPositioningState ps, 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 = ps.getIgnoreDefault();
- int[] counts = ps.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 = ps.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 ( Integer.valueOf ( 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, GlyphPositioningState ps, int[] rv ) {
- assert ps != 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 ( int i = 0, n = ra.length; i < n; i++ ) {
- Rule r = ra [ i ];
- if ( ( r != null ) && ( r instanceof ChainedClassSequenceRule ) ) {
- ChainedClassSequenceRule cr = (ChainedClassSequenceRule) r;
- int[] ca = cr.getClasses ( cdt.getClassIndex ( gi, ps.getClassMatchSet ( gi ) ) );
- if ( matches ( ps, cdt, ca, 0, rv ) ) {
- return r.getLookups();
- }
- }
- }
- }
- }
- return null;
- }
- static boolean matches ( GlyphPositioningState ps, 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 = ps.getIgnoreDefault();
- int[] counts = ps.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 = ps.getGlyphs ( offset, ngm, reverse, ignores, null, counts );
- for ( int k = 0; k < ngm; k++ ) {
- int gi = ga [ k ];
- int ms = ps.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)).intValue();
- }
- 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, GlyphPositioningState ps, int[] rv ) {
- assert ps != 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 ( int i = 0, n = ra.length; i < n; i++ ) {
- Rule r = ra [ i ];
- if ( ( r != null ) && ( r instanceof ChainedCoverageSequenceRule ) ) {
- ChainedCoverageSequenceRule cr = (ChainedCoverageSequenceRule) r;
- GlyphCoverageTable[] gca = cr.getCoverages();
- if ( matches ( ps, gca, 0, rv ) ) {
- return r.getLookups();
- }
- }
- }
- }
- }
- return null;
- }
- static boolean matches ( GlyphPositioningState ps, 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 = ps.getIgnoreDefault();
- int[] counts = ps.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 = ps.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 GlyphPositioningSubtable {
- ChainedContextualSubtable ( String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries ) {
- super ( id, sequence, flags, format, coverage );
- }
- /** {@inheritDoc} */
- public int getType() {
- return GPOS_LOOKUP_TYPE_CHAINED_CONTEXTUAL;
- }
- /** {@inheritDoc} */
- public boolean isCompatible ( GlyphSubtable subtable ) {
- return subtable instanceof ChainedContextualSubtable;
- }
- /** {@inheritDoc} */
- public boolean position ( GlyphPositioningState ps ) {
- boolean applied = false;
- int gi = ps.getGlyph();
- int ci;
- if ( ( ci = getCoverageIndex ( gi ) ) >= 0 ) {
- int[] rv = new int[1];
- RuleLookup[] la = getLookups ( ci, gi, ps, rv );
- if ( la != null ) {
- ps.apply ( la, rv[0] );
- applied = true;
- }
- }
- return applied;
- }
- /**
- * 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 ps glyph positioning 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, GlyphPositioningState ps, int[] rv );
- static GlyphPositioningSubtable 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, GlyphPositioningState ps, int[] rv ) {
- assert ps != 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 ( int i = 0, n = ra.length; i < n; i++ ) {
- Rule r = ra [ i ];
- if ( ( r != null ) && ( r instanceof ChainedGlyphSequenceRule ) ) {
- ChainedGlyphSequenceRule cr = (ChainedGlyphSequenceRule) r;
- int[] iga = cr.getGlyphs ( gi );
- if ( matches ( ps, iga, 0, rv ) ) {
- int[] bga = cr.getBacktrackGlyphs();
- if ( matches ( ps, bga, -1, null ) ) {
- int[] lga = cr.getLookaheadGlyphs();
- if ( matches ( ps, lga, rv[0], null ) ) {
- return r.getLookups();
- }
- }
- }
- }
- }
- }
- }
- return null;
- }
- private boolean matches ( GlyphPositioningState ps, int[] glyphs, int offset, int[] rv ) {
- return ContextualSubtableFormat1.matches ( ps, 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 ( Integer.valueOf ( 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, GlyphPositioningState ps, int[] rv ) {
- assert ps != 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 ( int i = 0, n = ra.length; i < n; i++ ) {
- Rule r = ra [ i ];
- if ( ( r != null ) && ( r instanceof ChainedClassSequenceRule ) ) {
- ChainedClassSequenceRule cr = (ChainedClassSequenceRule) r;
- int[] ica = cr.getClasses ( icdt.getClassIndex ( gi, ps.getClassMatchSet ( gi ) ) );
- if ( matches ( ps, icdt, ica, 0, rv ) ) {
- int[] bca = cr.getBacktrackClasses();
- if ( matches ( ps, bcdt, bca, -1, null ) ) {
- int[] lca = cr.getLookaheadClasses();
- if ( matches ( ps, lcdt, lca, rv[0], null ) ) {
- return r.getLookups();
- }
- }
- }
- }
- }
- }
- }
- return null;
- }
- private boolean matches ( GlyphPositioningState ps, GlyphClassTable cdt, int[] classes, int offset, int[] rv ) {
- return ContextualSubtableFormat2.matches ( ps, cdt, classes, offset, rv );
- }
- 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)).intValue();
- }
- 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, GlyphPositioningState ps, int[] rv ) {
- assert ps != 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 ( int i = 0, n = ra.length; i < n; i++ ) {
- Rule r = ra [ i ];
- if ( ( r != null ) && ( r instanceof ChainedCoverageSequenceRule ) ) {
- ChainedCoverageSequenceRule cr = (ChainedCoverageSequenceRule) r;
- GlyphCoverageTable[] igca = cr.getCoverages();
- if ( matches ( ps, igca, 0, rv ) ) {
- GlyphCoverageTable[] bgca = cr.getBacktrackCoverages();
- if ( matches ( ps, bgca, -1, null ) ) {
- GlyphCoverageTable[] lgca = cr.getLookaheadCoverages();
- if ( matches ( ps, lgca, rv[0], null ) ) {
- return r.getLookups();
- }
- }
- }
- }
- }
- }
- }
- return null;
- }
- private boolean matches ( GlyphPositioningState ps, GlyphCoverageTable[] gca, int offset, int[] rv ) {
- return ContextualSubtableFormat3.matches ( ps, 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;
- }
- }
- }
- }
-
- /**
- * The <code>DeviceTable</code> class implements a positioning device table record, comprising
- * adjustments to be made to scaled design units according to the scaled size.
- */
- public static class DeviceTable {
-
- private final int startSize;
- private final int endSize;
- private final int[] deltas;
-
- /**
- * Instantiate a DeviceTable.
- * @param startSize the
- * @param endSize the ending (scaled) size
- * @param deltas adjustments for each scaled size
- */
- public DeviceTable ( int startSize, int endSize, int[] deltas ) {
- assert startSize >= 0;
- assert startSize <= endSize;
- assert deltas != null;
- assert deltas.length == ( endSize - startSize ) + 1;
- this.startSize = startSize;
- this.endSize = endSize;
- this.deltas = deltas;
- }
-
- /** @return the start size */
- public int getStartSize() {
- return startSize;
- }
-
- /** @return the end size */
- public int getEndSize() {
- return endSize;
- }
-
- /** @return the deltas */
- public int[] getDeltas() {
- return deltas;
- }
-
- /**
- * Find device adjustment.
- * @param fontSize the font size to search for
- * @return an adjustment if font size matches an entry
- */
- public int findAdjustment ( int fontSize ) {
- // [TODO] at present, assumes that 1 device unit equals one point
- int fs = fontSize / 1000;
- if ( fs < startSize ) {
- return 0;
- } else if ( fs <= endSize ) {
- return deltas [ fs - startSize ] * 1000;
- } else {
- return 0;
- }
- }
-
- /** {@inheritDoc} */
- public String toString() {
- return "{ start = " + startSize + ", end = " + endSize + ", deltas = " + Arrays.toString ( deltas ) + "}";
- }
-
- }
-
- /**
- * The <code>Value</code> class implements a positioning value record, comprising placement
- * and advancement information in X and Y axes, and optionally including device data used to
- * perform device (grid-fitted) specific fine grain adjustments.
- */
- public static class Value {
-
- /** X_PLACEMENT value format flag */
- public static final int X_PLACEMENT = 0x0001;
- /** Y_PLACEMENT value format flag */
- public static final int Y_PLACEMENT = 0x0002;
- /** X_ADVANCE value format flag */
- public static final int X_ADVANCE = 0x0004;
- /** Y_ADVANCE value format flag */
- public static final int Y_ADVANCE = 0x0008;
- /** X_PLACEMENT_DEVICE value format flag */
- public static final int X_PLACEMENT_DEVICE = 0x0010;
- /** Y_PLACEMENT_DEVICE value format flag */
- public static final int Y_PLACEMENT_DEVICE = 0x0020;
- /** X_ADVANCE_DEVICE value format flag */
- public static final int X_ADVANCE_DEVICE = 0x0040;
- /** Y_ADVANCE_DEVICE value format flag */
- public static final int Y_ADVANCE_DEVICE = 0x0080;
-
- /** X_PLACEMENT value index (within adjustments arrays) */
- public static final int IDX_X_PLACEMENT = 0;
- /** Y_PLACEMENT value index (within adjustments arrays) */
- public static final int IDX_Y_PLACEMENT = 1;
- /** X_ADVANCE value index (within adjustments arrays) */
- public static final int IDX_X_ADVANCE = 2;
- /** Y_ADVANCE value index (within adjustments arrays) */
- public static final int IDX_Y_ADVANCE = 3;
-
- private int xPlacement; // x placement
- private int yPlacement; // y placement
- private int xAdvance; // x advance
- private int yAdvance; // y advance
- private final DeviceTable xPlaDevice; // x placement device table
- private final DeviceTable yPlaDevice; // y placement device table
- private final DeviceTable xAdvDevice; // x advance device table
- private final DeviceTable yAdvDevice; // x advance device table
-
- /**
- * Instantiate a Value.
- * @param xPlacement the x placement or zero
- * @param yPlacement the y placement or zero
- * @param xAdvance the x advance or zero
- * @param yAdvance the y advance or zero
- * @param xPlaDevice the x placement device table or null
- * @param yPlaDevice the y placement device table or null
- * @param xAdvDevice the x advance device table or null
- * @param yAdvDevice the y advance device table or null
- */
- public Value ( int xPlacement, int yPlacement, int xAdvance, int yAdvance, DeviceTable xPlaDevice, DeviceTable yPlaDevice, DeviceTable xAdvDevice, DeviceTable yAdvDevice ) {
- this.xPlacement = xPlacement;
- this.yPlacement = yPlacement;
- this.xAdvance = xAdvance;
- this.yAdvance = yAdvance;
- this.xPlaDevice = xPlaDevice;
- this.yPlaDevice = yPlaDevice;
- this.xAdvDevice = xAdvDevice;
- this.yAdvDevice = yAdvDevice;
- }
-
- /** @return the x placement */
- public int getXPlacement() {
- return xPlacement;
- }
-
- /** @return the y placement */
- public int getYPlacement() {
- return yPlacement;
- }
-
- /** @return the x advance */
- public int getXAdvance() {
- return xAdvance;
- }
-
- /** @return the y advance */
- public int getYAdvance() {
- return yAdvance;
- }
-
- /** @return the x placement device table */
- public DeviceTable getXPlaDevice() {
- return xPlaDevice;
- }
-
- /** @return the y placement device table */
- public DeviceTable getYPlaDevice() {
- return yPlaDevice;
- }
-
- /** @return the x advance device table */
- public DeviceTable getXAdvDevice() {
- return xAdvDevice;
- }
-
- /** @return the y advance device table */
- public DeviceTable getYAdvDevice() {
- return yAdvDevice;
- }
-
- /**
- * Apply value to specific adjustments to without use of device table adjustments.
- * @param xPlacement the x placement or zero
- * @param yPlacement the y placement or zero
- * @param xAdvance the x advance or zero
- * @param yAdvance the y advance or zero
- */
- public void adjust ( int xPlacement, int yPlacement, int xAdvance, int yAdvance ) {
- this.xPlacement += xPlacement;
- this.yPlacement += yPlacement;
- this.xAdvance += xAdvance;
- this.yAdvance += yAdvance;
- }
-
- /**
- * Apply value to adjustments using font size for device table adjustments.
- * @param adjustments array of four integers containing X,Y placement and X,Y advance adjustments
- * @param fontSize font size for device table adjustments
- * @return true if some adjustment was made
- */
- public boolean adjust ( int[] adjustments, int fontSize ) {
- boolean adjust = false;
- int dv;
- if ( ( dv = xPlacement ) != 0 ) {
- adjustments [ IDX_X_PLACEMENT ] += dv;
- adjust = true;
- }
- if ( ( dv = yPlacement ) != 0 ) {
- adjustments [ IDX_Y_PLACEMENT ] += dv;
- adjust = true;
- }
- if ( ( dv = xAdvance ) != 0 ) {
- adjustments [ IDX_X_ADVANCE ] += dv;
- adjust = true;
- }
- if ( ( dv = yAdvance ) != 0 ) {
- adjustments [ IDX_Y_ADVANCE ] += dv;
- adjust = true;
- }
- if ( fontSize != 0 ) {
- DeviceTable dt;
- if ( ( dt = xPlaDevice ) != null ) {
- if ( ( dv = dt.findAdjustment ( fontSize ) ) != 0 ) {
- adjustments [ IDX_X_PLACEMENT ] += dv;
- adjust = true;
- }
- }
- if ( ( dt = yPlaDevice ) != null ) {
- if ( ( dv = dt.findAdjustment ( fontSize ) ) != 0 ) {
- adjustments [ IDX_Y_PLACEMENT ] += dv;
- adjust = true;
- }
- }
- if ( ( dt = xAdvDevice ) != null ) {
- if ( ( dv = dt.findAdjustment ( fontSize ) ) != 0 ) {
- adjustments [ IDX_X_ADVANCE ] += dv;
- adjust = true;
- }
- }
- if ( ( dt = yAdvDevice ) != null ) {
- if ( ( dv = dt.findAdjustment ( fontSize ) ) != 0 ) {
- adjustments [ IDX_Y_ADVANCE ] += dv;
- adjust = true;
- }
- }
- }
- return adjust;
- }
-
- /** {@inheritDoc} */
- public String toString() {
- StringBuffer sb = new StringBuffer();
- boolean first = true;
- sb.append ( "{ " );
- if ( xPlacement != 0 ) {
- if ( ! first ) {
- sb.append ( ", " );
- } else {
- first = false;
- }
- sb.append ( "xPlacement = " + xPlacement );
- }
- if ( yPlacement != 0 ) {
- if ( ! first ) {
- sb.append ( ", " );
- } else {
- first = false;
- }
- sb.append ( "yPlacement = " + yPlacement );
- }
- if ( xAdvance != 0 ) {
- if ( ! first ) {
- sb.append ( ", " );
- } else {
- first = false;
- }
- sb.append ( "xAdvance = " + xAdvance );
- }
- if ( yAdvance != 0 ) {
- if ( ! first ) {
- sb.append ( ", " );
- } else {
- first = false;
- }
- sb.append ( "yAdvance = " + yAdvance );
- }
- if ( xPlaDevice != null ) {
- if ( ! first ) {
- sb.append ( ", " );
- } else {
- first = false;
- }
- sb.append ( "xPlaDevice = " + xPlaDevice );
- }
- if ( yPlaDevice != null ) {
- if ( ! first ) {
- sb.append ( ", " );
- } else {
- first = false;
- }
- sb.append ( "xPlaDevice = " + yPlaDevice );
- }
- if ( xAdvDevice != null ) {
- if ( ! first ) {
- sb.append ( ", " );
- } else {
- first = false;
- }
- sb.append ( "xAdvDevice = " + xAdvDevice );
- }
- if ( yAdvDevice != null ) {
- if ( ! first ) {
- sb.append ( ", " );
- } else {
- first = false;
- }
- sb.append ( "xAdvDevice = " + yAdvDevice );
- }
- sb.append(" }");
- return sb.toString();
- }
-
- }
-
- /**
- * The <code>PairValues</code> class implements a pair value record, comprising a glyph id (or zero)
- * and two optional positioning values.
- */
- public static class PairValues {
-
- private final int glyph; // glyph id (or 0)
- private final Value value1; // value for first glyph in pair (or null)
- private final Value value2; // value for second glyph in pair (or null)
-
- /**
- * Instantiate a PairValues.
- * @param glyph the glyph id (or zero)
- * @param value1 the value of the first glyph in pair (or null)
- * @param value2 the value of the second glyph in pair (or null)
- */
- public PairValues ( int glyph, Value value1, Value value2 ) {
- assert glyph >= 0;
- this.glyph = glyph;
- this.value1 = value1;
- this.value2 = value2;
- }
-
- /** @return the glyph id */
- public int getGlyph() {
- return glyph;
- }
-
- /** @return the first value */
- public Value getValue1() {
- return value1;
- }
-
- /** @return the second value */
- public Value getValue2() {
- return value2;
- }
-
- /** {@inheritDoc} */
- public String toString() {
- StringBuffer sb = new StringBuffer();
- boolean first = true;
- sb.append ( "{ " );
- if ( glyph != 0 ) {
- if ( ! first ) {
- sb.append ( ", " );
- } else {
- first = false;
- }
- sb.append ( "glyph = " + glyph );
- }
- if ( value1 != null ) {
- if ( ! first ) {
- sb.append ( ", " );
- } else {
- first = false;
- }
- sb.append ( "value1 = " + value1 );
- }
- if ( value2 != null ) {
- if ( ! first ) {
- sb.append ( ", " );
- } else {
- first = false;
- }
- sb.append ( "value2 = " + value2 );
- }
- sb.append(" }");
- return sb.toString();
- }
-
- }
-
- /**
- * The <code>Anchor</code> class implements a anchor record, comprising an X,Y coordinate pair,
- * an optional anchor point index (or -1), and optional X or Y device tables (or null if absent).
- */
- public static class Anchor {
-
- private final int x; // xCoordinate (in design units)
- private final int y; // yCoordinate (in design units)
- private final int anchorPoint; // anchor point index (or -1)
- private final DeviceTable xDevice; // x device table
- private final DeviceTable yDevice; // y device table
-
- /**
- * Instantiate an Anchor (format 1).
- * @param x the x coordinate
- * @param y the y coordinate
- */
- public Anchor ( int x, int y ) {
- this ( x, y, -1, null, null );
- }
-
- /**
- * Instantiate an Anchor (format 2).
- * @param x the x coordinate
- * @param y the y coordinate
- * @param anchorPoint anchor index (or -1)
- */
- public Anchor ( int x, int y, int anchorPoint ) {
- this ( x, y, anchorPoint, null, null );
- }
-
- /**
- * Instantiate an Anchor (format 3).
- * @param x the x coordinate
- * @param y the y coordinate
- * @param xDevice the x device table (or null if not present)
- * @param yDevice the y device table (or null if not present)
- */
- public Anchor ( int x, int y, DeviceTable xDevice, DeviceTable yDevice ) {
- this ( x, y, -1, xDevice, yDevice );
- }
-
- /**
- * Instantiate an Anchor based on an existing anchor.
- * @param a the existing anchor
- */
- protected Anchor ( Anchor a ) {
- this ( a.x, a.y, a.anchorPoint, a.xDevice, a.yDevice );
- }
-
- private Anchor ( int x, int y, int anchorPoint, DeviceTable xDevice, DeviceTable yDevice ) {
- assert ( anchorPoint >= 0 ) || ( anchorPoint == -1 );
- this.x = x;
- this.y = y;
- this.anchorPoint = anchorPoint;
- this.xDevice = xDevice;
- this.yDevice = yDevice;
- }
-
- /** @return the x coordinate */
- public int getX() {
- return x;
- }
-
- /** @return the y coordinate */
- public int getY() {
- return y;
- }
-
- /** @return the anchor point index (or -1 if not specified) */
- public int getAnchorPoint() {
- return anchorPoint;
- }
-
- /** @return the x device table (or null if not specified) */
- public DeviceTable getXDevice() {
- return xDevice;
- }
-
- /** @return the y device table (or null if not specified) */
- public DeviceTable getYDevice() {
- return yDevice;
- }
-
- /**
- * Obtain adjustment value required to align the specified anchor
- * with this anchor.
- * @param a the anchor to align
- * @return the adjustment value needed to effect alignment
- */
- public Value getAlignmentAdjustment ( Anchor a ) {
- assert a != null;
- // TODO - handle anchor point
- // TODO - handle device tables
- return new Value ( x - a.x, y - a.y, 0, 0, null, null, null, null );
- }
-
- /** {@inheritDoc} */
- public String toString() {
- StringBuffer sb = new StringBuffer();
- sb.append ( "{ [" + x + "," + y + "]" );
- if ( anchorPoint != -1 ) {
- sb.append ( ", anchorPoint = " + anchorPoint );
- }
- if ( xDevice != null ) {
- sb.append ( ", xDevice = " + xDevice );
- }
- if ( yDevice != null ) {
- sb.append ( ", yDevice = " + yDevice );
- }
- sb.append(" }");
- return sb.toString();
- }
-
- }
-
- /**
- * The <code>MarkAnchor</code> class is a subclass of the <code>Anchor</code> class, adding a mark
- * class designation.
- */
- public static class MarkAnchor extends Anchor {
-
- private final int markClass; // mark class
-
- /**
- * Instantiate a MarkAnchor
- * @param markClass the mark class
- * @param a the underlying anchor (whose fields are copied)
- */
- public MarkAnchor ( int markClass, Anchor a ) {
- super ( a );
- this.markClass = markClass;
- }
-
- /** @return the mark class */
- public int getMarkClass() {
- return markClass;
- }
-
- /** {@inheritDoc} */
- public String toString() {
- return "{ markClass = " + markClass + ", anchor = " + super.toString() + " }";
- }
-
- }
-
- }
|