Java Code Examples for com.lowagie.text.Font#getBaseFont()

The following examples show how to use com.lowagie.text.Font#getBaseFont() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RtfDestinationFontTable.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Create a font via the <code>FontFactory</code>
 * 
 * @param fontName The font name to create
 * @return The created <code>Font</code> object
 * 
 * @since 2.0.8
 */
private Font createfont(String fontName) {
	Font f1 = null;
	int pos=-1;
	do {
		f1 = FontFactory.getFont(fontName);
		
		if(f1.getBaseFont() != null) break;	// found a font, exit the do/while
		
		pos = fontName.lastIndexOf(' ');	// find the last space
		if(pos>0) {
			fontName = fontName.substring(0, pos );	// truncate it to the last space
		}
	} while(pos>0);
	return f1;
}
 
Example 2
Source File: FontSelector.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Adds a <CODE>Font</CODE> to be searched for valid characters.
 * 
 * @param font the <CODE>Font</CODE>
 */
public void addFont(Font font) {
	if (font.getBaseFont() != null) {
		fonts.add(font);
		return;
	}
	BaseFont bf = font.getCalculatedBaseFont(true);
	Font f2 = new Font(bf, font.getSize(), font.getCalculatedStyle(), font.getColor());
	fonts.add(f2);
}
 
Example 3
Source File: FontSelector.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Adds a <CODE>Font</CODE> to be searched for valid characters.
 * @param font the <CODE>Font</CODE>
 */    
public void addFont(Font font) {
    if (font.getBaseFont() != null) {
        fonts.add(font);
        return;
    }
    BaseFont bf = font.getCalculatedBaseFont(true);
    Font f2 = new Font(bf, font.getSize(), font.getCalculatedStyle(), font.getColor());
    fonts.add(f2);
}
 
Example 4
Source File: RtfFont.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Constructs a RtfFont from a com.lowagie.text.Font
 * @param doc The RtfDocument this font appears in
 * @param font The Font to use as a base
 */
public RtfFont(RtfDocument doc, Font font) {
    this.document = doc;
    if(font != null) {
        if(font instanceof RtfFont) {
            this.fontName = ((RtfFont) font).getFontName();
            this.charset = ((RtfFont) font).getCharset();
        } else {
            setToDefaultFamily(font.getFamilyname());
        }
        if(font.getBaseFont() != null) {
            String[][] fontNames = font.getBaseFont().getFullFontName();
            for(int i = 0; i < fontNames.length; i++) {
                if(fontNames[i][2].equals("0")) {
                    this.fontName = fontNames[i][3];
                    break;
                } else if(fontNames[i][2].equals("1033") || fontNames[i][2].equals("")) {
                    this.fontName = fontNames[i][3];
                }
            }
        }

        if(this.fontName.equalsIgnoreCase("unknown")) {
            this.fontName = DEFAULT_FONT;
        }
        
        setSize(font.getSize());
        setStyle(font.getStyle());
        setColor(font.getColor());
        if(document != null) {
        	this.fontNumber = document.getDocumentHeader().getFontNumber(this);
        }
    }

    if(document != null) {
        setRtfDocument(document);
    }
}
 
Example 5
Source File: PdfTimetableGridTable.java    From unitime with Apache License 2.0 5 votes vote down vote up
public void addTextVertical(PdfPCell cell, String text, boolean bold) throws Exception  {
	if (text==null) return;
       if (text.indexOf("<span")>=0)
           text = text.replaceAll("</span>","").replaceAll("<span .*>", "");
       Font font = PdfFont.getFont(bold);
	BaseFont bf = font.getBaseFont();
	float width = bf.getWidthPoint(text, font.getSize());
	PdfTemplate template = iWriter.getDirectContent().createTemplate(2 * font.getSize() + 4, width);
	template.beginText();
	template.setColorFill(Color.BLACK);
	template.setFontAndSize(bf, font.getSize());
	template.setTextMatrix(0, 2);
	template.showText(text);
	template.endText();
	template.setWidth(width);
	template.setHeight(font.getSize() + 2);
	//make an Image object from the template
	Image img = Image.getInstance(template);
	img.setRotationDegrees(270);
	//embed the image in a Chunk
	Chunk ck = new Chunk(img, 0, 0);
	
	if (cell.getPhrase()==null) {
		cell.setPhrase(new Paragraph(ck));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
	} else {
		cell.getPhrase().add(ck);
	}
}
 
