com.openhtmltopdf.pdfboxout.fontstore.AbstractFontStore Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of openhtmltopdf-pdfbox Show documentation
Show all versions of openhtmltopdf-pdfbox Show documentation
Openhtmltopdf is a CSS 2.1 renderer written in Java. This artifact supports PDF output with Apache PDF-BOX 2.
The newest version!
package com.openhtmltopdf.pdfboxout.fontstore;
import com.openhtmltopdf.css.constants.IdentValue;
import com.openhtmltopdf.layout.SharedContext;
import com.openhtmltopdf.outputdevice.helper.FontFamily;
import com.openhtmltopdf.outputdevice.helper.FontResolverHelper;
import com.openhtmltopdf.pdfboxout.PdfBoxFontResolver;
import com.openhtmltopdf.pdfboxout.PdfBoxFontResolver.FontDescription;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.font.Standard14Fonts;
import java.util.HashMap;
import java.util.Map;
public abstract class AbstractFontStore {
public abstract FontDescription resolveFont(
SharedContext ctx,
String fontFamily,
float size,
IdentValue weight,
IdentValue style,
IdentValue variant);
public static class EmptyFontStore extends AbstractFontStore {
@Override
public FontDescription resolveFont(
SharedContext ctx, String fontFamily, float size, IdentValue weight,
IdentValue style, IdentValue variant) {
return null;
}
}
public static class BuiltinFontStore extends AbstractFontStore {
final Map> _fontFamilies;
public BuiltinFontStore(PDDocument doc) {
this._fontFamilies = createInitialFontMap();
}
static Map> createInitialFontMap() {
HashMap> result = new HashMap<>();
addCourier(result);
addTimes(result);
addHelvetica(result);
addSymbol(result);
addZapfDingbats(result);
return result;
}
static void addCourier(HashMap> result) {
FontFamily courier = new FontFamily<>("Courier");
courier.addFontDescription(new PdfBoxFontResolver.FontDescription(new PDType1Font(Standard14Fonts.FontName.COURIER_BOLD_OBLIQUE), IdentValue.OBLIQUE, 700));
courier.addFontDescription(new PdfBoxFontResolver.FontDescription(new PDType1Font(Standard14Fonts.FontName.COURIER_OBLIQUE), IdentValue.OBLIQUE, 400));
courier.addFontDescription(new PdfBoxFontResolver.FontDescription(new PDType1Font(Standard14Fonts.FontName.COURIER_BOLD), IdentValue.NORMAL, 700));
courier.addFontDescription(new PdfBoxFontResolver.FontDescription(new PDType1Font(Standard14Fonts.FontName.COURIER), IdentValue.NORMAL, 400));
result.put("DialogInput", courier);
result.put("Monospaced", courier);
result.put("Courier", courier);
}
static void addTimes(HashMap> result) {
FontFamily times = new FontFamily<>("Times");
times.addFontDescription(new PdfBoxFontResolver.FontDescription(new PDType1Font(Standard14Fonts.FontName.TIMES_BOLD_ITALIC), IdentValue.ITALIC, 700));
times.addFontDescription(new PdfBoxFontResolver.FontDescription(new PDType1Font(Standard14Fonts.FontName.TIMES_ITALIC), IdentValue.ITALIC, 400));
times.addFontDescription(new PdfBoxFontResolver.FontDescription(new PDType1Font(Standard14Fonts.FontName.TIMES_BOLD), IdentValue.NORMAL, 700));
times.addFontDescription(new PdfBoxFontResolver.FontDescription(new PDType1Font(Standard14Fonts.FontName.TIMES_ROMAN), IdentValue.NORMAL, 400));
result.put("Serif", times);
result.put("TimesRoman", times);
}
static void addHelvetica(HashMap> result) {
FontFamily helvetica = new FontFamily<>("Helvetica");
helvetica.addFontDescription(new PdfBoxFontResolver.FontDescription(new PDType1Font(Standard14Fonts.FontName.HELVETICA_BOLD_OBLIQUE), IdentValue.OBLIQUE, 700));
helvetica.addFontDescription(new PdfBoxFontResolver.FontDescription(new PDType1Font(Standard14Fonts.FontName.HELVETICA_OBLIQUE), IdentValue.OBLIQUE, 400));
helvetica.addFontDescription(new PdfBoxFontResolver.FontDescription(new PDType1Font(Standard14Fonts.FontName.HELVETICA_BOLD), IdentValue.NORMAL, 700));
helvetica.addFontDescription(new PdfBoxFontResolver.FontDescription(new PDType1Font(Standard14Fonts.FontName.HELVETICA), IdentValue.NORMAL, 400));
result.put("Dialog", helvetica);
result.put("SansSerif", helvetica);
result.put("Helvetica", helvetica);
}
static void addSymbol(Map> result) {
FontFamily fontFamily = new FontFamily<>("Symbol");
fontFamily.addFontDescription(new PdfBoxFontResolver.FontDescription(new PDType1Font(Standard14Fonts.FontName.SYMBOL), IdentValue.NORMAL, 400));
result.put("Symbol", fontFamily);
}
static void addZapfDingbats(Map> result) {
FontFamily fontFamily = new FontFamily<>("ZapfDingbats");
fontFamily.addFontDescription(new PdfBoxFontResolver.FontDescription(new PDType1Font(Standard14Fonts.FontName.ZAPF_DINGBATS), IdentValue.NORMAL, 400));
result.put("ZapfDingbats", fontFamily);
}
@Override
public PdfBoxFontResolver.FontDescription resolveFont(
SharedContext ctx,
String fontFamily,
float size,
IdentValue weight,
IdentValue style,
IdentValue variant) {
String normalizedFontFamily = FontUtil.normalizeFontFamily(fontFamily);
FontFamily family = _fontFamilies.get(normalizedFontFamily);
if (family != null) {
return family.match(FontResolverHelper.convertWeightToInt(weight), style);
}
return null;
}
}
}