Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* NOTICE: This file has been changed by Plutext Pty Ltd for use in docx4j.
   * The package name has been changed; there may also be other changes.
   * 
   * This notice is included to meet the condition in clause 4(b) of the License. 
   */
  
   /*
   * 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: SingleByteFont.java 742620 2009-02-09 16:58:29Z jeremias $ */
 
 package org.docx4j.fonts.fop.fonts;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
Generic SingleByte font
 
 public class SingleByteFont extends CustomFont {

    
logger
 
     private  static Logger log = LoggerFactory.getLogger(SingleByteFont.class);
 
     private SingleByteEncoding mapping;
     private boolean useNativeEncoding = false;
 
     private int[] width = null;
 
     private Map unencodedCharacters;
     //Map<Character, UnencodedCharacter>
     private List additionalEncodings;


    
Main constructor.
 
     public SingleByteFont() {
     }

    
 
     public boolean isEmbeddable() {
         return (!(getEmbedFileName() == null
                 && getEmbedResourceName() == null));
     }

    
 
     public String getEncodingName() {
         return this..getName();
     }

    
Returns the code point mapping (encoding) of this font.

Returns:
the code point mapping
 
     public SingleByteEncoding getEncoding() {
         return this.;
     }

    
 
     public int getWidth(int iint size) {
         if (i < 256) {
             int idx = i - getFirstChar();
             if (idx >= 0 && idx < .) {
                 return size * [i - getFirstChar()];
             }
         } else if (this. != null) {
             int encodingIndex = (i / 256) - 1;
             SimpleSingleByteEncoding encoding = getAdditionalEncoding(encodingIndex);
             int codePoint = i % 256;
             NamedCharacter nc = encoding.getCharacterForIndex(codePoint);
             UnencodedCharacter uc
                 = (UnencodedCharacter)this..get(
                         new Character(nc.getSingleUnicodeValue()));
             return size * uc.getWidth();
         }
         return 0;
     }

    
    public int[] getWidths() {
        int[] arr = new int[.];
        System.arraycopy(, 0, arr, 0, . - 1);
        return arr;
    }

    
    public char mapChar(char c) {
        notifyMapOperation();
        char d = .mapChar(c);
        if (d != .) {
            return d;
        }
        //Check unencoded characters which are available in the font by character name
        d = mapUnencodedChar(c);
        if (d != .) {
            return d;
        }
        this.warnMissingGlyph(c);
        return .;
    }
    private char mapUnencodedChar(char ch) {
        if (this. != null) {
            UnencodedCharacter unencoded
                = (UnencodedCharacter)this..get(new Character(ch));
            if (unencoded != null) {
                if (this. == null) {
                    this. = new java.util.ArrayList();
                }
                SimpleSingleByteEncoding encoding = null;
                char mappedStart = 0;
                int additionalsCount = this..size();
                for (int i = 0; i < additionalsCounti++) {
                    mappedStart += 256;
                    encoding = getAdditionalEncoding(i);
                    char alt = encoding.mapChar(ch);
                    if (alt != 0) {
                        return (char)(mappedStart + alt);
                    }
                }
                if (encoding != null && encoding.isFull()) {
                    encoding = null;
                }
                if (encoding == null) {
                    encoding = new SimpleSingleByteEncoding(
                            getFontName() + "EncodingSupp" + (additionalsCount + 1));
                    this..add(encoding);
                    mappedStart += 256;
                }
                return (char)(mappedStart + encoding.addCharacter(unencoded.getCharacter()));
            }
        }
        return 0;
    }

    
    public boolean hasChar(char c) {
        char d = .mapChar(c);
        if (d != .) {
            return true;
        }
        //Check unencoded characters which are available in the font by character name
        d = mapUnencodedChar(c);
        if (d != .) {
            return true;
        }
        return false;
    }
    /* ---- single byte font specific setters --- */

    
Updates the mapping variable based on the encoding.

Parameters:
encoding the name of the encoding
    protected void updateMapping(String encoding) {
        try {
            this. = CodePointMapping.getMapping(encoding);
        } catch (UnsupportedOperationException e) {
            .error("Font '" + super.getFontName() + "': " + e.getMessage());
        }
    }

    
Sets the encoding of the font.

Parameters:
encoding the encoding (ex. "WinAnsiEncoding" or "SymbolEncoding")
    public void setEncoding(String encoding) {
        updateMapping(encoding);
    }

    
Sets the encoding of the font.

Parameters:
encoding the encoding information
    public void setEncoding(CodePointMapping encoding) {
        this. = encoding;
    }

    
Controls whether the font is configured to use its native encoding or if it may need to be re-encoded for the target format.

Parameters:
value true indicates that the configured encoding is the font's native encoding
    public void setUseNativeEncoding(boolean value) {
        this. = value;
    }

    
Indicates whether this font is configured to use its native encoding. This method is used to determine whether the font needs to be re-encoded.

Returns:
true if the font uses its native encoding.
    public boolean isUsingNativeEncoding() {
        return this.;
    }

    
Sets a width for a character.

Parameters:
index index of the character
w the width of the character
    public void setWidth(int indexint w) {
        if (this. == null) {
            this. = new int[getLastChar() - getFirstChar() + 1];
        }
        this.[index - getFirstChar()] = w;
    }

    
Adds an unencoded character (one that is not supported by the primary encoding).

Parameters:
ch the named character
width the width of the character
    public void addUnencodedCharacter(NamedCharacter chint width) {
        if (this. == null) {
            this. = new java.util.HashMap();
        }
        if (ch.hasSingleUnicodeValue()) {
            UnencodedCharacter uc = new UnencodedCharacter(chwidth);
            this..put(new Character(ch.getSingleUnicodeValue()), uc);
        } else {
            //Cannot deal with unicode sequences, so ignore this character
        }
    }

    
Makes all unencoded characters available through additional encodings. This method is used in cases where the fonts need to be encoded in the target format before all text of the document is processed (for example in PostScript when resource optimization is disabled).
    public void encodeAllUnencodedCharacters() {
        if (this. != null) {
            Set sortedKeys = new java.util.TreeSet(this..keySet());
            Iterator iter = sortedKeys.iterator();
            while (iter.hasNext()) {
                Character ch = (Character)iter.next();
                char mapped = mapChar(ch.charValue());
                assert mapped != .;
            }
        }
    }

    
Indicates whether the encoding has additional encodings besides the primary encoding.

Returns:
true if there are additional encodings.
    public boolean hasAdditionalEncodings() {
        return (this. != null) && (this..size() > 0);
    }

    
Returns the number of additional encodings this single-byte font maintains.

Returns:
the number of additional encodings
    public int getAdditionalEncodingCount() {
        if (hasAdditionalEncodings()) {
            return this..size();
        } else {
            return 0;
        }
    }

    
Returns an additional encoding.

Parameters:
index the index of the additional encoding
Returns:
the additional encoding
Throws:
java.lang.IndexOutOfBoundsException if the index is out of bounds
            throws IndexOutOfBoundsException {
        if (hasAdditionalEncodings()) {
            return (SimpleSingleByteEncoding)this..get(index);
        } else {
            throw new IndexOutOfBoundsException("No additional encodings available");
        }
    }

    
Returns an array with the widths for an additional encoding.

Parameters:
index the index of the additional encoding
Returns:
the width array
    public int[] getAdditionalWidths(int index) {
        SimpleSingleByteEncoding enc = getAdditionalEncoding(index);
        int[] arr = new int[enc.getLastChar() - enc.getFirstChar() + 1];
        for (int i = 0, c = arr.lengthi < ci++) {
            NamedCharacter nc = enc.getCharacterForIndex(enc.getFirstChar() + i);
            UnencodedCharacter uc = (UnencodedCharacter)this..get(
                    new Character(nc.getSingleUnicodeValue()));
            arr[i] = uc.getWidth();
        }
        return arr;
    }
    private static final class UnencodedCharacter {
        private NamedCharacter character;
        private int width;
        public UnencodedCharacter(NamedCharacter characterint width) {
            this. = character;
            this. = width;
        }
        public NamedCharacter getCharacter() {
            return this.;
        }
        public int getWidth() {
            return this.;
        }

        
        public String toString() {
            return getCharacter().toString();
        }
    }
New to GrepCode? Check out our FAQ X