Example 6
Source File: MetaFont.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
public BaseFont getFont() {
    if (font != null)
        return font;
    Font ff2 = FontFactory.getFont(faceName, BaseFont.CP1252, true, 10, ((italic != 0) ? Font.ITALIC : 0) | ((bold != 0) ? Font.BOLD : 0));
    font = ff2.getBaseFont();
    if (font != null)
        return font;
    String fontName;
    if (faceName.indexOf("courier") != -1 || faceName.indexOf("terminal") != -1
        || faceName.indexOf("fixedsys") != -1) {
        fontName = fontNames[MARKER_COURIER + italic + bold];
    }
    else if (faceName.indexOf("ms sans serif") != -1 || faceName.indexOf("arial") != -1
        || faceName.indexOf("system") != -1) {
        fontName = fontNames[MARKER_HELVETICA + italic + bold];
    }
    else if (faceName.indexOf("arial black") != -1) {
        fontName = fontNames[MARKER_HELVETICA + italic + MARKER_BOLD];
    }
    else if (faceName.indexOf("times") != -1 || faceName.indexOf("ms serif") != -1
        || faceName.indexOf("roman") != -1) {
        fontName = fontNames[MARKER_TIMES + italic + bold];
    }
    else if (faceName.indexOf("symbol") != -1) {
        fontName = fontNames[MARKER_SYMBOL];
    }
    else {
        int pitch = pitchAndFamily & 3;
        int family = (pitchAndFamily >> 4) & 7;
        switch (family) {
            case FF_MODERN:
                fontName = fontNames[MARKER_COURIER + italic + bold];
                break;
            case FF_ROMAN:
                fontName = fontNames[MARKER_TIMES + italic + bold];
                break;
            case FF_SWISS:
            case FF_SCRIPT:
            case FF_DECORATIVE:
                fontName = fontNames[MARKER_HELVETICA + italic + bold];
                break;
            default:
            {
                switch (pitch) {
                    case FIXED_PITCH:
                        fontName = fontNames[MARKER_COURIER + italic + bold];
                        break;
                    default:
                        fontName = fontNames[MARKER_HELVETICA + italic + bold];
                        break;
                }
            }
        }
    }
    try {
        font = BaseFont.createFont(fontName, "Cp1252", false);
    }
    catch (Exception e) {
        throw new ExceptionConverter(e);
    }
    
    return font;
}
 
Example 7
Source File: HtmlWriter.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Writes the representation of a <CODE>Font</CODE>.
 *
 * @param font a <CODE>Font</CODE>
 * @param styleAttributes the style of the font
 * @throws IOException
 */

