From a5c4a9c5d50b8766015e74046be3baeb34916205 Mon Sep 17 00:00:00 2001 From: Vincent Hennebert Date: Wed, 30 May 2012 13:32:35 +0000 Subject: Renamed BFEntry into CMapSegment git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_TrueTypeInPostScript@1344251 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/fop/fonts/BFEntry.java | 111 --------------------- src/java/org/apache/fop/fonts/CMapSegment.java | 106 ++++++++++++++++++++ src/java/org/apache/fop/fonts/CustomFont.java | 10 +- src/java/org/apache/fop/fonts/FontReader.java | 8 +- src/java/org/apache/fop/fonts/MultiByteFont.java | 26 ++--- src/java/org/apache/fop/fonts/apps/TTFReader.java | 4 +- .../org/apache/fop/fonts/truetype/TTFFile.java | 17 ++-- .../apache/fop/fonts/truetype/TTFFontLoader.java | 14 +-- src/java/org/apache/fop/render/ps/PSFontUtils.java | 28 +++--- 9 files changed, 160 insertions(+), 164 deletions(-) delete mode 100644 src/java/org/apache/fop/fonts/BFEntry.java create mode 100644 src/java/org/apache/fop/fonts/CMapSegment.java (limited to 'src/java/org/apache') diff --git a/src/java/org/apache/fop/fonts/BFEntry.java b/src/java/org/apache/fop/fonts/BFEntry.java deleted file mode 100644 index 0841f36c8..000000000 --- a/src/java/org/apache/fop/fonts/BFEntry.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * 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.fonts; - -/** - * This is just a holder class for bfentries, groups of characters of a base font (bf). - */ -public final class BFEntry { - - //TODO Think about renaming this class to CMapRange or something. - - private final int unicodeStart; - private final int unicodeEnd; - private final int glyphStartIndex; - - /** - * Main constructor. - * @param unicodeStart Unicode start index - * @param unicodeEnd Unicode end index - * @param glyphStartIndex glyph start index - */ - public BFEntry(int unicodeStart, int unicodeEnd, int glyphStartIndex) { - this.unicodeStart = unicodeStart; - this.unicodeEnd = unicodeEnd; - this.glyphStartIndex = glyphStartIndex; - } - - /** - * {@inheritDoc} - */ - @Override - public int hashCode() { - int hc = 17; - hc = 31 * hc + unicodeStart; - hc = 31 * hc + unicodeEnd; - hc = 31 * hc + glyphStartIndex; - return hc; - } - - /** - * {@inheritDoc} - */ - @Override - public boolean equals(Object o) { - if (o instanceof BFEntry) { - BFEntry ce = (BFEntry) o; - return ce.unicodeStart == this.unicodeStart - && ce.unicodeEnd == this.unicodeEnd - && ce.glyphStartIndex == this.glyphStartIndex; - } - return false; - } - - /** - * Returns the unicodeStart. - * @return the Unicode start index - */ - public int getUnicodeStart() { - return unicodeStart; - } - - /** - * Returns the unicodeEnd. - * @return the Unicode end index - */ - public int getUnicodeEnd() { - return unicodeEnd; - } - - /** - * Returns the glyphStartIndex. - * @return the glyph start index - */ - public int getGlyphStartIndex() { - return glyphStartIndex; - } - - /** {@inheritDoc} */ - @Override - public String toString() { - StringBuilder sb = new StringBuilder("BFEntry: "); - sb.append ( "{ UC[" ); - sb.append ( unicodeStart ); - sb.append ( ',' ); - sb.append ( unicodeEnd ); - sb.append ( "]: GC[" ); - sb.append ( glyphStartIndex ); - sb.append ( ',' ); - sb.append ( glyphStartIndex + ( unicodeEnd - unicodeStart ) ); - sb.append ( "] }" ); - return sb.toString(); - } - -} diff --git a/src/java/org/apache/fop/fonts/CMapSegment.java b/src/java/org/apache/fop/fonts/CMapSegment.java new file mode 100644 index 000000000..816df2ca0 --- /dev/null +++ b/src/java/org/apache/fop/fonts/CMapSegment.java @@ -0,0 +1,106 @@ +/* + * 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.fonts; + +/** + * A segment in a cmap table of format 4. Unicode code points between + * {@link #getUnicodeStart()} and {@link #getUnicodeEnd()} map to contiguous glyph indices + * starting from {@link #getGlyphStartIndex()}. + */ +public final class CMapSegment { + + private final int unicodeStart; + private final int unicodeEnd; + private final int glyphStartIndex; + + /** + * Creates a new segment. + * + * @param unicodeStart Unicode start index + * @param unicodeEnd Unicode end index + * @param glyphStartIndex glyph start index + */ + public CMapSegment(int unicodeStart, int unicodeEnd, int glyphStartIndex) { + this.unicodeStart = unicodeStart; + this.unicodeEnd = unicodeEnd; + this.glyphStartIndex = glyphStartIndex; + } + + @Override + public int hashCode() { + int hc = 17; + hc = 31 * hc + unicodeStart; + hc = 31 * hc + unicodeEnd; + hc = 31 * hc + glyphStartIndex; + return hc; + } + + @Override + public boolean equals(Object o) { + if (o instanceof CMapSegment) { + CMapSegment ce = (CMapSegment) o; + return ce.unicodeStart == this.unicodeStart + && ce.unicodeEnd == this.unicodeEnd + && ce.glyphStartIndex == this.glyphStartIndex; + } + return false; + } + + /** + * Returns the unicodeStart. + * @return the Unicode start index + */ + public int getUnicodeStart() { + return unicodeStart; + } + + /** + * Returns the unicodeEnd. + * @return the Unicode end index + */ + public int getUnicodeEnd() { + return unicodeEnd; + } + + /** + * Returns the glyphStartIndex. + * @return the glyph start index + */ + public int getGlyphStartIndex() { + return glyphStartIndex; + } + + /** {@inheritDoc} */ + @Override + public String toString() { + StringBuilder sb = new StringBuilder("CMapSegment: "); + sb.append ( "{ UC[" ); + sb.append ( unicodeStart ); + sb.append ( ',' ); + sb.append ( unicodeEnd ); + sb.append ( "]: GC[" ); + sb.append ( glyphStartIndex ); + sb.append ( ',' ); + sb.append ( glyphStartIndex + ( unicodeEnd - unicodeStart ) ); + sb.append ( "] }" ); + return sb.toString(); + } + +} diff --git a/src/java/org/apache/fop/fonts/CustomFont.java b/src/java/org/apache/fop/fonts/CustomFont.java index 7386b7341..b506393a6 100644 --- a/src/java/org/apache/fop/fonts/CustomFont.java +++ b/src/java/org/apache/fop/fonts/CustomFont.java @@ -64,7 +64,7 @@ public abstract class CustomFont extends Typeface private boolean useAdvanced = true; /** the character map, mapping Unicode ranges to glyph indices. */ - protected BFEntry[] cmap; + protected CMapSegment[] cmap; /** {@inheritDoc} */ public String getFontName() { @@ -497,8 +497,8 @@ public abstract class CustomFont extends Typeface * to their glyph indices inside the font. * @param cmap the identity character map */ - public void setCMap(BFEntry[] cmap) { - this.cmap = new BFEntry[cmap.length]; + public void setCMap(CMapSegment[] cmap) { + this.cmap = new CMapSegment[cmap.length]; System.arraycopy(cmap, 0, this.cmap, 0, cmap.length); } @@ -507,8 +507,8 @@ public abstract class CustomFont extends Typeface * to their glyph indices inside the font. * @return the identity character map */ - public BFEntry[] getCMap() { - BFEntry[] copy = new BFEntry[cmap.length]; + public CMapSegment[] getCMap() { + CMapSegment[] copy = new CMapSegment[cmap.length]; System.arraycopy(this.cmap, 0, copy, 0, this.cmap.length); return copy; } diff --git a/src/java/org/apache/fop/fonts/FontReader.java b/src/java/org/apache/fop/fonts/FontReader.java index 381eec7f4..46ea9123d 100644 --- a/src/java/org/apache/fop/fonts/FontReader.java +++ b/src/java/org/apache/fop/fonts/FontReader.java @@ -64,7 +64,7 @@ public class FontReader extends DefaultHandler { private Map currentKerning = null; - private List bfranges = null; + private List bfranges = null; private void createFont(InputSource source) throws FOPException { XMLReader parser = null; @@ -201,9 +201,9 @@ public class FontReader extends DefaultHandler { returnFont.putKerningEntry(new Integer(attributes.getValue("kpx1")), currentKerning); } else if ("bfranges".equals(localName)) { - bfranges = new ArrayList(); + bfranges = new ArrayList(); } else if ("bf".equals(localName)) { - BFEntry entry = new BFEntry(getInt(attributes.getValue("us")), + CMapSegment entry = new CMapSegment(getInt(attributes.getValue("us")), getInt(attributes.getValue("ue")), getInt(attributes.getValue("gi"))); bfranges.add(entry); @@ -307,7 +307,7 @@ public class FontReader extends DefaultHandler { multiFont.setWidthArray(wds); } else if ("bfranges".equals(localName)) { - multiFont.setCMap(bfranges.toArray(new BFEntry[0])); + multiFont.setCMap(bfranges.toArray(new CMapSegment[0])); } text.setLength(0); //Reset text buffer (see characters()) } diff --git a/src/java/org/apache/fop/fonts/MultiByteFont.java b/src/java/org/apache/fop/fonts/MultiByteFont.java index c249c4d19..4db5ece84 100644 --- a/src/java/org/apache/fop/fonts/MultiByteFont.java +++ b/src/java/org/apache/fop/fonts/MultiByteFont.java @@ -183,17 +183,17 @@ public class MultiByteFont extends CIDFont implements Substitutable, Positionabl } /** - * Add a private use mapping {PU,GI} to the existing BFENTRIES map. + * Add a private use mapping {PU,GI} to the existing character map. * N.B. Does not insert in order, merely appends to end of existing map. */ private synchronized void addPrivateUseMapping ( int pu, int gi ) { assert findGlyphIndex ( pu ) == SingleByteEncoding.NOT_FOUND_CODE_POINT; - BFEntry[] bfeOld = cmap; - int bfeCnt = bfeOld.length; - BFEntry[] bfeNew = new BFEntry [ bfeCnt + 1 ]; - System.arraycopy ( bfeOld, 0, bfeNew, 0, bfeCnt ); - bfeNew [ bfeCnt ] = new BFEntry ( pu, pu, gi ); - cmap = bfeNew; + CMapSegment[] oldCmap = cmap; + int cmapLength = oldCmap.length; + CMapSegment[] newCmap = new CMapSegment [ cmapLength + 1 ]; + System.arraycopy ( oldCmap, 0, newCmap, 0, cmapLength ); + newCmap [ cmapLength ] = new CMapSegment ( pu, pu, gi ); + cmap = newCmap; } /** @@ -252,11 +252,11 @@ public class MultiByteFont extends CIDFont implements Substitutable, Positionabl private int findCharacterFromGlyphIndex ( int gi, boolean augment ) { int cc = 0; for ( int i = 0, n = cmap.length; i < n; i++ ) { - BFEntry be = cmap [ i ]; - int s = be.getGlyphStartIndex(); - int e = s + ( be.getUnicodeEnd() - be.getUnicodeStart() ); + CMapSegment segment = cmap [ i ]; + int s = segment.getGlyphStartIndex(); + int e = s + ( segment.getUnicodeEnd() - segment.getUnicodeStart() ); if ( ( gi >= s ) && ( gi <= e ) ) { - cc = be.getUnicodeStart() + ( gi - s ); + cc = segment.getUnicodeStart() + ( gi - s ); break; } } @@ -296,10 +296,10 @@ public class MultiByteFont extends CIDFont implements Substitutable, Positionabl * Sets the array of BFEntry instances which constitutes the Unicode to glyph index map for * a font. ("BF" means "base font") * @param entries the Unicode to glyph index map - * @deprecated use {@link #setCMap(BFEntry[])} instead + * @deprecated use {@link #setCMap(CMapSegment[])} instead */ @Deprecated - public void setBFEntries(BFEntry[] entries) { + public void setBFEntries(CMapSegment[] entries) { setCMap(entries); } diff --git a/src/java/org/apache/fop/fonts/apps/TTFReader.java b/src/java/org/apache/fop/fonts/apps/TTFReader.java index 59a5c47bb..224c8de2f 100644 --- a/src/java/org/apache/fop/fonts/apps/TTFReader.java +++ b/src/java/org/apache/fop/fonts/apps/TTFReader.java @@ -33,7 +33,7 @@ import org.xml.sax.SAXException; import org.apache.commons.logging.LogFactory; import org.apache.fop.Version; -import org.apache.fop.fonts.BFEntry; +import org.apache.fop.fonts.CMapSegment; import org.apache.fop.fonts.FontUtil; import org.apache.fop.fonts.truetype.FontFileReader; import org.apache.fop.fonts.truetype.TTFFile; @@ -385,7 +385,7 @@ public class TTFReader extends AbstractFontReader { el = doc.createElement("bfranges"); mel.appendChild(el); - for (BFEntry ce : ttf.getCMaps()) { + for (CMapSegment ce : ttf.getCMaps()) { Element el2 = doc.createElement("bf"); el.appendChild(el2); el2.setAttribute("us", String.valueOf(ce.getUnicodeStart())); diff --git a/src/java/org/apache/fop/fonts/truetype/TTFFile.java b/src/java/org/apache/fop/fonts/truetype/TTFFile.java index 168b49482..77cdddcdd 100644 --- a/src/java/org/apache/fop/fonts/truetype/TTFFile.java +++ b/src/java/org/apache/fop/fonts/truetype/TTFFile.java @@ -43,7 +43,7 @@ import org.apache.fop.complexscripts.fonts.GlyphDefinitionTable; import org.apache.fop.complexscripts.fonts.GlyphPositioningTable; import org.apache.fop.complexscripts.fonts.GlyphSubstitutionTable; import org.apache.fop.complexscripts.fonts.OTFAdvancedTypographicTableReader; -import org.apache.fop.fonts.BFEntry; +import org.apache.fop.fonts.CMapSegment; import org.apache.fop.fonts.FontUtil; /** @@ -161,7 +161,7 @@ public class TTFFile { protected Map dirTabs; private Map> kerningTab; // for CIDs private Map> ansiKerningTab; // For winAnsiEncoding - private List cmaps; + private List cmaps; private Set unicodeMappings; private int upem; // unitsPerEm from "head" table @@ -764,7 +764,7 @@ public class TTFFile { } private void createCMaps() { - cmaps = new ArrayList(); + cmaps = new ArrayList(); int unicodeStart; int glyphStart; int unicodeEnd; @@ -781,7 +781,7 @@ public class TTFFile { if (((lastMapping.getUnicodeIndex() + 1) != um.getUnicodeIndex()) || ((lastMapping.getGlyphIndex() + 1) != um.getGlyphIndex())) { unicodeEnd = lastMapping.getUnicodeIndex(); - cmaps.add(new BFEntry(unicodeStart, unicodeEnd, glyphStart)); + cmaps.add(new CMapSegment(unicodeStart, unicodeEnd, glyphStart)); unicodeStart = um.getUnicodeIndex(); glyphStart = um.getGlyphIndex(); } @@ -789,7 +789,7 @@ public class TTFFile { } unicodeEnd = lastMapping.getUnicodeIndex(); - cmaps.add(new BFEntry(unicodeStart, unicodeEnd, glyphStart)); + cmaps.add(new CMapSegment(unicodeStart, unicodeEnd, glyphStart)); } /** @@ -1758,10 +1758,11 @@ public class TTFFile { } /** - * Return a List with TTFCmapEntry. - * @return A list of TTFCmapEntry objects + * Returns this font's character to glyph mapping. + * + * @return the font's cmap */ - public List getCMaps() { + public List getCMaps() { return cmaps; } diff --git a/src/java/org/apache/fop/fonts/truetype/TTFFontLoader.java b/src/java/org/apache/fop/fonts/truetype/TTFFontLoader.java index 9a19f287b..9c6098de2 100644 --- a/src/java/org/apache/fop/fonts/truetype/TTFFontLoader.java +++ b/src/java/org/apache/fop/fonts/truetype/TTFFontLoader.java @@ -26,7 +26,7 @@ import java.util.Set; import org.apache.commons.io.IOUtils; -import org.apache.fop.fonts.BFEntry; +import org.apache.fop.fonts.CMapSegment; import org.apache.fop.fonts.CIDFontType; import org.apache.fop.fonts.EmbeddingMode; import org.apache.fop.fonts.EncodingMode; @@ -181,8 +181,8 @@ public class TTFFontLoader extends FontLoader { } } - private BFEntry[] getCMap(TTFFile ttf) { - BFEntry[] array = new BFEntry[ttf.getCMaps().size()]; + private CMapSegment[] getCMap(TTFFile ttf) { + CMapSegment[] array = new CMapSegment[ttf.getCMaps().size()]; return ttf.getCMaps().toArray(array); } @@ -192,12 +192,12 @@ public class TTFFontLoader extends FontLoader { singleFont.setWidth(i, ttf.getCharWidth(i)); } - for (BFEntry ce : ttf.getCMaps()) { - if (ce.getUnicodeStart() < 0xFFFE) { - for (char u = (char)ce.getUnicodeStart(); u <= ce.getUnicodeEnd(); u++) { + for (CMapSegment segment : ttf.getCMaps()) { + if (segment.getUnicodeStart() < 0xFFFE) { + for (char u = (char)segment.getUnicodeStart(); u <= segment.getUnicodeEnd(); u++) { int codePoint = singleFont.getEncoding().mapChar(u); if (codePoint <= 0) { - int glyphIndex = ce.getGlyphStartIndex() + u - ce.getUnicodeStart(); + int glyphIndex = segment.getGlyphStartIndex() + u - segment.getUnicodeStart(); String glyphName = ttf.getGlyphName(glyphIndex); if (glyphName == "" && ttf.getPostScriptVersion() != PostScriptVersion.V2) { glyphName = "u" + HexEncoder.encode(u); diff --git a/src/java/org/apache/fop/render/ps/PSFontUtils.java b/src/java/org/apache/fop/render/ps/PSFontUtils.java index 157f4f419..53ceff53e 100644 --- a/src/java/org/apache/fop/render/ps/PSFontUtils.java +++ b/src/java/org/apache/fop/render/ps/PSFontUtils.java @@ -40,7 +40,7 @@ import org.apache.xmlgraphics.ps.PSGenerator; import org.apache.xmlgraphics.ps.PSResource; import org.apache.xmlgraphics.ps.dsc.ResourceTracker; -import org.apache.fop.fonts.BFEntry; +import org.apache.fop.fonts.CMapSegment; import org.apache.fop.fonts.Base14Font; import org.apache.fop.fonts.CIDFontType; import org.apache.fop.fonts.CIDSubset; @@ -314,7 +314,7 @@ public class PSFontUtils extends org.apache.xmlgraphics.ps.PSFontUtils { } private static void createType42DictionaryEntries(PSGenerator gen, CustomFont font, - BFEntry[] cmap, TTFFile ttfFile) throws IOException { + CMapSegment[] cmap, TTFFile ttfFile) throws IOException { gen.write("/FontName /"); gen.write(font.getEmbedFontName()); gen.writeln(" def"); @@ -354,14 +354,14 @@ public class PSFontUtils extends org.apache.xmlgraphics.ps.PSFontUtils { } private static void buildCharStrings(PSGenerator gen, boolean buildCharStrings, - BFEntry[] cmap, Set glyphNames, CustomFont font) throws IOException { + CMapSegment[] cmap, Set glyphNames, CustomFont font) throws IOException { gen.write("/CharStrings "); if (!buildCharStrings) { gen.write(1); } else if (font.getEmbeddingMode() != EmbeddingMode.FULL) { int charCount = 1; //1 for .notdef - for (BFEntry entry : cmap) { - charCount += entry.getUnicodeEnd() - entry.getUnicodeStart() + 1; + for (CMapSegment segment : cmap) { + charCount += segment.getUnicodeEnd() - segment.getUnicodeStart() + 1; } gen.write(charCount); } else { @@ -378,9 +378,9 @@ public class PSFontUtils extends org.apache.xmlgraphics.ps.PSFontUtils { } if (font.getEmbeddingMode() != EmbeddingMode.FULL) { //Only performed in singly-byte mode, ignored for CID fonts - for (BFEntry entry : cmap) { - int glyphIndex = entry.getGlyphStartIndex(); - for (int ch = entry.getUnicodeStart(); ch <= entry.getUnicodeEnd(); ch++) { + for (CMapSegment segment : cmap) { + int glyphIndex = segment.getGlyphStartIndex(); + for (int ch = segment.getUnicodeStart(); ch <= segment.getUnicodeEnd(); ch++) { char ch16 = (char)ch; //TODO Handle Unicode characters beyond 16bit String glyphName = Glyphs.charToGlyphName(ch16); if ("".equals(glyphName)) { @@ -410,10 +410,10 @@ public class PSFontUtils extends org.apache.xmlgraphics.ps.PSFontUtils { gen.writeln(" def"); } - private static int getGlyphIndex(char c, BFEntry[] cmap) { - for (BFEntry entry : cmap) { - if (entry.getUnicodeStart() <= c && c <= entry.getUnicodeEnd()) { - return entry.getGlyphStartIndex() + c - entry.getUnicodeStart(); + private static int getGlyphIndex(char c, CMapSegment[] cmap) { + for (CMapSegment segment : cmap) { + if (segment.getUnicodeStart() <= c && c <= segment.getUnicodeEnd()) { + return segment.getGlyphStartIndex() + c - segment.getUnicodeStart(); } } return 0; @@ -509,7 +509,7 @@ public class PSFontUtils extends org.apache.xmlgraphics.ps.PSFontUtils { } - createType42DictionaryEntries(gen, font, new BFEntry[0], ttfFile); + createType42DictionaryEntries(gen, font, new CMapSegment[0], ttfFile); gen.writeln("CIDFontName currentdict end /CIDFont defineresource pop"); gen.writeln("end"); gen.writeln("%%EndResource"); @@ -685,7 +685,7 @@ public class PSFontUtils extends org.apache.xmlgraphics.ps.PSFontUtils { private static PSResource defineDerivedTrueTypeFont(PSGenerator gen, PSEventProducer eventProducer, String baseFontName, String fontName, - SingleByteEncoding encoding, BFEntry[] cmap) throws IOException { + SingleByteEncoding encoding, CMapSegment[] cmap) throws IOException { checkPostScriptLevel3(gen, eventProducer); PSResource res = new PSResource(PSResource.TYPE_FONT, fontName); gen.writeDSCComment(DSCConstants.BEGIN_RESOURCE, res); -- cgit v1.2.3