protected BaseFont getRealFont() throws IOException, DocumentException { if (font == null) return BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false); else return font; }
private float calculateFontSize(float w, float h) throws IOException, DocumentException { BaseFont ufont = getRealFont(); float fsize = fontSize; if (fsize == 0) { float bw = ufont.getWidthPoint(text, 1); if (bw == 0) fsize = 12; else fsize = w / bw; float nfsize = h / (1 - ufont.getFontDescriptor(BaseFont.DESCENT, 1)); fsize = Math.min(fsize, nfsize); if (fsize < 4) fsize = 4; } return fsize; }
@Override public boolean charExists(int c) { if (cjkMirror != null) return cjkMirror.charExists(c); else if (isType0) { return metrics.containsKey(Integer.valueOf(c)); } else return super.charExists(c); }
private BaseFont getIdentityFont(String fontFamily,String fontPath,ApplicationContext applicationContext) throws DocumentException,IOException { if(!fontPath.startsWith(ApplicationContext.CLASSPATH_URL_PREFIX)){ fontPath=ApplicationContext.CLASSPATH_URL_PREFIX+fontPath; } String fontName = fontPath; int lastSlashPos=fontPath.lastIndexOf("/"); if(lastSlashPos!=-1){ fontName = fontPath.substring(lastSlashPos+1,fontPath.length()); } if (fontName.toLowerCase().endsWith(".ttc")) { fontName = fontName + ",0"; } InputStream inputStream=null; try{ fontPathMap.put(fontFamily, fontPath); inputStream=applicationContext.getResource(fontPath).getInputStream(); byte[] bytes = IOUtils.toByteArray(inputStream); BaseFont baseFont = BaseFont.createFont(fontName, BaseFont.IDENTITY_H,BaseFont.EMBEDDED,true,bytes,null); baseFont.setSubset(true); return baseFont; }finally{ if(inputStream!=null)inputStream.close(); } } }
private float getTextHeight(BaseFont baseFont, float fontSize, String text) { float ascend = baseFont.getAscentPoint(text, fontSize); float descend = baseFont.getDescentPoint(text, fontSize); return ascend + descend; }
final String FONT = "c:/windows/... .ttf"; BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); document.add(new Paragraph("Font: " + bf.getPostscriptFontName() + " with encoding: " + bf.getEncoding()));
try { if (path.toLowerCase().endsWith(".ttf") || path.toLowerCase().endsWith(".otf") || path.toLowerCase().indexOf(".ttc,") > 0) { Object allNames[] = BaseFont.getAllFontNames(path, BaseFont.WINANSI, null); trueTypeFonts.put(((String)allNames[0]).toLowerCase(), path); if (alias != null) { if (alias != null) LOGGER.error("You can't define an alias for a true type collection."); String[] names = BaseFont.enumerateTTCNames(path); for (int i = 0; i < names.length; i++) { register(path + "," + i); BaseFont bf = BaseFont.createFont(path, BaseFont.CP1252, false); String fullName = bf.getFullFontName()[0][3].toLowerCase(); String familyName = bf.getFamilyFontName()[0][3].toLowerCase(); String psName = bf.getPostscriptFontName().toLowerCase(); registerFamily(familyName, fullName, null); trueTypeFonts.put(psName, path);
/** Gets all the names from the font. Only the required tables are read. * @param name the name of the font * @param encoding the encoding of the font * @param ttfAfm the true type font or the afm in a byte array * @throws DocumentException on error * @throws IOException on error * @return an array of Object[] built with {getPostscriptFontName(), getFamilyFontName(), getFullFontName()} */ public static Object[] getAllFontNames(String name, String encoding, byte ttfAfm[]) throws DocumentException, IOException { String nameBase = getBaseName(name); BaseFont fontBuilt = null; if (nameBase.toLowerCase().endsWith(".ttf") || nameBase.toLowerCase().endsWith(".otf") || nameBase.toLowerCase().indexOf(".ttc,") > 0) fontBuilt = new TrueTypeFont(name, CP1252, false, ttfAfm, true, false); else fontBuilt = createFont(name, encoding, false, false, ttfAfm, null); return new Object[]{fontBuilt.getPostscriptFontName(), fontBuilt.getFamilyFontName(), fontBuilt.getFullFontName()}; }
try helv = BaseFont.createFont("Helvetica", BaseFont.WINANSI, false); float textSize = helv.getWidthPoint(text, 12); float textBase = document.bottom() - 20; cb.beginText(); cb.setFontAndSize(helv, 12); float adjust = helv.getWidthPoint("0", 12); cb.setTextMatrix(document.right() - textSize - adjust, textBase); cb.showText(text);
/** * Returns the width of a certain character of this font. * * @param character a certain character * @return a width in Text Space */ float width(int character) { return font.getWidthPoint(character, size) * hScale; }
/** Gets the full name of the font. If it is a True Type font * each array element will have {Platform ID, Platform Encoding ID, * Language ID, font name}. The interpretation of this values can be * found in the Open Type specification, chapter 2, in the 'name' table.<br> * For the other fonts the array has a single element with {"", "", "", * font name}. * @param name the name of the font * @param encoding the encoding of the font * @param ttfAfm the true type font or the afm in a byte array * @throws DocumentException on error * @throws IOException on error * @return the full name of the font */ public static String[][] getFullFontName(String name, String encoding, byte ttfAfm[]) throws DocumentException, IOException { String nameBase = getBaseName(name); BaseFont fontBuilt = null; if (nameBase.toLowerCase().endsWith(".ttf") || nameBase.toLowerCase().endsWith(".otf") || nameBase.toLowerCase().indexOf(".ttc,") > 0) fontBuilt = new TrueTypeFont(name, CP1252, false, ttfAfm, true, false); else fontBuilt = createFont(name, encoding, false, false, ttfAfm, null); return fontBuilt.getFullFontName(); }
/** Gets all the entries of the namestable from the font. Only the required tables are read. * @param name the name of the font * @param encoding the encoding of the font * @param ttfAfm the true type font or the afm in a byte array * @throws DocumentException on error * @throws IOException on error * @return an array of Object[] built with {getPostscriptFontName(), getFamilyFontName(), getFullFontName()} * @since 2.0.8 */ public static String[][] getAllNameEntries(String name, String encoding, byte ttfAfm[]) throws DocumentException, IOException { String nameBase = getBaseName(name); BaseFont fontBuilt = null; if (nameBase.toLowerCase().endsWith(".ttf") || nameBase.toLowerCase().endsWith(".otf") || nameBase.toLowerCase().indexOf(".ttc,") > 0) fontBuilt = new TrueTypeFont(name, CP1252, false, ttfAfm, true, false); else fontBuilt = createFont(name, encoding, false, false, ttfAfm, null); return fontBuilt.getAllNameEntries(); }
w = bf.getWidthPointKerned(text, state.size); else w = bf.getWidthPoint(text, state.size); if (state.wordSpace != 0.0f && !bf.isVertical()) { for (int i = 0; i < text.length() -1; i++) { if (text.charAt(i) == ' ')
float fsize = fontSize; if (fsize == 0) { float bw = ufont.getWidthPoint(text, 1); if (bw == 0) fsize = 12; else fsize = wt / bw; float nfsize = h / (ufont.getFontDescriptor(BaseFont.ASCENT, 1)); fsize = Math.min(fsize, nfsize); app.beginText(); app.setFontAndSize(ufont, fsize); app.setTextMatrix((box.getWidth() - ufont.getWidthPoint(text, fsize)) / 2, (box.getHeight() - ufont.getAscentPoint(text, fsize)) / 2); app.showText(text); app.endText();
float angle = baseFont.getFontDescriptor(BaseFont.ITALICANGLE, 1000); float angle2 = font.getItalicAngle(); if (baseFont.getPostscriptFontName().toLowerCase().indexOf("bold") < 0) { float adv = ((float)width - baseFont.getWidthPoint(s, fontSize)) / (s.length() - 1); cb.setCharacterSpacing(adv);
if (dao[DA_FONT] != null) { BaseFont bf = (BaseFont)value; PdfName psn = PdfAppearance.stdFieldFontNames.get(bf.getPostscriptFontName()); if (psn == null) { psn = new PdfName(bf.getPostscriptFontName()); if (bf.getFontType() == BaseFont.FONT_TYPE_DOCUMENT) { fd = new FontDetails(null, ((DocumentFont)bf).getIndirectReference(), bf); bf.setSubset(false); fd = writer.addSimple(bf); localFonts.put(psn.toString().substring(1), bf);
@Override protected void checkFont(PdfWriter writer, int key, Object obj1) { BaseFont bf = (BaseFont) obj1; if (bf.getFontType() == BaseFont.FONT_TYPE_DOCUMENT) { PdfStream prs = null; PdfDictionary fontDictionary = ((DocumentFont) bf).getFontDictionary(); PdfDictionary fontDescriptor = getDirectDictionary(fontDictionary.get(PdfName.FONTDESCRIPTOR)); if (fontDescriptor != null) { prs = getDirectStream(fontDescriptor.get(PdfName.FONTFILE)); if (prs == null) { prs = getDirectStream(fontDescriptor.get(PdfName.FONTFILE2)); } if (prs == null) { prs = getDirectStream(fontDescriptor.get(PdfName.FONTFILE3)); } } if (prs == null) { throw new PdfAConformanceException(obj1, MessageLocalization.getComposedMessage("all.the.fonts.must.be.embedded.this.one.isn.t.1", ((BaseFont) obj1).getPostscriptFontName())); } } else { if (!bf.isEmbedded()) throw new PdfAConformanceException(obj1, MessageLocalization.getComposedMessage("all.the.fonts.must.be.embedded.this.one.isn.t.1", ((BaseFont) obj1).getPostscriptFontName())); } }
/** * Set the font and the size for the subsequent text writing. * * @param bf the font * @param size the font size in points */ @Override public void setFontAndSize(BaseFont bf, float size) { checkWriter(); state.size = size; if (bf.getFontType() == BaseFont.FONT_TYPE_DOCUMENT) { state.fontDetails = new FontDetails(null, ((DocumentFont)bf).getIndirectReference(), bf); } else state.fontDetails = writer.addSimple(bf); PdfName psn = stdFieldFontNames.get(bf.getPostscriptFontName()); if (psn == null) { if (bf.isSubset() && bf.getFontType() == BaseFont.FONT_TYPE_TTUNI) psn = state.fontDetails.getFontName(); else { psn = new PdfName(bf.getPostscriptFontName()); state.fontDetails.setSubset(false); } } PageResources prs = getPageResources(); // PdfName name = state.fontDetails.getFontName(); prs.addFont(psn, state.fontDetails.getIndirectReference()); content.append(psn.getBytes()).append(' ').append(size).append(" Tf").append_i(separator); }
break; case PdfIsoKeys.PDFISOKEY_FONT: if (!((BaseFont)obj1).isEmbedded()) throw new PdfXConformanceException(MessageLocalization.getComposedMessage("all.the.fonts.must.be.embedded.this.one.isn.t.1", ((BaseFont)obj1).getPostscriptFontName())); break; case PdfIsoKeys.PDFISOKEY_IMAGE:
BaseFont fonty = BaseFont.createFont(url, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); fonty.setSubset(true);