Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows to add existing font to font resolver #22

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
package com.openhtmltopdf.pdfboxout;

import org.apache.fontbox.ttf.TTFParser;
import org.apache.fontbox.ttf.TrueTypeFont;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDFontDescriptor;
Expand Down Expand Up @@ -156,39 +158,43 @@ public boolean accept(File dir, String name) {

public void addFont(String path, String fontFamilyNameOverride) throws IOException {
String lower = path.toLowerCase(Locale.US);

if (lower.endsWith(".otf") || lower.endsWith(".ttf")) {
PDType0Font font = PDType0Font.load(_doc, new FileInputStream(path), _useSubsets);

String[] fontFamilyNames;
if (fontFamilyNameOverride != null) {
fontFamilyNames = new String[] { fontFamilyNameOverride };
} else {
fontFamilyNames = new String[] { font.getFontDescriptor().getFontFamily() };
}

for (int i = 0; i < fontFamilyNames.length; i++) {
String fontFamilyName = fontFamilyNames[i];
FontFamily fontFamily = getFontFamily(fontFamilyName);

FontDescription descr = new FontDescription(font);
PDFontDescriptor descriptor = font.getFontDescriptor();
descr.setUnderlinePosition(descriptor.getDescent());
descr.setWeight((int) descriptor.getFontWeight());
descr.setStyle(descriptor.getItalicAngle() != 0 ? IdentValue.ITALIC : IdentValue.NORMAL);
// TODO: Check if we can get anything better for measurements below.
descr.setYStrikeoutPosition(descriptor.getFontBoundingBox().getUpperRightY() / 3f);
descr.setYStrikeoutSize(100f);
descr.setUnderlineThickness(50f);

fontFamily.addFontDescription(descr);
}
if (lower.endsWith(".otf") || lower.endsWith(".ttf")) {
addFont(new TTFParser().parse(new FileInputStream(path)), fontFamilyNameOverride);
} else {
// TODO: Logging.
throw new IOException("Unsupported font type");
}
}

public void addFont(TrueTypeFont trueTypeFont, String fontFamilyNameOverride) throws IOException {
PDType0Font font = PDType0Font.load(_doc, trueTypeFont, _useSubsets);

String[] fontFamilyNames;
if (fontFamilyNameOverride != null) {
fontFamilyNames = new String[] { fontFamilyNameOverride };
} else {
fontFamilyNames = new String[] { font.getFontDescriptor().getFontFamily() };
}

for (int i = 0; i < fontFamilyNames.length; i++) {
String fontFamilyName = fontFamilyNames[i];
FontFamily fontFamily = getFontFamily(fontFamilyName);

FontDescription descr = new FontDescription(font);
PDFontDescriptor descriptor = font.getFontDescriptor();
descr.setUnderlinePosition(descriptor.getDescent());
descr.setWeight((int) descriptor.getFontWeight());
descr.setStyle(descriptor.getItalicAngle() != 0 ? IdentValue.ITALIC : IdentValue.NORMAL);
// TODO: Check if we can get anything better for measurements below.
descr.setYStrikeoutPosition(descriptor.getFontBoundingBox().getUpperRightY() / 3f);
descr.setYStrikeoutSize(100f);
descr.setUnderlineThickness(50f);

fontFamily.addFontDescription(descr);
}
}

private void addFontFaceFont(
String fontFamilyNameOverride, IdentValue fontWeightOverride, IdentValue fontStyleOverride, String uri, byte[] font1)
throws IOException {
Expand Down