From f50e891ec220a45aad594d1364683c8ff4d8a15f Mon Sep 17 00:00:00 2001 From: Claudio Clemens Date: Sat, 2 Mar 2024 13:25:49 +0100 Subject: [PATCH] Cleanups after PR #1082 - Coverage - Remove unused Method convertToBytes - Add more Tests - Added Font Awesome to tests --- .../com/lowagie/text/pdf/FontDetails.java | 17 +- .../com/lowagie/text/pdf/FontDetailsTest.java | 42 +++++ .../lowagie/text/pdf/TrueTypeFontTest.java | 10 ++ .../text/pdf/TrueTypeFontUnicodeTest.java | 19 ++ .../resources/fonts/font-awesome/LICENSE.txt | 165 ++++++++++++++++++ .../fonts/font-awesome/fa-v4compatibility.ttf | Bin 0 -> 10832 bytes 6 files changed, 241 insertions(+), 12 deletions(-) create mode 100644 openpdf/src/test/java/com/lowagie/text/pdf/FontDetailsTest.java create mode 100644 openpdf/src/test/java/com/lowagie/text/pdf/TrueTypeFontUnicodeTest.java create mode 100644 openpdf/src/test/resources/fonts/font-awesome/LICENSE.txt create mode 100644 openpdf/src/test/resources/fonts/font-awesome/fa-v4compatibility.ttf diff --git a/openpdf/src/main/java/com/lowagie/text/pdf/FontDetails.java b/openpdf/src/main/java/com/lowagie/text/pdf/FontDetails.java index 959c75d81f..1188f3ccc8 100755 --- a/openpdf/src/main/java/com/lowagie/text/pdf/FontDetails.java +++ b/openpdf/src/main/java/com/lowagie/text/pdf/FontDetails.java @@ -197,7 +197,7 @@ byte[] convertToBytes(String text, TextRenderingOptions options) { case BaseFont.FONT_TYPE_T1: case BaseFont.FONT_TYPE_TT: { b = baseFont.convertToBytes(text); - int len = b.length; + for (byte b1 : b) { shortTag[b1 & 0xff] = 1; } @@ -256,7 +256,7 @@ byte[] convertToBytes(String text, TextRenderingOptions options) { return b; } - private byte[] convertToBytesWithGlyphs(String text) throws UnsupportedEncodingException { + private byte[] convertToBytesWithGlyphs(String text) { int len = text.length(); int[] metrics = null; int[] glyph = new int[len]; @@ -274,10 +274,8 @@ private byte[] convertToBytesWithGlyphs(String text) throws UnsupportedEncodingE continue; } int m0 = metrics[0]; - Integer gl = m0; - if (!longTag.containsKey(gl)) { - longTag.put(gl, new int[]{m0, metrics[1], val}); - } + int m1 = metrics[1]; + longTag.computeIfAbsent(m0, key -> new int[]{m0, m1, val}); glyph[i++] = m0; } return getCJKEncodingBytes(glyph, i); @@ -293,10 +291,6 @@ private byte[] getCJKEncodingBytes(int[] glyph, int size) { return result; } - byte[] convertToBytes(GlyphVector glyphVector) { - return convertToBytes(glyphVector, 0, glyphVector.getNumGlyphs()); - } - byte[] convertToBytes(GlyphVector glyphVector, int beginIndex, int endIndex) { if (fontType != BaseFont.FONT_TYPE_TTUNI || symbolic) { throw new UnsupportedOperationException("Only supported for True Type Unicode fonts"); @@ -325,8 +319,7 @@ byte[] convertToBytes(GlyphVector glyphVector, int beginIndex, int endIndex) { String s = new String(glyphs, 0, glyphCount); try { - byte[] b = s.getBytes(CJKFont.CJK_ENCODING); - return b; + return s.getBytes(CJKFont.CJK_ENCODING); } catch (UnsupportedEncodingException e) { throw new ExceptionConverter(e); } diff --git a/openpdf/src/test/java/com/lowagie/text/pdf/FontDetailsTest.java b/openpdf/src/test/java/com/lowagie/text/pdf/FontDetailsTest.java new file mode 100644 index 0000000000..023a3e2244 --- /dev/null +++ b/openpdf/src/test/java/com/lowagie/text/pdf/FontDetailsTest.java @@ -0,0 +1,42 @@ +package com.lowagie.text.pdf; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatNullPointerException; + +import com.lowagie.text.TextRenderingOptions; +import java.io.IOException; +import org.junit.jupiter.api.Test; + +class FontDetailsTest { + + @Test + void convertToBytesBaseFontNullShouldThrowNpe() { + assertThatNullPointerException().isThrownBy(() -> new FontDetails(null, null, null)); + } + + @Test + void convertToBytesShouldExerciseSomeCode() throws IOException { + BaseFont baseFont = BaseFont.createFont( + this.getClass().getClassLoader().getResource("fonts/jp/GenShinGothic-Normal.ttf").getFile(), + BaseFont.IDENTITY_H, BaseFont.EMBEDDED); + FontDetails fontDetails = new FontDetails(null, null, baseFont); + TextRenderingOptions options = new TextRenderingOptions(); + byte[] bytes = fontDetails.convertToBytes("hällö wörld", options); + assertThat(bytes).hasSize(22); + assertThat(fontDetails.isSubset()).isTrue(); + } + + @Test + void convertToBytesAwesomeShouldExerciseSomeCode() throws IOException { + String fontPath = this.getClass().getClassLoader() + .getResource("fonts/font-awesome/fa-v4compatibility.ttf").getFile(); + BaseFont baseFont = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); + FontDetails fontDetails = new FontDetails(null, null, baseFont); + TextRenderingOptions options = new TextRenderingOptions(); + String earthAmericas = "\uf0ac"; + byte[] bytes = fontDetails.convertToBytes(earthAmericas, options); + assertThat(bytes).hasSize(2); + assertThat(fontDetails.isSubset()).isTrue(); + } + +} diff --git a/openpdf/src/test/java/com/lowagie/text/pdf/TrueTypeFontTest.java b/openpdf/src/test/java/com/lowagie/text/pdf/TrueTypeFontTest.java index 0c65509269..6d984fadbc 100644 --- a/openpdf/src/test/java/com/lowagie/text/pdf/TrueTypeFontTest.java +++ b/openpdf/src/test/java/com/lowagie/text/pdf/TrueTypeFontTest.java @@ -11,4 +11,14 @@ void testTrueTypeFontDefaultConstructor() { TrueTypeFont font = new TrueTypeFont(); assertThat(font).isNotNull(); } + + @Test + void testGetTtcNameNoTtc() { + assertThat(TrueTypeFont.getTTCName("font.ttf")).isEqualTo("font.ttf"); + } + + @Test + void testGetTtcName() { + assertThat(TrueTypeFont.getTTCName("font.ttc,123456")).isEqualTo("font.ttc"); + } } diff --git a/openpdf/src/test/java/com/lowagie/text/pdf/TrueTypeFontUnicodeTest.java b/openpdf/src/test/java/com/lowagie/text/pdf/TrueTypeFontUnicodeTest.java new file mode 100644 index 0000000000..04bbcce0cc --- /dev/null +++ b/openpdf/src/test/java/com/lowagie/text/pdf/TrueTypeFontUnicodeTest.java @@ -0,0 +1,19 @@ +package com.lowagie.text.pdf; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.lowagie.text.DocumentException; +import org.junit.jupiter.api.Test; + +class TrueTypeFontUnicodeTest { + + // Test that an Exception will be thrown when the font is not a TTF + @Test + void test() { + assertThatThrownBy( + () -> new TrueTypeFontUnicode("notReallyAFont", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, null, true)) + .isInstanceOf(DocumentException.class) + .hasMessage("notReallyAFont is not a TTF font file."); + } + +} diff --git a/openpdf/src/test/resources/fonts/font-awesome/LICENSE.txt b/openpdf/src/test/resources/fonts/font-awesome/LICENSE.txt new file mode 100644 index 0000000000..39e18e3d30 --- /dev/null +++ b/openpdf/src/test/resources/fonts/font-awesome/LICENSE.txt @@ -0,0 +1,165 @@ +Fonticons, Inc. (https://fontawesome.com) + +-------------------------------------------------------------------------------- + +Font Awesome Free License + +Font Awesome Free is free, open source, and GPL friendly. You can use it for +commercial projects, open source projects, or really almost whatever you want. +Full Font Awesome Free license: https://fontawesome.com/license/free. + +-------------------------------------------------------------------------------- + +# Icons: CC BY 4.0 License (https://creativecommons.org/licenses/by/4.0/) + +The Font Awesome Free download is licensed under a Creative Commons +Attribution 4.0 International License and applies to all icons packaged +as SVG and JS file types. + +-------------------------------------------------------------------------------- + +# Fonts: SIL OFL 1.1 License + +In the Font Awesome Free download, the SIL OFL license applies to all icons +packaged as web and desktop font files. + +Copyright (c) 2023 Fonticons, Inc. (https://fontawesome.com) +with Reserved Font Name: "Font Awesome". + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +http://scripts.sil.org/OFL + +SIL OPEN FONT LICENSE +Version 1.1 - 26 February 2007 + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting — in part or in whole — any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. + +-------------------------------------------------------------------------------- + +# Code: MIT License (https://opensource.org/licenses/MIT) + +In the Font Awesome Free download, the MIT license applies to all non-font and +non-icon files. + +Copyright 2023 Fonticons, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in the +Software without restriction, including without limitation the rights to use, copy, +modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, +and to permit persons to whom the Software is furnished to do so, subject to the +following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +-------------------------------------------------------------------------------- + +# Attribution + +Attribution is required by MIT, SIL OFL, and CC BY licenses. Downloaded Font +Awesome Free files already contain embedded comments with sufficient +attribution, so you shouldn't need to do anything additional when using these +files normally. + +We've kept attribution comments terse, so we ask that you do not actively work +to remove them from files, especially code. They're a great way for folks to +learn about Font Awesome. + +-------------------------------------------------------------------------------- + +# Brand Icons + +All brand icons are trademarks of their respective owners. The use of these +trademarks does not indicate endorsement of the trademark holder by Font +Awesome, nor vice versa. **Please do not use brand logos for any purpose except +to represent the company, product, or service to which they refer.** diff --git a/openpdf/src/test/resources/fonts/font-awesome/fa-v4compatibility.ttf b/openpdf/src/test/resources/fonts/font-awesome/fa-v4compatibility.ttf new file mode 100644 index 0000000000000000000000000000000000000000..b175aa8ece8b1881ed7a23ec6ee6db6ad6b7cd94 GIT binary patch literal 10832 zcmbtaeQaCTb-(xV5A(;qo?iZR<7WBdPXsa!gfx-e1!?icXQEuq71Q$_&)4BxS( zmFC6!zQ=&epGNzozH3a`}j)fVvS9)Co%3q+gvTJloy_=e1@?E*0W!( zRU6Ge`RH#?FqZmD@bEGNuQrE`eLM08pZ&0L`!ASB!8FGHe=>5iJlbuBc_Ue8t2=v?EFW3jXBW$06&Y-`>gjb*f3^c zDKOUkaDNxV`>vDkd5~>v-Szq<$pd)158Z+TekcDcz$Th4`6ZU+xASYxI=}6=yZyi` zFnyrkR0<^7!1#usG(6tv}pqZGC*}*{!v$Pi=i+>y@pmTR+(P(bmtl{^y$K zTH@NB*Y3aez_k-=A-6 z7~e2HZ9Hu}Wn4BMH!c~E7)9g5#s`eI8*@h5NE&Yab^XWsKk9$4zp8&r|EB&8{qy>> z`a#|6`@*(8hG74X&%^9_?&U>(mH)5uHsw{@wCykKe*5kAO~>7iFFWT|yZU9SY|q(n+aqNr;-UZ5D26)c{LSHWizU(=G8zZ8jYr+iA2wlL@%Ykdz5#@`g}`aTrS{LcxBc@cpM2m+eCF`wUh%?d zB?IkzNAP$HJHS{pm(OJ6H>x@;@F;rS|Vrlw9uNhjTea z*|$HH8r5`N+cOp%8VZidXErvQzwNfPzyN0Yw%hWvF|U_5_>GZ{Go9;pZX@xW~pc9a4MBa zf^A793+ZyH6pv~?pH|RSci1fQV9;N1y9@qckQdFcTh$A4)aAPIDF4cv!QM$r=v>OW zm)!goW5Hx% zyVcy-HiQLy(xqv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