Calculates the length of the charset according to its format
@param Offset The Charset Offset
@param NumofGlyphs Number of glyphs in the font
@return the length of the Charset
774537
int CountCharset(int Offset,int NumofGlyphs){
    	int format;
    	int Length=0;
    	seek(Offset);
    	// Read the format
    	format = getCard8();
    	// Calc according to format
    	switch (format){
    		case 0:
    			Length = 1+2*NumofGlyphs;
    			break;
    		case 1:
    			Length = 1+3*CountRange(NumofGlyphs,1);
    			break;
    		case 2:
    			Length = 1+4*CountRange(NumofGlyphs,2);
    			break;
    		default:
    			break;
    	}
    	return Length;
    }
18971
public OffsetRange<CharOffset> offsets() {
    final CharOffset start = tokens.get(0).offsets().startInclusive();
    final CharOffset end = tokens.reverse().get(0).offsets().endInclusive();
    return OffsetRange.charOffsetRange(start.asInt(), end.asInt());
  }
372890
def getCharStringForGlyph(self, glyph, private, globalSubrs):
        
        width = glyph.width
        defaultWidth = private.defaultWidthX
        nominalWidth = private.nominalWidthX
        if width == defaultWidth:
            # if width equals the default it can be omitted from charstring
            width = None
        else:
            # subtract the nominal width
            width -= nominalWidth
        if width is not None:
            width = otRound(width)
        pen = T2CharStringPen(width, self.allGlyphs,
                              roundTolerance=self.roundTolerance)
        glyph.draw(pen)
        charString = pen.getCharString(
            private, globalSubrs, optimize=self.optimizeCFF
        )
        return charString
518757
static String hepburn(
        CharSequence text,
        int offset
    ) {

        StringBuilder sb = null;
        int n = Math.min(text.length(), offset + 32);

        for (int i = offset; i < n; i++) {
            final char c = text.charAt(i);
            char std;
            if (i == offset) { // capitalization
                std = ((c == '\u00D4') || (c == '\u00F4') || (c == '\u014D') ? '\u014C' : Character.toUpperCase(c));
                std = ((c == '\u00DB') || (c == '\u00FB') || (c == '\u016B') ? '\u016A' : std);
            } else {
                std = ((c == '\u00D4') || (c == '\u00F4') || (c == '\u014C') ? '\u014D' : Character.toLowerCase(c));
                std = ((c == '\u00DB') || (c == '\u00FB') || (c == '\u016A') ? '\u016B' : std);
            }
            std = ((c == '\'') ? '\u2019' : std); // use ’ as apostroph
            std = ((c == ' ') ? '-' : std); // use hyphen instead of space
            if ((sb != null) || (std != c)) {
                if (sb == null) {
                    sb = new StringBuilder(32);
                    sb.append(text.subSequence(offset, i));
                }
                sb.append(std);
            }
        }

        return ((sb == null) ? text.subSequence(offset, n).toString() : sb.toString());

    }
485062
public CharacterHit offset(int offsetAmount) {
        return new CharacterHit(
                charIdx.isPresent()
                        ? OptionalInt.of(charIdx.getAsInt() + offsetAmount)
                        : charIdx,
                insertionIndex + offsetAmount);
    }
464301
public float getWidthCorrected(float charSpacing, float wordSpacing)
    {
        if (image != null) {
            return image.getScaledWidth() + charSpacing;
        }
        int numberOfSpaces = 0;
        int idx = -1;
        while ((idx = value.indexOf(' ', idx + 1)) >= 0)
            ++numberOfSpaces;
        return width() + (value.length() * charSpacing + numberOfSpaces * wordSpacing);
    }
303464
public static char getChar(MemorySegment[] segments, int offset) {
		if (inFirstSegment(segments, offset, 2)) {
			return segments[0].getChar(offset);
		} else {
			return getCharMultiSegments(segments, offset);
		}
	}
432958
public static int calculateFixLengthPartSize(InternalType type) {
		if (type.equals(InternalTypes.BOOLEAN)) {
			return 1;
		} else if (type.equals(InternalTypes.BYTE)) {
			return 1;
		} else if (type.equals(InternalTypes.SHORT)) {
			return 2;
		} else if (type.equals(InternalTypes.INT)) {
			return 4;
		} else if (type.equals(InternalTypes.FLOAT)) {
			return 4;
		} else if (type.equals(InternalTypes.CHAR)) {
			return 2;
		} else if (type.equals(InternalTypes.DATE)) {
			return 4;
		} else if (type.equals(InternalTypes.TIME)) {
			return 4;
		} else {
			// long, double is 8 bytes.
			// It store the length and offset of variable-length part when type is string, map, etc.
			return 8;
		}
	}
