Skip to content

Commit 9bde9a8

Browse files
committed
Minor changes in a font selector logic. Add a new test.
Check whether font alias contains the passed fontName. DEVSIX-1685
1 parent fd74dcd commit 9bde9a8

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

layout/src/main/java/com/itextpdf/layout/font/FontSelector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ private static int characteristicsSimilarity(String fontName, FontCharacteristic
195195
}
196196

197197
if (checkContains) {
198-
//yes, we will not find contains for each alias.
199198
if (descriptor.getFullNameLowerCase().contains(fontName)) score += 3;
200199
if (descriptor.getFontNameLowerCase().contains(fontName)) score += 3;
200+
if (null != fontInfo.getAlias() && fontInfo.getAlias().contains(fontName)) score += 3;
201201
}
202202

203203
return score;

layout/src/test/java/com/itextpdf/layout/FontSelectorTest.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ This file is part of the iText (R) project.
4242
*/
4343
package com.itextpdf.layout;
4444

45+
import com.itextpdf.io.font.FontConstants;
4546
import com.itextpdf.io.font.PdfEncodings;
4647
import com.itextpdf.kernel.colors.ColorConstants;
4748
import com.itextpdf.kernel.pdf.PdfDocument;
4849
import com.itextpdf.kernel.pdf.PdfWriter;
4950
import com.itextpdf.kernel.utils.CompareTool;
51+
import com.itextpdf.layout.element.Div;
5052
import com.itextpdf.layout.element.Paragraph;
5153
import com.itextpdf.layout.element.Text;
5254
import com.itextpdf.layout.font.FontInfo;
@@ -146,6 +148,59 @@ public void latinAndNotdefGroup() throws Exception {
146148
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff"));
147149
}
148150

151+
@Test
152+
public void customFontWeight() throws Exception {
153+
String outFileName = destinationFolder + "customFontWeight.pdf";
154+
String cmpFileName = sourceFolder + "cmp_customFontWeight.pdf";
155+
156+
FontProvider sel = new FontProvider();
157+
sel.getFontSet().addFont(FontConstants.HELVETICA);
158+
sel.getFontSet().addFont(FontConstants.HELVETICA_BOLD);
159+
sel.getFontSet().addFont(FontConstants.TIMES_ROMAN);
160+
sel.getFontSet().addFont(FontConstants.TIMES_BOLD);
161+
162+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileOutputStream(outFileName)));
163+
Document doc = new Document(pdfDoc);
164+
doc.setFontProvider(sel);
165+
166+
Div div = new Div().setFont(FontConstants.TIMES_ROMAN);
167+
Paragraph paragraph = new Paragraph("Times Roman Bold text");
168+
paragraph.setProperty(Property.FONT_WEIGHT, "bold");
169+
div.add(paragraph);
170+
doc.add(div);
171+
172+
doc.close();
173+
174+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff"));
175+
}
176+
177+
@Test
178+
public void customFontWeight2() throws Exception {
179+
String outFileName = destinationFolder + "customFontWeight2.pdf";
180+
String cmpFileName = sourceFolder + "cmp_customFontWeight2.pdf";
181+
182+
FontProvider sel = new FontProvider();
183+
sel.getFontSet().addFont(FontConstants.HELVETICA);
184+
sel.getFontSet().addFont(FontConstants.HELVETICA_BOLD);
185+
sel.getFontSet().addFont(FontConstants.TIMES_ROMAN);
186+
//sel.getFontSet().addFont(FontConstants.TIMES_BOLD);
187+
188+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileOutputStream(outFileName)));
189+
Document doc = new Document(pdfDoc);
190+
doc.setFontProvider(sel);
191+
192+
Div div = new Div().setFont(FontConstants.TIMES_ROMAN);
193+
Paragraph paragraph = new Paragraph("Times Roman Bold text");
194+
paragraph.setProperty(Property.FONT_WEIGHT, "bold");
195+
div.add(paragraph);
196+
doc.add(div);
197+
198+
doc.close();
199+
200+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff"));
201+
}
202+
203+
149204
@Test
150205
public void standardPdfFonts() throws Exception {
151206
String outFileName = destinationFolder + "standardPdfFonts.pdf";

0 commit comments

Comments
 (0)