Skip to content

Commit 1ac0780

Browse files
yulian-gaponenkoitext-teamcity
authored andcommitted
Support WOFF font format
DEVSIX-1148 Autoported commit. Original commit hash: [9ee8674]
1 parent d8aab03 commit 1ac0780

File tree

8 files changed

+250
-22
lines changed

8 files changed

+250
-22
lines changed

itext.tests/itext.kernel.tests/itext/kernel/pdf/PdfFontTest.cs

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public class PdfFontTest : ExtendedITextTest {
6868

6969
internal const String author = "Alexander Chingarev";
7070

71-
internal const String creator = "iText 6";
71+
internal const String creator = "iText 7";
7272

7373
internal const String pangramme = "Amazingly few discothegues provide jukeboxes" + "but it now while sayingly ABEFGHJKNOPQRSTUWYZ?";
7474

@@ -204,7 +204,7 @@ public virtual void CreateDocumentWithType3Font() {
204204
String testString = "A A A A E E E ~ \u00E9";
205205
// A A A A E E E ~ é
206206
//writing type3 font characters
207-
String title = "Type3 font iText 6 Document";
207+
String title = "Type3 font iText 7 Document";
208208
PdfWriter writer = new PdfWriter(filename);
209209
writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
210210
PdfDocument pdfDoc = new PdfDocument(writer);
@@ -280,7 +280,7 @@ public virtual void CreateDocumentWithHelvetica() {
280280
public virtual void CreateDocumentWithHelveticaOblique() {
281281
String filename = destinationFolder + "DocumentWithHelveticaOblique.pdf";
282282
String cmpFilename = sourceFolder + "cmp_DocumentWithHelveticaOblique.pdf";
283-
String title = "Empty iText 6 Document";
283+
String title = "Empty iText 7 Document";
284284
PdfWriter writer = new PdfWriter(filename);
285285
writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
286286
PdfDocument pdfDoc = new PdfDocument(writer);
@@ -305,7 +305,7 @@ public virtual void CreateDocumentWithHelveticaOblique() {
305305
public virtual void CreateDocumentWithHelveticaBoldOblique() {
306306
String filename = destinationFolder + "DocumentWithHelveticaBoldOblique.pdf";
307307
String cmpFilename = sourceFolder + "cmp_DocumentWithHelveticaBoldOblique.pdf";
308-
String title = "Empty iText 6 Document";
308+
String title = "Empty iText 7 Document";
309309
PdfWriter writer = new PdfWriter(filename);
310310
writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
311311
PdfDocument pdfDoc = new PdfDocument(writer);
@@ -330,7 +330,7 @@ public virtual void CreateDocumentWithHelveticaBoldOblique() {
330330
public virtual void CreateDocumentWithCourierBold() {
331331
String filename = destinationFolder + "DocumentWithCourierBold.pdf";
332332
String cmpFilename = sourceFolder + "cmp_DocumentWithCourierBold.pdf";
333-
String title = "Empty iText 6 Document";
333+
String title = "Empty iText 7 Document";
334334
PdfWriter writer = new PdfWriter(filename);
335335
writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
336336
PdfDocument pdfDoc = new PdfDocument(writer);
@@ -355,7 +355,7 @@ public virtual void CreateDocumentWithCourierBold() {
355355
public virtual void CreateDocumentWithType1FontAfm() {
356356
String filename = destinationFolder + "DocumentWithCMR10Afm.pdf";
357357
String cmpFilename = sourceFolder + "cmp_DocumentWithCMR10Afm.pdf";
358-
String title = "Empty iText 6 Document";
358+
String title = "Empty iText 7 Document";
359359
PdfWriter writer = new PdfWriter(filename);
360360
writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
361361
PdfDocument pdfDoc = new PdfDocument(writer);
@@ -387,7 +387,7 @@ public virtual void CreateDocumentWithType1FontAfm() {
387387
public virtual void CreateDocumentWithType1FontPfm() {
388388
String filename = destinationFolder + "DocumentWithCMR10Pfm.pdf";
389389
String cmpFilename = sourceFolder + "cmp_DocumentWithCMR10Pfm.pdf";
390-
String title = "Empty iText 6 Document";
390+
String title = "Empty iText 7 Document";
391391
PdfWriter writer = new PdfWriter(filename);
392392
writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
393393
PdfDocument pdfDoc = new PdfDocument(writer);
@@ -412,7 +412,7 @@ public virtual void CreateDocumentWithType1FontPfm() {
412412
public virtual void CreateDocumentWithTrueTypeFont1() {
413413
String filename = destinationFolder + "DocumentWithTrueTypeFont1.pdf";
414414
String cmpFilename = sourceFolder + "cmp_DocumentWithTrueTypeFont1.pdf";
415-
String title = "Empty iText 6 Document";
415+
String title = "Empty iText 7 Document";
416416
PdfWriter writer = new PdfWriter(filename);
417417
writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
418418
PdfDocument pdfDoc = new PdfDocument(writer);
@@ -443,7 +443,7 @@ public virtual void CreateDocumentWithTrueTypeFont1() {
443443
public virtual void CreateDocumentWithTrueTypeOtfFont() {
444444
String filename = destinationFolder + "DocumentWithTrueTypeOtfFont.pdf";
445445
String cmpFilename = sourceFolder + "cmp_DocumentWithTrueTypeOtfFont.pdf";
446-
String title = "Empty iText 6 Document";
446+
String title = "Empty iText 7 Document";
447447
PdfWriter writer = new PdfWriter(filename);
448448
writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
449449
PdfDocument pdfDoc = new PdfDocument(writer);
@@ -480,7 +480,7 @@ public virtual void CreateDocumentWithTrueTypeOtfFont() {
480480
public virtual void CreateDocumentWithType0OtfFont() {
481481
String filename = destinationFolder + "DocumentWithType0OtfFont.pdf";
482482
String cmpFilename = sourceFolder + "cmp_DocumentWithType0OtfFont.pdf";
483-
String title = "Empty iText 6 Document";
483+
String title = "Empty iText 7 Document";
484484
PdfWriter writer = new PdfWriter(filename);
485485
writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
486486
PdfDocument pdfDoc = new PdfDocument(writer);
@@ -518,7 +518,7 @@ public virtual void TestUpdateType3FontBasedExistingFont() {
518518
String inputFileName = sourceFolder + "type3Font.pdf";
519519
String outputFileName = destinationFolder + "type3Font_update.pdf";
520520
String cmpOutputFileName = sourceFolder + "cmp_type3Font_update.pdf";
521-
String title = "Type3 font iText 6 Document";
521+
String title = "Type3 font iText 7 Document";
522522
PdfReader reader = new PdfReader(inputFileName);
523523
PdfWriter writer = new PdfWriter(outputFileName);
524524
writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
@@ -549,7 +549,7 @@ public virtual void TestNewType3FontBasedExistingFont() {
549549
String inputFileName = sourceFolder + "type3Font.pdf";
550550
String outputFileName = destinationFolder + "type3Font_new.pdf";
551551
String cmpOutputFileName = sourceFolder + "cmp_type3Font_new.pdf";
552-
String title = "Type3 font iText 6 Document";
552+
String title = "Type3 font iText 7 Document";
553553
PdfReader reader = new PdfReader(inputFileName);
554554
PdfWriter pdfWriter = new PdfWriter(outputFileName);
555555
pdfWriter.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
@@ -583,7 +583,7 @@ public virtual void TestNewType1FontBasedExistingFont() {
583583
String inputFileName1 = sourceFolder + "DocumentWithCMR10Afm.pdf";
584584
String filename = destinationFolder + "DocumentWithCMR10Afm_new.pdf";
585585
String cmpFilename = sourceFolder + "cmp_DocumentWithCMR10Afm_new.pdf";
586-
String title = "Type 1 font iText 6 Document";
586+
String title = "Type 1 font iText 7 Document";
587587
PdfReader reader1 = new PdfReader(inputFileName1);
588588
PdfDocument inputPdfDoc1 = new PdfDocument(reader1);
589589
PdfDictionary pdfDictionary = (PdfDictionary)inputPdfDoc1.GetPdfObject(4);
@@ -639,7 +639,7 @@ public virtual void TestNewTrueTypeFont2BasedExistingFont() {
639639
String inputFileName1 = sourceFolder + "DocumentWithTrueTypeFont2.pdf";
640640
String filename = destinationFolder + "DocumentWithTrueTypeFont2_new.pdf";
641641
String cmpFilename = sourceFolder + "cmp_DocumentWithTrueTypeFont2_new.pdf";
642-
String title = "True Type font iText 6 Document";
642+
String title = "True Type font iText 7 Document";
643643
PdfReader reader1 = new PdfReader(inputFileName1);
644644
PdfDocument inputPdfDoc1 = new PdfDocument(reader1);
645645
PdfDictionary pdfDictionary = (PdfDictionary)inputPdfDoc1.GetPdfObject(4);
@@ -692,7 +692,7 @@ public virtual void TestUpdateCjkFontBasedExistingFont() {
692692
String inputFileName1 = sourceFolder + "DocumentWithKozmin.pdf";
693693
String filename = destinationFolder + "DocumentWithKozmin_update.pdf";
694694
String cmpFilename = sourceFolder + "cmp_DocumentWithKozmin_update.pdf";
695-
String title = "Type0 font iText 6 Document";
695+
String title = "Type0 font iText 7 Document";
696696
PdfReader reader = new PdfReader(inputFileName1);
697697
PdfWriter writer = new PdfWriter(filename);
698698
writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
@@ -719,7 +719,7 @@ public virtual void TestNewCjkFontBasedExistingFont() {
719719
String inputFileName1 = sourceFolder + "DocumentWithKozmin.pdf";
720720
String filename = destinationFolder + "DocumentWithKozmin_new.pdf";
721721
String cmpFilename = sourceFolder + "cmp_DocumentWithKozmin_new.pdf";
722-
String title = "Type0 font iText 6 Document";
722+
String title = "Type0 font iText 7 Document";
723723
PdfReader reader1 = new PdfReader(inputFileName1);
724724
PdfDocument inputPdfDoc1 = new PdfDocument(reader1);
725725
PdfDictionary pdfDictionary = (PdfDictionary)inputPdfDoc1.GetPdfObject(6);
@@ -747,7 +747,7 @@ public virtual void CreateDocumentWithTrueTypeAsType0BasedExistingFont() {
747747
String inputFileName1 = sourceFolder + "DocumentWithTrueTypeAsType0.pdf";
748748
String filename = destinationFolder + "DocumentWithTrueTypeAsType0_new.pdf";
749749
String cmpFilename = sourceFolder + "cmp_DocumentWithTrueTypeAsType0_new.pdf";
750-
String title = "Type0 font iText 6 Document";
750+
String title = "Type0 font iText 7 Document";
751751
PdfReader reader1 = new PdfReader(inputFileName1);
752752
PdfDocument inputPdfDoc1 = new PdfDocument(reader1);
753753
PdfDictionary pdfDictionary = (PdfDictionary)inputPdfDoc1.GetPdfObject(6);
@@ -775,7 +775,7 @@ public virtual void CreateUpdatedDocumentWithTrueTypeAsType0BasedExistingFont()
775775
String inputFileName1 = sourceFolder + "DocumentWithTrueTypeAsType0.pdf";
776776
String filename = destinationFolder + "DocumentWithTrueTypeAsType0_update.pdf";
777777
String cmpFilename = sourceFolder + "cmp_DocumentWithTrueTypeAsType0_update.pdf";
778-
String title = "Type0 font iText 6 Document";
778+
String title = "Type0 font iText 7 Document";
779779
PdfReader reader = new PdfReader(inputFileName1);
780780
PdfWriter writer = new PdfWriter(filename);
781781
writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
@@ -801,7 +801,7 @@ public virtual void CreateDocumentWithType1WithToUnicodeBasedExistingFont() {
801801
String inputFileName1 = sourceFolder + "fontWithToUnicode.pdf";
802802
String filename = destinationFolder + "fontWithToUnicode_new.pdf";
803803
String cmpFilename = sourceFolder + "cmp_fontWithToUnicode_new.pdf";
804-
String title = "Type1 font iText 6 Document";
804+
String title = "Type1 font iText 7 Document";
805805
PdfReader reader1 = new PdfReader(inputFileName1);
806806
PdfDocument inputPdfDoc1 = new PdfDocument(reader1);
807807
PdfDictionary pdfDictionary = (PdfDictionary)inputPdfDoc1.GetPdfObject(4);
@@ -994,7 +994,7 @@ public virtual void TestPdfFontFactoryTtc() {
994994
public virtual void TestWriteTTC() {
995995
String filename = destinationFolder + "DocumentWithTTC.pdf";
996996
String cmpFilename = sourceFolder + "cmp_DocumentWithTTC.pdf";
997-
String title = "Empty iText 6 Document";
997+
String title = "Empty iText 7 Document";
998998
PdfWriter writer = new PdfWriter(filename);
999999
writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
10001000
PdfDocument pdfDoc = new PdfDocument(writer);
@@ -1041,6 +1041,24 @@ public virtual void TestNotoFont() {
10411041
"diff_"));
10421042
}
10431043

1044+
/// <exception cref="System.IO.IOException"/>
1045+
/// <exception cref="System.Exception"/>
1046+
[NUnit.Framework.Test]
1047+
public virtual void TestWoffFont() {
1048+
String filename = destinationFolder + "testWoffFont.pdf";
1049+
String cmpFilename = sourceFolder + "cmp_testWoffFont.pdf";
1050+
String helloWorld = "Hello world";
1051+
PdfDocument doc = new PdfDocument(new PdfWriter(filename));
1052+
PdfPage page = doc.AddNewPage();
1053+
PdfFont font = PdfFontFactory.CreateFont(fontsFolder + "Amaranth-Regular.woff", "Identity-H", true);
1054+
PdfCanvas canvas = new PdfCanvas(page);
1055+
canvas.SaveState().BeginText().MoveText(36, 680).SetFontAndSize(font, 12).ShowText(helloWorld).EndText().RestoreState
1056+
();
1057+
doc.Close();
1058+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(filename, cmpFilename, destinationFolder,
1059+
"diff_"));
1060+
}
1061+
10441062
/// <exception cref="System.IO.IOException"/>
10451063
/// <exception cref="System.Exception"/>
10461064
[NUnit.Framework.Test]
Binary file not shown.
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
SIL Open Font License
2+
3+
Copyright (c) 2011, Gesine Todt ([email protected]),
4+
with Reserved Font Name Amaranth.
5+
6+
This Font Software is licensed under the SIL Open Font License, Version 1.1.
7+
License could be found in the file LICENSE_OFL.txt, and is also available with a FAQ at: http://scripts.sil.org/OFL

itext/itext.io/itext/io/IOException.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ public class IOException : Exception {
174174

175175
public const String InvalidJpeg2000File = "Invalid JPEG2000 file.";
176176

177+
public const String InvalidWoffFile = "Invalid WOFF font file.";
178+
177179
[Obsolete]
178180
public const String InvalidMagicValueForBmpFile = "Invalid magic value for bmp file. Must be 'BM'";
179181

itext/itext.io/itext/io/font/FontProgramFactory.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ source product.
4343
*/
4444
using System;
4545
using System.Collections.Generic;
46+
using System.IO;
47+
using iText.IO.Util;
4648

4749
namespace iText.IO.Font {
4850
/// <summary>Provides methods for creating various types of fonts.</summary>
@@ -193,6 +195,9 @@ public static FontProgram CreateFont(String name, byte[] fontProgram, bool cache
193195
if (name == null) {
194196
if (fontProgram != null) {
195197
try {
198+
if (WoffConverter.IsWoffFont(fontProgram)) {
199+
fontProgram = WoffConverter.Convert(fontProgram);
200+
}
196201
fontBuilt = new TrueTypeFont(fontProgram);
197202
}
198203
catch (Exception) {
@@ -216,7 +221,19 @@ public static FontProgram CreateFont(String name, byte[] fontProgram, bool cache
216221
fontBuilt = new CidFont(name, FontCache.GetCompatibleCmaps(baseName));
217222
}
218223
else {
219-
if (baseName.ToLowerInvariant().EndsWith(".ttf") || baseName.ToLowerInvariant().EndsWith(".otf")) {
224+
if (baseName.ToLowerInvariant().EndsWith(".ttf") || baseName.ToLowerInvariant().EndsWith(".otf") || baseName
225+
.ToLowerInvariant().EndsWith(".woff")) {
226+
if (baseName.ToLowerInvariant().EndsWith(".woff")) {
227+
if (fontProgram == null) {
228+
fontProgram = StreamUtil.InputStreamToArray(new FileStream(baseName, FileMode.Open, FileAccess.Read));
229+
}
230+
try {
231+
fontProgram = WoffConverter.Convert(fontProgram);
232+
}
233+
catch (ArgumentException woffException) {
234+
throw new iText.IO.IOException(iText.IO.IOException.InvalidWoffFile, woffException);
235+
}
236+
}
220237
if (fontProgram != null) {
221238
fontBuilt = new TrueTypeFont(fontProgram);
222239
}

0 commit comments

Comments
 (0)