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 0000000000..b175aa8ece Binary files /dev/null and b/openpdf/src/test/resources/fonts/font-awesome/fa-v4compatibility.ttf differ