439985
function getFirstCharIndex(glyphs) {
    var minGlyph = _.min(glyphs, function (glyph) {
        return (glyph.unicode === 0) ? 0xFFFF : (glyph.unicode || 0);
    });
    if (minGlyph) {
        return minGlyph.unicode > 0xFFFF ? 0xFFFF : (minGlyph.unicode || 0);
    } else {
        return 0xFFFF;
    }
}
138681
private int getCharWidth(String text, Align align)
    {
        final int width;
        if (align == Align.RIGHT)
        {
            width = getTextWidth(text);
        }
        else if (align == Align.CENTER)
        {
            width = getTextWidth(text) / 2;
        }
        else
        {
            width = 0;
        }
        return width;
    }
378212
byte[] convertToBytes(String text) {
        byte b[] = null;
        switch (fontType) {
            case BaseFont.FONT_TYPE_T3:
                return baseFont.convertToBytes(text);
            case BaseFont.FONT_TYPE_T1:
            case BaseFont.FONT_TYPE_TT: {
                b = baseFont.convertToBytes(text);
                int len = b.length;
                for (int k = 0; k < len; ++k)
                    shortTag[b[k] & 0xff] = 1;
                break;
            }
            case BaseFont.FONT_TYPE_CJK: {
                int len = text.length();
                for (int k = 0; k < len; ++k)
                    cjkTag.put(cjkFont.getCidCode(text.charAt(k)), 0);
                b = baseFont.convertToBytes(text);
                break;
            }
            case BaseFont.FONT_TYPE_DOCUMENT: {
                b = baseFont.convertToBytes(text);
                break;
            }
            case BaseFont.FONT_TYPE_TTUNI: {
                try {
                    int len = text.length();
                    int metrics[] = null;
                    char glyph[] = new char[len];
                    int i = 0;
                    if (symbolic) {
                        b = PdfEncodings.convertToBytes(text, "symboltt");
                        len = b.length;
                        for (int k = 0; k < len; ++k) {
                            metrics = ttu.getMetricsTT(b[k] & 0xff);
                            if (metrics == null)
                                continue;
                            longTag.put(Integer.valueOf(metrics[0]), new int[]{metrics[0], metrics[1], ttu.getUnicodeDifferences(b[k] & 0xff)});
                            glyph[i++] = (char)metrics[0];
                        }
                    }
                    else {
                    	for (int k = 0; k < len; ++k) {
                    		int val;
                    		if (Utilities.isSurrogatePair(text, k)) {
                    			val = Utilities.convertToUtf32(text, k);
                    			k++;
                    		}
                    		else {
                    			val = text.charAt(k);
                    		}
                    		metrics = ttu.getMetricsTT(val);
                    		if (metrics == null)
                    			continue;
                    		int m0 = metrics[0];
                    		Integer gl = Integer.valueOf(m0);
                    		if (!longTag.containsKey(gl))
                    			longTag.put(gl, new int[]{m0, metrics[1], val});
                    		glyph[i++] = (char)m0;
                    	}
                    }
                    String s = new String(glyph, 0, i);
                    b = s.getBytes(CJKFont.CJK_ENCODING);
                }
                catch (UnsupportedEncodingException e) {
                    throw new ExceptionConverter(e);
                }
                break;
            }
        }
        return b;
    }