protected void write(Font font, Properties styleAttributes) throws IOException {
	if (font == null || !isOtherFont(font) /* || styleAttributes == null*/) {
		return;
	}
	write(" ");
	write(HtmlTags.STYLE);
	write("=\"");
	if (styleAttributes != null) {
		String key;
		for (Enumeration e = styleAttributes.propertyNames(); e.hasMoreElements();) {
			key = (String) e.nextElement();
			writeCssProperty(key, styleAttributes.getProperty(key));
		}
	}
	if (isOtherFont(font)) {
		writeCssProperty(Markup.CSS_KEY_FONTFAMILY, font.getFamilyname());

		if (font.getSize() != Font.UNDEFINED) {
			writeCssProperty(Markup.CSS_KEY_FONTSIZE, font.getSize() + "pt");
		}
		if (font.getColor() != null) {
			writeCssProperty(Markup.CSS_KEY_COLOR, HtmlEncoder.encode(font.getColor()));
		}

		int fontstyle = font.getStyle();
		BaseFont bf = font.getBaseFont();
		if (bf != null) {
			String ps = bf.getPostscriptFontName().toLowerCase();
			if (ps.indexOf("bold") >= 0) {
				if (fontstyle == Font.UNDEFINED) {
					fontstyle = 0;
				}
				fontstyle |= Font.BOLD;
			}
			if (ps.indexOf("italic") >= 0 || ps.indexOf("oblique") >= 0) {
				if (fontstyle == Font.UNDEFINED) {
					fontstyle = 0;
				}
				fontstyle |= Font.ITALIC;
			}
		}
		if (fontstyle != Font.UNDEFINED && fontstyle != Font.NORMAL) {
			switch (fontstyle & Font.BOLDITALIC) {
				case Font.BOLD:
					writeCssProperty(Markup.CSS_KEY_FONTWEIGHT, Markup.CSS_VALUE_BOLD);
					break;
				case Font.ITALIC:
					writeCssProperty(Markup.CSS_KEY_FONTSTYLE, Markup.CSS_VALUE_ITALIC);
					break;
				case Font.BOLDITALIC:
					writeCssProperty(Markup.CSS_KEY_FONTWEIGHT, Markup.CSS_VALUE_BOLD);
					writeCssProperty(Markup.CSS_KEY_FONTSTYLE, Markup.CSS_VALUE_ITALIC);
					break;
			}

			// CSS only supports one decoration tag so if both are specified
			// only one of the two will display
			if ((fontstyle & Font.UNDERLINE) > 0) {
				writeCssProperty(Markup.CSS_KEY_TEXTDECORATION, Markup.CSS_VALUE_UNDERLINE);
			}
			if ((fontstyle & Font.STRIKETHRU) > 0) {
				writeCssProperty(Markup.CSS_KEY_TEXTDECORATION, Markup.CSS_VALUE_LINETHROUGH);
			}
		}
	}
	write("\"");
}
 
Example 8
Source File: MetaFont.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
public BaseFont getFont() {
    if (font != null)
        return font;
    Font ff2 = FontFactory.getFont(faceName, BaseFont.CP1252, true, 10, ((italic != 0) ? Font.ITALIC : 0) | ((bold != 0) ? Font.BOLD : 0));
    font = ff2.getBaseFont();
    if (font != null)
        return font;
    String fontName;
    if (faceName.indexOf("courier") != -1 || faceName.indexOf("terminal") != -1
        || faceName.indexOf("fixedsys") != -1) {
        fontName = fontNames[MARKER_COURIER + italic + bold];
    }
    else if (faceName.indexOf("ms sans serif") != -1 || faceName.indexOf("arial") != -1
        || faceName.indexOf("system") != -1) {
        fontName = fontNames[MARKER_HELVETICA + italic + bold];
    }
    else if (faceName.indexOf("arial black") != -1) {
        fontName = fontNames[MARKER_HELVETICA + italic + MARKER_BOLD];
    }
    else if (faceName.indexOf("times") != -1 || faceName.indexOf("ms serif") != -1
        || faceName.indexOf("roman") != -1) {
        fontName = fontNames[MARKER_TIMES + italic + bold];
    }
    else if (faceName.indexOf("symbol") != -1) {
        fontName = fontNames[MARKER_SYMBOL];
    }
    else {
        int pitch = pitchAndFamily & 3;
        int family = (pitchAndFamily >> 4) & 7;
        switch (family) {
            case FF_MODERN:
                fontName = fontNames[MARKER_COURIER + italic + bold];
                break;
            case FF_ROMAN:
                fontName = fontNames[MARKER_TIMES + italic + bold];
                break;
            case FF_SWISS:
            case FF_SCRIPT:
            case FF_DECORATIVE:
                fontName = fontNames[MARKER_HELVETICA + italic + bold];
                break;
            default:
            {
                switch (pitch) {
                    case FIXED_PITCH:
                        fontName = fontNames[MARKER_COURIER + italic + bold];
                        break;
                    default:
                        fontName = fontNames[MARKER_HELVETICA + italic + bold];
                        break;
                }
            }
        }
    }
    try {
        font = BaseFont.createFont(fontName, "Cp1252", false);
    }
    catch (Exception e) {
        throw new ExceptionConverter(e);
    }
    
    return font;
}
 
Example 9
Source File: RtfDestinationFontTable.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Process the font information that was parsed from the input.
 * 
 * @since 2.0.8
 */
