/** * Font.java * Copyright (c) 2018, Innovatics Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and / or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.pdfjet; import java.io.InputStream; import java.util.Collections; import java.util.HashSet; import java.util.Map; /** * Used to create font objects. * The font objects must added to the PDF before they can be used to draw text. * */ public class Font { // Chinese (Traditional) font public static final String AdobeMingStd_Light = "AdobeMingStd-Light"; // Chinese (Simplified) font public static final String STHeitiSC_Light = "STHeitiSC-Light"; // Japanese font public static final String KozMinProVI_Regular = "KozMinProVI-Regular"; // Korean font public static final String AdobeMyungjoStd_Medium = "AdobeMyungjoStd-Medium"; public static final boolean STREAM = true; protected String name; protected String info; protected int objNumber; // The object number of the embedded font file protected int fileObjNumber = -1; // Font attributes protected int unitsPerEm = 1000; protected float size = 12.0f; protected float ascent; protected float descent; protected float capHeight; protected float body_height; // Font metrics protected int[][] metrics = null; // Don't change the following default values! protected boolean isCoreFont = false; protected boolean isCJK = false; protected int firstChar = 32; protected int lastChar = 255; protected boolean skew15 = false; protected boolean kernPairs = false; // Font bounding box protected float bBoxLLx; protected float bBoxLLy; protected float bBoxURx; protected float bBoxURy; protected float underlinePosition; protected float underlineThickness; protected int compressed_size; protected int uncompressed_size; protected int[] advanceWidth = null; protected int[] glyphWidth = null; protected int[] unicodeToGID; protected boolean cff; protected String fontID; private int fontDescriptorObjNumber = -1; private int cMapObjNumber = -1; private int cidFontDictObjNumber = -1; private int toUnicodeCMapObjNumber = -1; private int widthsArrayObjNumber = -1; private int encodingObjNumber = -1; private int codePage = CodePage.UNICODE; private int fontUnderlinePosition = 0; private int fontUnderlineThickness = 0; /** * Constructor for the 14 standard fonts. * Creates a font object and adds it to the PDF. * *
* Examples: * Font font1 = new Font(pdf, CoreFont.HELVETICA); * Font font2 = new Font(pdf, CoreFont.TIMES_ITALIC); * Font font3 = new Font(pdf, CoreFont.ZAPF_DINGBATS); * ... ** * @param pdf the PDF to add this font to. * @param coreFont the core font. Must be one the names defined in the CoreFont class. */ public Font(PDF pdf, CoreFont coreFont) throws Exception { this.isCoreFont = true; StandardFont font = StandardFont.getInstance(coreFont); this.name = font.name; this.bBoxLLx = font.bBoxLLx; this.bBoxLLy = font.bBoxLLy; this.bBoxURx = font.bBoxURx; this.bBoxURy = font.bBoxURy; this.metrics = font.metrics; this.ascent = bBoxURy * size / unitsPerEm; this.descent = bBoxLLy * size / unitsPerEm; this.body_height = ascent - descent; this.fontUnderlinePosition = font.underlinePosition; this.fontUnderlineThickness = font.underlineThickness; this.underlineThickness = fontUnderlineThickness * size / unitsPerEm; this.underlinePosition = fontUnderlinePosition * size / -unitsPerEm + underlineThickness / 2.0f; pdf.newobj(); pdf.append("<<\n"); pdf.append("/Type /Font\n"); pdf.append("/Subtype /Type1\n"); pdf.append("/BaseFont /"); pdf.append(this.name); pdf.append('\n'); if (!this.name.equals("Symbol") && !this.name.equals("ZapfDingbats")) { pdf.append("/Encoding /WinAnsiEncoding\n"); } pdf.append(">>\n"); pdf.endobj(); objNumber = pdf.objNumber; pdf.fonts.add(this); } // Used by PDFobj protected Font(CoreFont coreFont) { this.isCoreFont = true; StandardFont font = StandardFont.getInstance(coreFont); this.name = font.name; this.bBoxLLx = font.bBoxLLx; this.bBoxLLy = font.bBoxLLy; this.bBoxURx = font.bBoxURx; this.bBoxURy = font.bBoxURy; this.metrics = font.metrics; this.ascent = bBoxURy * size / unitsPerEm; this.descent = bBoxLLy * size / unitsPerEm; this.body_height = ascent - descent; this.fontUnderlinePosition = font.underlinePosition; this.fontUnderlineThickness = font.underlineThickness; this.underlineThickness = fontUnderlineThickness * size / unitsPerEm; this.underlinePosition = fontUnderlinePosition * size / -unitsPerEm + underlineThickness / 2.0f; } public Font(PDF pdf, String fontName) throws Exception { this(pdf, fontName, CodePage.UNICODE); } /** * Constructor for CJK - Chinese, Japanese and Korean fonts. * Please see Example_04. * * @param pdf the PDF to add this font to. * @param fontName the font name. Please see Example_04. * @param codePage the code page. Must be: CodePage.UNICODE */ public Font(PDF pdf, String fontName, int codePage) throws Exception { this.name = fontName; isCJK = true; firstChar = 0x0020; lastChar = 0xFFEE; // Font Descriptor pdf.newobj(); pdf.append("<<\n"); pdf.append("/Type /FontDescriptor\n"); pdf.append("/FontName /"); pdf.append(fontName); pdf.append('\n'); pdf.append("/Flags 4\n"); pdf.append("/FontBBox [0 0 0 0]\n"); pdf.append(">>\n"); pdf.endobj(); // CIDFont Dictionary pdf.newobj(); pdf.append("<<\n"); pdf.append("/Type /Font\n"); pdf.append("/Subtype /CIDFontType0\n"); pdf.append("/BaseFont /"); pdf.append(fontName); pdf.append('\n'); pdf.append("/FontDescriptor "); pdf.append(pdf.objNumber - 1); pdf.append(" 0 R\n"); pdf.append("/CIDSystemInfo <<\n"); pdf.append("/Registry (Adobe)\n"); if (fontName.startsWith("AdobeMingStd")) { pdf.append("/Ordering (CNS1)\n"); pdf.append("/Supplement 4\n"); } else if (fontName.startsWith("AdobeSongStd") || fontName.startsWith("STHeitiSC")) { pdf.append("/Ordering (GB1)\n"); pdf.append("/Supplement 4\n"); } else if (fontName.startsWith("KozMinPro")) { pdf.append("/Ordering (Japan1)\n"); pdf.append("/Supplement 4\n"); } else if (fontName.startsWith("AdobeMyungjoStd")) { pdf.append("/Ordering (Korea1)\n"); pdf.append("/Supplement 1\n"); } else { throw new Exception("Unsupported font: " + fontName); } pdf.append(">>\n"); pdf.append(">>\n"); pdf.endobj(); // Type0 Font Dictionary pdf.newobj(); pdf.append("<<\n"); pdf.append("/Type /Font\n"); pdf.append("/Subtype /Type0\n"); pdf.append("/BaseFont /"); if (fontName.startsWith("AdobeMingStd")) { pdf.append(fontName + "-UniCNS-UTF16-H\n"); pdf.append("/Encoding /UniCNS-UTF16-H\n"); } else if (fontName.startsWith("AdobeSongStd") || fontName.startsWith("STHeitiSC")) { pdf.append(fontName + "-UniGB-UTF16-H\n"); pdf.append("/Encoding /UniGB-UTF16-H\n"); } else if (fontName.startsWith("KozMinPro")) { pdf.append(fontName + "-UniJIS-UCS2-H\n"); pdf.append("/Encoding /UniJIS-UCS2-H\n"); } else if (fontName.startsWith("AdobeMyungjoStd")) { pdf.append(fontName + "-UniKS-UCS2-H\n"); pdf.append("/Encoding /UniKS-UCS2-H\n"); } else { throw new Exception("Unsupported font: " + fontName); } pdf.append("/DescendantFonts ["); pdf.append(pdf.objNumber - 1); pdf.append(" 0 R]\n"); pdf.append(">>\n"); pdf.endobj(); objNumber = pdf.objNumber; ascent = size; descent = -ascent/4; body_height = ascent - descent; pdf.fonts.add(this); } // Constructor for .ttf.stream fonts: public Font(PDF pdf, InputStream inputStream, boolean flag) throws Exception { FastFont.register(pdf, this, inputStream); this.ascent = bBoxURy * size / unitsPerEm; this.descent = bBoxLLy * size / unitsPerEm; this.body_height = ascent - descent; this.underlineThickness = fontUnderlineThickness * size / unitsPerEm; this.underlinePosition = fontUnderlinePosition * size / -unitsPerEm + underlineThickness / 2f; pdf.fonts.add(this); } // Constructor for .ttf.stream fonts: public Font(Map