@@ -162,9 +162,8 @@ public virtual void TrueTypeFontWithDifferencesTest(PdfUAConformance pdfUAConfor
162162 , 36 ) . ShowText ( "world" ) . EndText ( ) . RestoreState ( ) . CloseTag ( ) ;
163163 }
164164 ) ;
165- // TODO DEVSIX-9017 Support PDF/UA rules for fonts.
166- // TODO DEVSIX-9004 Support Character encodings related rules in UA-2
167- framework . AssertOnlyVeraPdfFail ( "trueTypeFontWithDifferencesTest" , pdfUAConformance ) ;
165+ framework . AssertBothFail ( "trueTypeFontWithDifferencesTest" , PdfUAExceptionMessageConstants . NON_SYMBOLIC_TTF_SHALL_SPECIFY_MAC_ROMAN_OR_WIN_ANSI_ENCODING
166+ , false , pdfUAConformance ) ;
168167 }
169168
170169 [ NUnit . Framework . TestCaseSource ( "Data" ) ]
@@ -232,7 +231,7 @@ public virtual void NonSymbolicTtfWithValidEncodingTest(PdfUAConformance pdfUACo
232231 document . Add ( paragraph ) ;
233232 }
234233 ) ;
235- framework . AssertBothValid ( "nonSymbolicTtfWithIncompatibleEncoding " , pdfUAConformance ) ;
234+ framework . AssertBothValid ( "nonSymbolicTtfWithValidEncodingTest " , pdfUAConformance ) ;
236235 }
237236
238237 [ NUnit . Framework . TestCaseSource ( "Data" ) ]
@@ -251,8 +250,8 @@ public virtual void NonSymbolicTtfWithIncompatibleEncodingTest(PdfUAConformance
251250 document . Add ( paragraph ) ;
252251 }
253252 ) ;
254- // TODO DEVSIX-9004 Support Character encodings related rules in UA-2
255- framework . AssertOnlyVeraPdfFail ( "nonSymbolicTtfWithIncompatibleEncoding" , pdfUAConformance ) ;
253+ framework . AssertBothFail ( "nonSymbolicTtfWithIncompatibleEncoding" , PdfUAExceptionMessageConstants . NON_SYMBOLIC_TTF_SHALL_SPECIFY_MAC_ROMAN_OR_WIN_ANSI_ENCODING
254+ , false , pdfUAConformance ) ;
256255 }
257256
258257 [ NUnit . Framework . TestCaseSource ( "Data" ) ]
@@ -261,7 +260,8 @@ public virtual void SymbolicTtfTest(PdfUAConformance pdfUAConformance) {
261260 Document document = new Document ( pdfDoc ) ;
262261 PdfFont font ;
263262 try {
264- font = PdfFontFactory . CreateFont ( FONT_FOLDER + "Symbols1.ttf" ) ;
263+ font = PdfFontFactory . CreateFont ( FONT_FOLDER + "Symbols1.ttf" , PdfEncodings . MACROMAN , PdfFontFactory . EmbeddingStrategy
264+ . FORCE_EMBEDDED ) ;
265265 }
266266 catch ( System . IO . IOException ) {
267267 throw new Exception ( ) ;
@@ -280,19 +280,83 @@ public virtual void SymbolicTtfWithEncodingTest(PdfUAConformance pdfUAConformanc
280280 Document document = new Document ( pdfDoc ) ;
281281 PdfFont font ;
282282 try {
283- // if we specify encoding, symbolic font is treated as non-symbolic
284283 font = PdfFontFactory . CreateFont ( FONT_FOLDER + "Symbols1.ttf" , PdfEncodings . MACROMAN , PdfFontFactory . EmbeddingStrategy
285284 . FORCE_EMBEDDED ) ;
286285 }
287286 catch ( System . IO . IOException ) {
288287 throw new Exception ( ) ;
289288 }
289+ font . GetPdfObject ( ) . Put ( PdfName . Encoding , PdfName . MacRomanEncoding ) ;
290+ document . SetFont ( font ) ;
291+ Paragraph paragraph = new Paragraph ( "ABC" ) ;
292+ document . Add ( paragraph ) ;
293+ }
294+ ) ;
295+ // VeraPDF is valid since iText fixes symbolic flag to non-symbolic on closing.
296+ framework . AssertOnlyITextFail ( "symbolicTtfWithEncoding" , PdfUAExceptionMessageConstants . SYMBOLIC_TTF_SHALL_NOT_CONTAIN_ENCODING
297+ , pdfUAConformance ) ;
298+ }
299+
300+ [ NUnit . Framework . TestCaseSource ( "Data" ) ]
301+ public virtual void SymbolicTtfWithInvalidCmapTest ( PdfUAConformance pdfUAConformance ) {
302+ framework . AddBeforeGenerationHook ( ( pdfDoc ) => {
303+ Document document = new Document ( pdfDoc ) ;
304+ PdfFont font ;
305+ try {
306+ TrueTypeFont fontProgram = new PdfUAFontsTest . CustomSymbolicTrueTypeFont ( FONT ) ;
307+ font = PdfFontFactory . CreateFont ( fontProgram , PdfEncodings . MACROMAN , PdfFontFactory . EmbeddingStrategy . FORCE_EMBEDDED
308+ ) ;
309+ }
310+ catch ( System . IO . IOException ) {
311+ throw new Exception ( ) ;
312+ }
313+ document . SetFont ( font ) ;
314+ Paragraph paragraph = new Paragraph ( "ABC" ) ;
315+ document . Add ( paragraph ) ;
316+ }
317+ ) ;
318+ // VeraPDF is valid since iText fixes symbolic flag to non-symbolic on closing.
319+ if ( PdfUAConformance . PDF_UA_1 == pdfUAConformance ) {
320+ framework . AssertOnlyITextFail ( "symbolicTtfWithInvalidCmapTest" , PdfUAExceptionMessageConstants . SYMBOLIC_TTF_SHALL_CONTAIN_EXACTLY_ONE_OR_AT_LEAST_MICROSOFT_SYMBOL_CMAP
321+ , pdfUAConformance ) ;
322+ }
323+ else {
324+ if ( PdfUAConformance . PDF_UA_2 == pdfUAConformance ) {
325+ framework . AssertOnlyITextFail ( "symbolicTtfWithInvalidCmapTest" , PdfUAExceptionMessageConstants . SYMBOLIC_TTF_SHALL_CONTAIN_MAC_ROMAN_OR_MICROSOFT_SYMBOL_CMAP
326+ , pdfUAConformance ) ;
327+ }
328+ }
329+ }
330+
331+ [ NUnit . Framework . TestCaseSource ( "Data" ) ]
332+ public virtual void NonSymbolicTtfWithInvalidCmapTest ( PdfUAConformance pdfUAConformance ) {
333+ framework . AddBeforeGenerationHook ( ( pdfDoc ) => {
334+ Document document = new Document ( pdfDoc ) ;
335+ PdfFont font ;
336+ try {
337+ TrueTypeFont fontProgram = new PdfUAFontsTest . CustomNonSymbolicTrueTypeFont ( FONT ) ;
338+ font = PdfFontFactory . CreateFont ( fontProgram , PdfEncodings . MACROMAN , PdfFontFactory . EmbeddingStrategy . FORCE_EMBEDDED
339+ ) ;
340+ }
341+ catch ( System . IO . IOException ) {
342+ throw new Exception ( ) ;
343+ }
290344 document . SetFont ( font ) ;
291345 Paragraph paragraph = new Paragraph ( "ABC" ) ;
292346 document . Add ( paragraph ) ;
293347 }
294348 ) ;
295- framework . AssertBothValid ( "symbolicTtfWithEncoding" , pdfUAConformance ) ;
349+ // VeraPDF is valid since the file itself is valid, but itext code is modified for testing.
350+ if ( PdfUAConformance . PDF_UA_1 == pdfUAConformance ) {
351+ framework . AssertOnlyITextFail ( "nonSymbolicTtfWithInvalidCmapTest" , PdfUAExceptionMessageConstants . NON_SYMBOLIC_TTF_SHALL_CONTAIN_NON_SYMBOLIC_CMAP
352+ , pdfUAConformance ) ;
353+ }
354+ else {
355+ if ( PdfUAConformance . PDF_UA_2 == pdfUAConformance ) {
356+ framework . AssertOnlyITextFail ( "nonSymbolicTtfWithInvalidCmapTest" , PdfUAExceptionMessageConstants . NON_SYMBOLIC_TTF_SHALL_CONTAIN_MAC_ROMAN_OR_MICROSOFT_UNI_CMAP
357+ , pdfUAConformance ) ;
358+ }
359+ }
296360 }
297361
298362 [ NUnit . Framework . Test ]
@@ -301,5 +365,43 @@ public virtual void SymbolicTtfWithChangedCmapTest() {
301365 NUnit . Framework . Assert . Catch ( typeof ( NullReferenceException ) , ( ) => PdfFontFactory . CreateFont ( FONT_FOLDER +
302366 "Symbols1_changed_cmap.ttf" , PdfFontFactory . EmbeddingStrategy . FORCE_EMBEDDED ) ) ;
303367 }
368+
369+ private class CustomSymbolicTrueTypeFont : TrueTypeFont {
370+ public CustomSymbolicTrueTypeFont ( String path )
371+ : base ( path ) {
372+ }
373+
374+ public override int GetPdfFontFlags ( ) {
375+ return 4 ;
376+ }
377+
378+ public override bool IsCmapPresent ( int platformID , int encodingID ) {
379+ if ( platformID == 1 ) {
380+ return false ;
381+ }
382+ return base . IsCmapPresent ( platformID , encodingID ) ;
383+ }
384+ }
385+
386+ private class CustomNonSymbolicTrueTypeFont : TrueTypeFont {
387+ public CustomNonSymbolicTrueTypeFont ( String path )
388+ : base ( path ) {
389+ }
390+
391+ public override int GetPdfFontFlags ( ) {
392+ return 32 ;
393+ }
394+
395+ public override bool IsCmapPresent ( int platformID , int encodingID ) {
396+ if ( platformID == 1 || encodingID == 1 ) {
397+ return false ;
398+ }
399+ return base . IsCmapPresent ( platformID , encodingID ) ;
400+ }
401+
402+ public override int GetNumberOfCmaps ( ) {
403+ return 0 ;
404+ }
405+ }
304406 }
305407}
0 commit comments