private void processFont() {
	this.fontName = this.fontName.trim();
	if(fontName.length() == 0) return;
	if(fontNr.length() == 0) return;
	
	if(fontName.length()>0 && fontName.indexOf(';') >= 0) {
		fontName = fontName.substring(0,fontName.indexOf(';'));
	}

	if(this.rtfParser.isImport()) {
		//TODO: If primary font fails, use the alternate
			//TODO: Problem: RtfFont defaults family to \froman and doesn't allow any other family.
			// if you set the family, it changes the font name and not the family in the Font.java class.
			
//			if(this.fontFamily.length() > 0) {
//				if(this.importHeader.importFont(this.fontNr, this.fontName, this.fontFamily, Integer.parseInt(this.charset)) == false) {
//					if(this.falt.length() > 0) {
//						this.importHeader.importFont(this.fontNr, this.falt, this.fontFamily, Integer.parseInt(this.charset));
//					}
//				}
//			} else {
				if(!this.importHeader.importFont(this.fontNr, this.fontName, Integer.parseInt("".equals(this.charset)?CHARSET_DEFAULT:this.charset))) {
					if(this.falt.length() > 0) {
						this.importHeader.importFont(this.fontNr, this.falt, Integer.parseInt("".equals(this.charset)?CHARSET_DEFAULT:this.charset));
					}
				}
//			}
		}
	if(this.rtfParser.isConvert()) {
		// This could probably be written as a better font matching function
		
		String fName = this.fontName;	// work variable for trimming name if needed.
		Font f1 = createfont(fName);
		if(f1.getBaseFont() == null && this.falt.length()>0)
			f1 = createfont(this.falt);
		
		if(f1.getBaseFont() == null) {
			// Did not find a font, let's try a substring of the first name.
			if(FontFactory.COURIER.indexOf(fName) > -1 ) {
				f1 = FontFactory.getFont(FontFactory.COURIER);
			} else if(FontFactory.HELVETICA.indexOf(fName) > -1 ) {
				f1 = FontFactory.getFont(FontFactory.HELVETICA);
			} else if(FontFactory.TIMES.indexOf(fName) > -1 ) {
				f1 = FontFactory.getFont(FontFactory.TIMES);
			} else if(FontFactory.SYMBOL.indexOf(fName) > -1 ) {
				f1 = FontFactory.getFont(FontFactory.SYMBOL);
			} else if(FontFactory.ZAPFDINGBATS.indexOf(fName) > -1 ) {
				f1 = FontFactory.getFont(FontFactory.ZAPFDINGBATS);
			} else {
				// we did not find a matching font in any form.
				// default to HELVETICA for now.
				f1 = FontFactory.getFont(FontFactory.HELVETICA);
			}
		}
		fontMap.put(this.fontNr, f1);
		//System.out.println(f1.getFamilyname());
	}
	this.setToDefaults();
}
 
Example 10
Source File: HtmlWriter.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Writes the representation of a <CODE>Font</CODE>.
 *
 * @param font              a <CODE>Font</CODE>
 * @param styleAttributes   the style of the font
 * @throws IOException
 */