1069806
public int readText(char []charBuffer, int charOffset, int charLength)
    throws IOException
  {
    byte []byteBuffer = _is.buffer();
    int byteOffset = _is.offset();
    int byteLength = _is.length();

    if (byteLength <= byteOffset || length() == 0) {
      if (! fillFrameBuffer()) {
        return -1;
      }

      byteOffset = _is.offset();
      byteLength = _is.length();
    }

    int charEnd = charOffset + charLength;
    int i = charOffset;

    int byteBegin = byteOffset;
    int byteEnd = (int) Math.min(byteLength, byteOffset + length());

    while (i < charEnd && byteOffset < byteEnd) {
      int d1 = byteBuffer[byteOffset++] & 0xff;

      char ch;

      if (d1 < 0x80) {
        ch = (char) d1;
      }
      else if ((d1 & 0xe0) == 0xc0) {
        int d2 = byteBuffer[byteOffset++] & 0xff;

        ch = (char) (((d1 & 0x1f) << 6) + (d2 & 0x3f));

        if (d2 < 0) {
          closeError(CloseCodes.NOT_CONSISTENT, "illegal utf-8");
          ch = UTF8_ERROR;
        }
        else if ((d2 & 0xc0) != 0x80) {
          closeError(CloseCodes.NOT_CONSISTENT, "illegal utf-8");
          ch = UTF8_ERROR;
        }
        else if (ch < 0x80) {
          closeError(CloseCodes.NOT_CONSISTENT, "illegal utf-8");
          ch = UTF8_ERROR;
        }
      }
      else if ((d1 & 0xf0) == 0xe0){
        int d2 = byteBuffer[byteOffset++] & 0xff;
        int d3 = byteBuffer[byteOffset++] & 0xff;

        ch = (char) (((d1 & 0x0f) << 12) + ((d2 & 0x3f) << 6) + (d3 & 0x3f));

        if (d3 < 0) {
          closeError(CloseCodes.NOT_CONSISTENT, "illegal utf-8");
          ch = UTF8_ERROR;
        }
        else if ((d2 & 0xc0) != 0x80) {
          closeError(CloseCodes.NOT_CONSISTENT, "illegal utf-8");
          ch = UTF8_ERROR;
        }
        else if ((d3 & 0xc0) != 0x80) {
          closeError(CloseCodes.NOT_CONSISTENT, "illegal utf-8");
          ch = UTF8_ERROR;
        }
        else if (ch < 0x800) {
          closeError(CloseCodes.NOT_CONSISTENT, "illegal utf-8");
          ch = UTF8_ERROR;
        }
        else if (0xd800 <= ch && ch <= 0xdfff) {
          closeError(CloseCodes.NOT_CONSISTENT, "illegal utf-8");
          ch = UTF8_ERROR;
        }
      }
      else if ((d1 & 0xf8) == 0xf0){
        int d2 = byteBuffer[byteOffset++] & 0xff;
        int d3 = byteBuffer[byteOffset++] & 0xff;
        int d4 = byteBuffer[byteOffset++] & 0xff;

        int cp = (((d1 & 0x7) << 18)
                   + ((d2 & 0x3f) << 12)
                   + ((d3 & 0x3f) << 6)
                   + ((d4 & 0x3f)));

        cp -= 0x10000;

        char h = (char) (0xd800 + ((cp >> 10) & 0x3ff));

        charBuffer[i++] = h;

        ch = (char) (0xdc00 + (cp & 0x3ff));

        if (d4 < 0) {
          closeError(CloseCodes.NOT_CONSISTENT, "illegal utf-8");
          ch = UTF8_ERROR;
        }
        else if ((d2 & 0xc0) != 0x80) {
          closeError(CloseCodes.NOT_CONSISTENT, "illegal utf-8");
          ch = UTF8_ERROR;
        }
        else if ((d3 & 0xc0) != 0x80) {
          closeError(CloseCodes.NOT_CONSISTENT, "illegal utf-8");
          ch = UTF8_ERROR;
        }
        else if ((d4 & 0xc0) != 0x80) {
          closeError(CloseCodes.NOT_CONSISTENT, "illegal utf-8");
          ch = UTF8_ERROR;
        }
        else if (cp < 0x0) {
          closeError(CloseCodes.NOT_CONSISTENT, "illegal utf-8");
          ch = UTF8_ERROR;
        }
        else if (cp >= 0x100000) {
          closeError(CloseCodes.NOT_CONSISTENT, "illegal utf-8");
          ch = UTF8_ERROR;
        }
      }
      else {
        closeError(CloseCodes.NOT_CONSISTENT, "illegal utf-8");

        ch = UTF8_ERROR;
      }

      charBuffer[i++] = ch;
    }

    _is.offset(byteOffset);
    _length -= (byteOffset - byteBegin);

    return i - charOffset;
  }
535747
protected void readFDSelect(int Font)
	{
		// Restore the number of glyphs
		int NumOfGlyphs = fonts[Font].nglyphs;
		int[] FDSelect = new int[NumOfGlyphs];
		// Go to the beginning of the FDSelect
		seek(fonts[Font].fdselectOffset);
		// Read the FDSelect's format
		fonts[Font].FDSelectFormat = getCard8();
		
		switch(fonts[Font].FDSelectFormat){
			// Format==0 means each glyph has an entry that indicated
			// its FD.
			case 0:
				for (int i=0;i<NumOfGlyphs;i++)
				{
					FDSelect[i] = getCard8();
				}
				// The FDSelect's Length is one for each glyph + the format
				// for later use
				fonts[Font].FDSelectLength = fonts[Font].nglyphs+1;
				break;
			case 3:
				// Format==3 means the ranges version
				// The number of ranges
				int nRanges = getCard16();
				int l=0;
				// Read the first in the first range
				int first = getCard16();
				for (int i=0;i<nRanges;i++)
				{
					// Read the FD index
					int fd = getCard8();
					// Read the first of the next range
					int last = getCard16();
					// Calc the steps and write to the array
					int steps = last-first;
					for (int k=0;k<steps;k++)
					{
						FDSelect[l] = fd;
						l++;
					}
					// The last from this iteration is the first of the next
					first = last;
				}
				// Store the length for later use
				fonts[Font].FDSelectLength = 1+2+nRanges*3+2;
				break;
			default:
				break;
		}
		// Save the FDSelect of the font 
		fonts[Font].FDSelect = FDSelect; 
	}
