/** * Encodes the given string for use in a PDF content stream. * * @param text Any Unicode text. * @return Array of PDF content stream bytes. * @throws IOException If the text could not be encoded. */ public final byte[] encode(String text) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); int offset = 0; while (offset < text.length()) { int codePoint = text.codePointAt(offset); // multi-byte encoding with 1 to 4 bytes byte[] bytes = encode(codePoint); out.write(bytes); offset += Character.charCount(codePoint); } return out.toByteArray(); }
/** * Returns the width of the given Unicode string. * * @param text The text to get the width of. * @return The width of the string in 1/1000 units of text space. * @throws IOException If there is an error getting the width information. */ public float getStringWidth(String text) throws IOException { byte[] bytes = encode(text); ByteArrayInputStream in = new ByteArrayInputStream(bytes); float width = 0; while (in.available() > 0) { int code = readCode(in); width += getWidth(code); } return width; }
encodedText = font.encode(text);
/** * Encodes the given string for use in a PDF content stream. * * @param text Any Unicode text. * @return Array of PDF content stream bytes. * @throws IOException If the text could not be encoded. */ public final byte[] encode(String text) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); int offset = 0; while (offset < text.length()) { int codePoint = text.codePointAt(offset); // multi-byte encoding with 1 to 4 bytes byte[] bytes = encode(codePoint); out.write(bytes); offset += Character.charCount(codePoint); } return out.toByteArray(); }
/** * Encodes the given string for use in a PDF content stream. * * @param text Any Unicode text. * @return Array of PDF content stream bytes. * @throws IOException If the text could not be encoded. */ public final byte[] encode(String text) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); for (int offset = 0; offset < text.length(); ) { int codePoint = text.codePointAt(offset); // multi-byte encoding with 1 to 4 bytes byte[] bytes = encode(codePoint); out.write(bytes); offset += Character.charCount(codePoint); } return out.toByteArray(); }
/** * Returns the width of the given Unicode string. * * @param text The text to get the width of. * @return The width of the string in 1/1000 units of text space. * @throws IOException If there is an error getting the width information. */ public float getStringWidth(String text) throws IOException { byte[] bytes = encode(text); ByteArrayInputStream in = new ByteArrayInputStream(bytes); float width = 0; while (in.available() > 0) { int code = readCode(in); width += getWidth(code); } return width; }
/** * Returns the width of the given Unicode string. * * @param text The text to get the width of. * @return The width of the string in 1/1000 units of text space. * @throws IOException If there is an error getting the width information. */ public float getStringWidth(String text) throws IOException { byte[] bytes = encode(text); ByteArrayInputStream in = new ByteArrayInputStream(bytes); float width = 0; while (in.available() > 0) { int code = readCode(in); width += getWidth(code); } return width; }
COSWriter.writeString(font.encode(text), output);
/** * Outputs a string using the correct encoding and subsetting as required. * * @param text The Unicode text to show. * * @throws IOException If an io exception occurs. */ protected void showTextInternal(String text) throws IOException { if (!inTextMode) { throw new IllegalStateException("Must call beginText() before showText()"); } if (fontStack.isEmpty()) { throw new IllegalStateException("Must call setFont() before showText()"); } PDFont font = fontStack.peek(); // Unicode code points to keep when subsetting if (font.willBeSubset()) { for (int offset = 0; offset < text.length(); ) { int codePoint = text.codePointAt(offset); font.addToSubset(codePoint); offset += Character.charCount(codePoint); } } COSWriter.writeString(font.encode(text), output); }
COSWriter.writeString(font.encode(text), getOutput());