protected void write(Font font, Properties styleAttributes) throws IOException {
    if (font == null || !isOtherFont(font) /* || styleAttributes == null*/) return;
    write(" ");
    write(HtmlTags.STYLE);
    write("=\"");
    if (styleAttributes != null) {
        String key;
        for (Enumeration e = styleAttributes.propertyNames(); e.hasMoreElements(); ) {
            key = (String)e.nextElement();
            writeCssProperty(key, styleAttributes.getProperty(key));
        }
    }
    if (isOtherFont(font)) {
        writeCssProperty(Markup.CSS_KEY_FONTFAMILY, font.getFamilyname());
        
        if (font.getSize() != Font.UNDEFINED) {
            writeCssProperty(Markup.CSS_KEY_FONTSIZE, font.getSize() + "pt");
        }
        if (font.getColor() != null) {
            writeCssProperty(Markup.CSS_KEY_COLOR, HtmlEncoder.encode(font.getColor()));
        }
        
        int fontstyle = font.getStyle();
        BaseFont bf = font.getBaseFont();
        if (bf != null) {
            String ps = bf.getPostscriptFontName().toLowerCase();
            if (ps.indexOf("bold") >= 0) {
                if (fontstyle == Font.UNDEFINED)
                    fontstyle = 0;
                fontstyle |= Font.BOLD;
            }
            if (ps.indexOf("italic") >= 0 || ps.indexOf("oblique") >= 0) {
                if (fontstyle == Font.UNDEFINED)
                    fontstyle = 0;
                fontstyle |= Font.ITALIC;
            }
        }
        if (fontstyle != Font.UNDEFINED && fontstyle != Font.NORMAL) {
            switch (fontstyle & Font.BOLDITALIC) {
                case Font.BOLD:
                    writeCssProperty(Markup.CSS_KEY_FONTWEIGHT, Markup.CSS_VALUE_BOLD);
                    break;
                case Font.ITALIC:
                    writeCssProperty(Markup.CSS_KEY_FONTSTYLE, Markup.CSS_VALUE_ITALIC);
                    break;
                case Font.BOLDITALIC:
                    writeCssProperty(Markup.CSS_KEY_FONTWEIGHT, Markup.CSS_VALUE_BOLD);
                    writeCssProperty(Markup.CSS_KEY_FONTSTYLE, Markup.CSS_VALUE_ITALIC);
                    break;
            }
            
            // CSS only supports one decoration tag so if both are specified
            // only one of the two will display
            if ((fontstyle & Font.UNDERLINE) > 0) {
                writeCssProperty(Markup.CSS_KEY_TEXTDECORATION, Markup.CSS_VALUE_UNDERLINE);
            }
            if ((fontstyle & Font.STRIKETHRU) > 0) {
                writeCssProperty(Markup.CSS_KEY_TEXTDECORATION, Markup.CSS_VALUE_LINETHROUGH);
            }
        }
    }
    write("\"");
}
 
Example 11
Source File: JRPdfExporter.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected boolean canUseGlyphRendering(FontKey fontKey) 
{
	Map<Attribute, Object> fontAttributes = new HashMap<Attribute, Object>();
	fontKey.fontAttribute.putAttributes(fontAttributes);
	fontAttributes.put(TextAttribute.SIZE, 10f);

	int style = 0;
	if (fontKey.italic)
	{
		style |= java.awt.Font.ITALIC;
		fontAttributes.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
	}
	if (fontKey.bold)
	{
		style |= java.awt.Font.BOLD;
		fontAttributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
	}
	
	Font pdfFont = getFont(fontAttributes, fontKey.locale, false);
	BaseFont baseFont = pdfFont.getBaseFont();
	if (baseFont.getFontType() != BaseFont.FONT_TYPE_TTUNI
			|| baseFont.isFontSpecific())
	{
		if (log.isDebugEnabled())
		{
			log.debug("pdf font for " + fontKey + " has type " + baseFont.getFontType()
					+ ", symbol " + baseFont.isFontSpecific()
					+ ", cannot use glyph rendering");
		}
		return false;
	}
	
	java.awt.Font awtFont = fontUtil.getAwtFontFromBundles(fontKey.fontAttribute, style,
			10f, fontKey.locale, awtIgnoreMissingFont);
	if (awtFont == null)
	{
		awtFont = new java.awt.Font(fontAttributes);
	}
	String awtFontName = awtFont.getFontName();
	
	if (log.isDebugEnabled())
	{
		log.debug(fontKey + " resolved to awt font " + awtFontName);
	}
	
	// we need the fonts to be identical.
	// it would be safer to only allow fonts from extensions, 
	// but for now we are just checking the font names.
	// we need to compare full names because we can't get the base name from awt.
	String[][] pdfFontNames = baseFont.getFullFontName();
	boolean nameMatch = false;
	for (String[] nameArray : pdfFontNames)
	{
		if (nameArray.length >= 4)
		{
			if (log.isDebugEnabled())
			{
				log.debug(fontKey + " resolved to pdf font " + nameArray[3]);
			}
			
			if (awtFontName.equals(nameArray[3]))
			{
				nameMatch = true;
				break;
			}
		}
	}
	
	return nameMatch;
}