Skip to content

Commit 5c136f5

Browse files
committed
OTF: Support GposLookupType1 format
DEVSIX-4633
1 parent 4e37c92 commit 5c136f5

File tree

6 files changed

+253
-0
lines changed

6 files changed

+253
-0
lines changed

io/src/main/java/com/itextpdf/io/font/otf/GlyphPositioningTableReader.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ protected OpenTableLookup readLookupTable(int lookupType, int lookupFlag, int[]
7777
}
7878
}
7979
switch (lookupType) {
80+
case 1:
81+
return new GposLookupType1(this, lookupFlag, subTableLocations);
8082
case 2:
8183
return new GposLookupType2(this, lookupFlag, subTableLocations);
8284
case 4:
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2020 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
package com.itextpdf.io.font.otf;
24+
25+
import java.util.HashMap;
26+
import java.util.List;
27+
import java.util.Map;
28+
29+
30+
/**
31+
* Lookup Type 1: Single Adjustment Positioning Subtable
32+
*/
33+
public class GposLookupType1 extends OpenTableLookup {
34+
35+
private static final long serialVersionUID = 4562279115440679363L;
36+
37+
private Map<Integer, GposValueRecord> valueRecordMap = new HashMap<>();
38+
39+
public GposLookupType1(OpenTypeFontTableReader openReader, int lookupFlag, int[] subTableLocations)
40+
throws java.io.IOException {
41+
super(openReader, lookupFlag, subTableLocations);
42+
readSubTables();
43+
}
44+
45+
@Override
46+
public boolean transformOne(GlyphLine line) {
47+
if (line.idx >= line.end) {
48+
return false;
49+
}
50+
if (openReader.isSkip(line.get(line.idx).getCode(), lookupFlag)) {
51+
line.idx++;
52+
return false;
53+
}
54+
int glyphCode = line.get(line.idx).getCode();
55+
boolean positionApplied = false;
56+
GposValueRecord valueRecord = valueRecordMap.get(glyphCode);
57+
if (valueRecord != null) {
58+
Glyph newGlyph = new Glyph(line.get(line.idx));
59+
newGlyph.xAdvance += (short)valueRecord.XAdvance;
60+
newGlyph.yAdvance += (short)valueRecord.YAdvance;
61+
line.set(line.idx, newGlyph);
62+
positionApplied = true;
63+
}
64+
line.idx++;
65+
return positionApplied;
66+
}
67+
68+
@Override
69+
protected void readSubTable(int subTableLocation) throws java.io.IOException {
70+
openReader.rf.seek(subTableLocation);
71+
openReader.rf.readShort();
72+
int coverage = openReader.rf.readUnsignedShort();
73+
int valueFormat = openReader.rf.readUnsignedShort();
74+
GposValueRecord valueRecord = OtfReadCommon.readGposValueRecord(openReader, valueFormat);
75+
List<Integer> coverageGlyphIds = openReader.readCoverageFormat(subTableLocation + coverage);
76+
for (Integer glyphId : coverageGlyphIds) {
77+
valueRecordMap.put((int)glyphId, valueRecord);
78+
}
79+
}
80+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2020 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
package com.itextpdf.io.font.otf;
24+
25+
import com.itextpdf.io.font.FontProgramFactory;
26+
import com.itextpdf.io.font.TrueTypeFont;
27+
import com.itextpdf.test.ExtendedITextTest;
28+
import com.itextpdf.test.annotations.type.IntegrationTest;
29+
30+
import java.io.IOException;
31+
import java.util.Arrays;
32+
import java.util.List;
33+
import org.junit.Assert;
34+
import org.junit.Test;
35+
import org.junit.experimental.categories.Category;
36+
37+
@Category(IntegrationTest.class)
38+
public class GposLookupType1Test extends ExtendedITextTest {
39+
private static final String RESOURCE_FOLDER = "./src/test/resources/com/itextpdf/io/font/otf/GposLookupType1Test/";
40+
41+
@Test
42+
public void verifyXAdvanceIsApplied() throws IOException {
43+
TrueTypeFont fontProgram = (TrueTypeFont) FontProgramFactory.createFont(RESOURCE_FOLDER + "NotoSansMyanmar-Regular.ttf");
44+
GlyphPositioningTableReader gposTableReader = fontProgram.getGposTable();
45+
GposLookupType1 lookup = (GposLookupType1) gposTableReader.getLookupTable(29);
46+
List<Glyph> glyphs = Arrays.asList(new Glyph(fontProgram.getGlyphByCode(174)),
47+
new Glyph(fontProgram.getGlyphByCode(5)));
48+
49+
GlyphLine gl = new GlyphLine(glyphs);
50+
gl.idx = 0;
51+
52+
Assert.assertEquals(0, gl.get(0).getXAdvance());
53+
54+
Assert.assertTrue(lookup.transformOne(gl));
55+
56+
Assert.assertEquals(219, gl.get(0).getXAdvance());
57+
}
58+
59+
@Test
60+
public void verifyPositionIsNotAppliedForIrrelevantGlyph() throws IOException {
61+
TrueTypeFont fontProgram = (TrueTypeFont) FontProgramFactory.createFont(RESOURCE_FOLDER + "NotoSansMyanmar-Regular.ttf");
62+
GlyphPositioningTableReader gposTableReader = fontProgram.getGposTable();
63+
GposLookupType1 lookup = (GposLookupType1) gposTableReader.getLookupTable(29);
64+
List<Glyph> glyphs = Arrays.asList(new Glyph(fontProgram.getGlyphByCode(5)),
65+
new Glyph(fontProgram.getGlyphByCode(174)));
66+
67+
GlyphLine gl = new GlyphLine(glyphs);
68+
gl.idx = 0;
69+
70+
Assert.assertEquals(0, gl.get(0).getXAdvance());
71+
72+
Assert.assertFalse(lookup.transformOne(gl));
73+
74+
Assert.assertEquals(0, gl.get(0).getXAdvance());
75+
}
76+
77+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
This Font Software is licensed under the SIL Open Font License,
2+
Version 1.1.
3+
4+
This license is copied below, and is also available with a FAQ at:
5+
http://scripts.sil.org/OFL
6+
7+
-----------------------------------------------------------
8+
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
9+
-----------------------------------------------------------
10+
11+
PREAMBLE
12+
The goals of the Open Font License (OFL) are to stimulate worldwide
13+
development of collaborative font projects, to support the font
14+
creation efforts of academic and linguistic communities, and to
15+
provide a free and open framework in which fonts may be shared and
16+
improved in partnership with others.
17+
18+
The OFL allows the licensed fonts to be used, studied, modified and
19+
redistributed freely as long as they are not sold by themselves. The
20+
fonts, including any derivative works, can be bundled, embedded,
21+
redistributed and/or sold with any software provided that any reserved
22+
names are not used by derivative works. The fonts and derivatives,
23+
however, cannot be released under any other type of license. The
24+
requirement for fonts to remain under this license does not apply to
25+
any document created using the fonts or their derivatives.
26+
27+
DEFINITIONS
28+
"Font Software" refers to the set of files released by the Copyright
29+
Holder(s) under this license and clearly marked as such. This may
30+
include source files, build scripts and documentation.
31+
32+
"Reserved Font Name" refers to any names specified as such after the
33+
copyright statement(s).
34+
35+
"Original Version" refers to the collection of Font Software
36+
components as distributed by the Copyright Holder(s).
37+
38+
"Modified Version" refers to any derivative made by adding to,
39+
deleting, or substituting -- in part or in whole -- any of the
40+
components of the Original Version, by changing formats or by porting
41+
the Font Software to a new environment.
42+
43+
"Author" refers to any designer, engineer, programmer, technical
44+
writer or other person who contributed to the Font Software.
45+
46+
PERMISSION & CONDITIONS
47+
Permission is hereby granted, free of charge, to any person obtaining
48+
a copy of the Font Software, to use, study, copy, merge, embed,
49+
modify, redistribute, and sell modified and unmodified copies of the
50+
Font Software, subject to the following conditions:
51+
52+
1) Neither the Font Software nor any of its individual components, in
53+
Original or Modified Versions, may be sold by itself.
54+
55+
2) Original or Modified Versions of the Font Software may be bundled,
56+
redistributed and/or sold with any software, provided that each copy
57+
contains the above copyright notice and this license. These can be
58+
included either as stand-alone text files, human-readable headers or
59+
in the appropriate machine-readable metadata fields within text or
60+
binary files as long as those fields can be easily viewed by the user.
61+
62+
3) No Modified Version of the Font Software may use the Reserved Font
63+
Name(s) unless explicit written permission is granted by the
64+
corresponding Copyright Holder. This restriction only applies to the
65+
primary font name as presented to the users.
66+
67+
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
68+
Software shall not be used to promote, endorse or advertise any
69+
Modified Version, except to acknowledge the contribution(s) of the
70+
Copyright Holder(s) and the Author(s) or with their explicit written
71+
permission.
72+
73+
5) The Font Software, modified or unmodified, in part or in whole,
74+
must be distributed entirely under this license, and must not be
75+
distributed under any other license. The requirement for fonts to
76+
remain under this license does not apply to any document created using
77+
the Font Software.
78+
79+
TERMINATION
80+
This license becomes null and void if any of the above conditions are
81+
not met.
82+
83+
DISCLAIMER
84+
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
85+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
86+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
87+
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
88+
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
89+
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
90+
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
91+
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
92+
OTHER DEALINGS IN THE FONT SOFTWARE.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This software uses the following test resources under the following licenses:
2+
| NotoSansMyanmar-Regular.ttf | OFL-1.1 | LICENSE-OFL.txt |

0 commit comments

Comments
 (0)