137437
private void guessVerticalMetricsFromGlyphBBox() {
        // Approximate capHeight from height of "H"
        // It's most unlikely that a font misses the PCLT table
        // This also assumes that postscriptnames exists ("H")
        // Should look it up int the cmap (that wouldn't help
        // for charsets without H anyway...)
        // Same for xHeight with the letter "x"
        int localCapHeight = 0;
        int localXHeight = 0;
        int localAscender = 0;
        int localDescender = 0;
        for (int i = 0; i < mtxTab.length; i++) {
            if ("H".equals(mtxTab[i].getName())) {
                localCapHeight = mtxTab[i].getBoundingBox()[3];
            } else if ("x".equals(mtxTab[i].getName())) {
                localXHeight = mtxTab[i].getBoundingBox()[3];
            } else if ("d".equals(mtxTab[i].getName())) {
                localAscender = mtxTab[i].getBoundingBox()[3];
            } else if ("p".equals(mtxTab[i].getName())) {
                localDescender = mtxTab[i].getBoundingBox()[1];
            } else {
                // OpenType Fonts with a version 3.0 "post" table don't have glyph names.
                // Use Unicode indices instead.
                List unicodeIndex = mtxTab[i].getUnicodeIndex();
                if (unicodeIndex.size() > 0) {
                    //Only the first index is used
                    char ch = (char)((Integer)unicodeIndex.get(0)).intValue();
                    if (ch == 'H') {
                        localCapHeight = mtxTab[i].getBoundingBox()[3];
                    } else if (ch == 'x') {
                        localXHeight = mtxTab[i].getBoundingBox()[3];
                    } else if (ch == 'd') {
                        localAscender = mtxTab[i].getBoundingBox()[3];
                    } else if (ch == 'p') {
                        localDescender = mtxTab[i].getBoundingBox()[1];
                    }
                }
            }
        }
        log.debug("Ascender from glyph 'd': " + localAscender 
                + " " + (localAscender));
        log.debug("Descender from glyph 'p': " + localDescender
                + " " + (localDescender));
        if (ascender - descender > upem) {
            log.debug("Replacing specified ascender/descender with derived values to get values"
                    + " which fit in the em box.");
            ascender = localAscender;
            descender = localDescender;
        }

        log.debug("xHeight from glyph 'x': " + localXHeight
                + " " + (localXHeight));
        log.debug("CapHeight from glyph 'H': " + localCapHeight
                + " " + (localCapHeight));
        if (capHeight == 0) {
            capHeight = localCapHeight;
            if (localCapHeight == 0) {
                log.warn("capHeight value could not be determined."
                        + " The font may not work as expected.");
            }
        }
        if (xHeight == 0) {
            xHeight = localXHeight;
            if (xHeight == 0) {
                log.warn("xHeight value could not be determined."
                        + " The font may not work as expected.");
            }
        }
    }
596188
public float getWidthPointKerned(String text, float fontSize) {
        float size = getWidth(text) * 0.001f * fontSize;
        if (!hasKernPairs())
            return size;
        int len = text.length() - 1;
        int kern = 0;
        char c[] = text.toCharArray();
        for (int k = 0; k < len; ++k) {
            kern += getKerning(c[k], c[k + 1]);
        }
        return size + kern * 0.001f * fontSize;
    }
199520
public float getWidthCorrected(float charSpacing, float wordSpacing) {
        float total = 0;
        for (int k = 0; k < line.size(); ++k) {
            PdfChunk ck = (PdfChunk)line.get(k);
            total += ck.getWidthCorrected(charSpacing, wordSpacing);
        }
        return total;
    }
325086
public int getSelectedFontSize() {
        int fontSize = 1;
        String fontSizeString = getFontSizeTextField().getText();
        while (true) {
            try {
                fontSize = Integer.parseInt(fontSizeString);
                break;
            } catch (NumberFormatException e) {
                fontSizeString = (String) getFontSizeList().getSelectedValue();
                getFontSizeTextField().setText(fontSizeString);
            }
        }

        return fontSize;
    }