You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FontSelectorTestCase.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.fonts;
  19. import java.io.PrintStream;
  20. import java.util.concurrent.ExecutorService;
  21. import java.util.concurrent.Executors;
  22. import java.util.concurrent.TimeUnit;
  23. import org.junit.Before;
  24. import org.junit.Test;
  25. import org.mockito.invocation.InvocationOnMock;
  26. import org.mockito.stubbing.Answer;
  27. import static org.junit.Assert.assertEquals;
  28. import static org.mockito.ArgumentMatchers.anyInt;
  29. import static org.mockito.ArgumentMatchers.eq;
  30. import static org.mockito.Mockito.mock;
  31. import static org.mockito.Mockito.when;
  32. import org.apache.commons.io.output.NullPrintStream;
  33. import org.apache.fop.datatypes.PercentBaseContext;
  34. import org.apache.fop.fo.Constants;
  35. import org.apache.fop.fo.FOEventHandler;
  36. import org.apache.fop.fo.FOText;
  37. import org.apache.fop.fo.PropertyList;
  38. import org.apache.fop.fo.StaticPropertyList;
  39. import org.apache.fop.fo.expr.PropertyException;
  40. import org.apache.fop.fo.pagination.Root;
  41. import org.apache.fop.fo.properties.CommonFont;
  42. import org.apache.fop.fo.properties.EnumProperty;
  43. import org.apache.fop.fo.properties.FixedLength;
  44. import org.apache.fop.fo.properties.FontFamilyProperty;
  45. import org.apache.fop.fo.properties.NumberProperty;
  46. import org.apache.fop.fo.properties.Property;
  47. public class FontSelectorTestCase {
  48. private static final FontTriplet LATIN_FONT_TRIPLET = new FontTriplet("Verdana", "normal", 400);
  49. private static final FontTriplet EMOJI_FONT_TRIPLET = new FontTriplet("Emoji", "normal", 400);
  50. private FOText foText;
  51. private PercentBaseContext context;
  52. private Font latinFont;
  53. private Font emojiFont;
  54. @Before
  55. public void setUp() throws Exception {
  56. FontTriplet[] fontState = new FontTriplet[] { LATIN_FONT_TRIPLET, EMOJI_FONT_TRIPLET };
  57. foText = mock(FOText.class);
  58. context = mock(PercentBaseContext.class);
  59. FOEventHandler eventHandler = mock(FOEventHandler.class);
  60. FontInfo fontInfo = mock(FontInfo.class);
  61. CommonFont commonFont = makeCommonFont();
  62. latinFont = mock(Font.class, "Latin Font");
  63. emojiFont = mock(Font.class, "Emoji Font");
  64. when(eventHandler.getFontInfo()).thenReturn(fontInfo);
  65. when(foText.getFOEventHandler()).thenReturn(eventHandler);
  66. when(foText.getCommonFont()).thenReturn(commonFont);
  67. when(commonFont.getFontState(fontInfo)).thenReturn(fontState);
  68. when(fontInfo.getFontInstance(eq(LATIN_FONT_TRIPLET), anyInt())).thenReturn(latinFont);
  69. when(fontInfo.getFontInstance(eq(EMOJI_FONT_TRIPLET), anyInt())).thenReturn(emojiFont);
  70. when(latinFont.hasCodePoint(anyInt())).thenAnswer(new LatinFontAnswer());
  71. when(emojiFont.hasCodePoint(anyInt())).thenAnswer(new EmojiFontAnswer());
  72. }
  73. @Test
  74. public void selectFontForCharactersInText() throws Exception {
  75. String latinText = "Hello FontSelector";
  76. String emojiText = "\uD83D\uDE48\uD83D\uDE49\uD83D\uDE4A";
  77. String mixedText = latinText + emojiText;
  78. Font f = FontSelector.selectFontForCharactersInText(latinText, 0, latinText.length(), foText, context);
  79. assertEquals(latinFont, f);
  80. f = FontSelector.selectFontForCharactersInText(emojiText, 0, emojiText.length(), foText, context);
  81. assertEquals(emojiFont, f);
  82. // When the text is mixed the font that can cover most chars should be returned
  83. f = FontSelector.selectFontForCharactersInText(mixedText, 0, mixedText.length(), foText, context);
  84. assertEquals(latinFont, f);
  85. f = FontSelector.selectFontForCharactersInText(mixedText, latinText.length() - 1, mixedText.length(), foText,
  86. context);
  87. assertEquals(emojiFont, f);
  88. }
  89. private static class LatinFontAnswer implements Answer<Boolean> {
  90. @Override
  91. public Boolean answer(InvocationOnMock invocation) throws Throwable {
  92. int codepoint = (Integer) invocation.getArguments()[0];
  93. return codepoint <= 0xFFFF;
  94. }
  95. }
  96. private static class EmojiFontAnswer implements Answer<Boolean> {
  97. @Override
  98. public Boolean answer(InvocationOnMock invocation) throws Throwable {
  99. int codepoint = (Integer) invocation.getArguments()[0];
  100. return codepoint > 0xFFFF;
  101. }
  102. }
  103. private CommonFont makeCommonFont() throws PropertyException {
  104. PropertyList pList = mock(PropertyList.class);
  105. String fontFamilyVal = LATIN_FONT_TRIPLET.getName() + "," + EMOJI_FONT_TRIPLET.getName();
  106. Property fontFamilyProp = new FontFamilyProperty.Maker(Constants.PR_FONT_FAMILY).make(pList, fontFamilyVal,
  107. null);
  108. Property fontWeightProp = EnumProperty.getInstance(Constants.PR_FONT_WEIGHT, "400");
  109. Property fontStyle = EnumProperty.getInstance(Constants.PR_FONT_STYLE, "normal");
  110. Property fontSizeAdjustProp = NumberProperty.getInstance(1);
  111. Property fontSizeProp = FixedLength.getInstance(12);
  112. when(pList.get(Constants.PR_FONT_FAMILY)).thenReturn(fontFamilyProp);
  113. when(pList.get(Constants.PR_FONT_WEIGHT)).thenReturn(fontWeightProp);
  114. when(pList.get(Constants.PR_FONT_STYLE)).thenReturn(fontStyle);
  115. when(pList.get(Constants.PR_FONT_SIZE_ADJUST)).thenReturn(fontSizeAdjustProp);
  116. when(pList.get(Constants.PR_FONT_SIZE)).thenReturn(fontSizeProp);
  117. return CommonFont.getInstance(pList);
  118. }
  119. @Test
  120. public void testFontCacheWithThreads() throws Exception {
  121. PrintStream old = System.err;
  122. System.setErr(new NullPrintStream());
  123. ExecutorService executor = Executors.newFixedThreadPool(10);
  124. for (int i = 0; i < 100000; i++) {
  125. executor.submit(new FontTask());
  126. }
  127. executor.shutdown();
  128. executor.awaitTermination(1, TimeUnit.DAYS);
  129. System.setErr(old);
  130. }
  131. static class FontTask implements Runnable {
  132. public void run() {
  133. try {
  134. Root r = new Root(null);
  135. StaticPropertyList pList = new StaticPropertyList(r, null);
  136. Property x = new FontFamilyProperty.Maker(Constants.PR_FONT_FAMILY).make(pList, "a-963191172", null);
  137. pList.putExplicit(Constants.PR_FONT_FAMILY, x);
  138. CommonFont.getInstance(pList);
  139. x = new FontFamilyProperty.Maker(Constants.PR_FONT_FAMILY).make(pList, "a385863058", null);
  140. pList.putExplicit(Constants.PR_FONT_FAMILY, x);
  141. CommonFont.getInstance(pList);
  142. } catch (Exception e) {
  143. throw new RuntimeException(e);
  144. }
  145. }
  146. }
  147. }