Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

FontSelectorTestCase.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 org.junit.Before;
  20. import org.junit.Test;
  21. import org.mockito.invocation.InvocationOnMock;
  22. import org.mockito.stubbing.Answer;
  23. import static org.junit.Assert.assertEquals;
  24. import static org.mockito.Matchers.anyInt;
  25. import static org.mockito.Matchers.eq;
  26. import static org.mockito.Mockito.mock;
  27. import static org.mockito.Mockito.when;
  28. import org.apache.fop.datatypes.PercentBaseContext;
  29. import org.apache.fop.fo.Constants;
  30. import org.apache.fop.fo.FOEventHandler;
  31. import org.apache.fop.fo.FOText;
  32. import org.apache.fop.fo.PropertyList;
  33. import org.apache.fop.fo.expr.PropertyException;
  34. import org.apache.fop.fo.properties.CommonFont;
  35. import org.apache.fop.fo.properties.EnumProperty;
  36. import org.apache.fop.fo.properties.FixedLength;
  37. import org.apache.fop.fo.properties.FontFamilyProperty;
  38. import org.apache.fop.fo.properties.NumberProperty;
  39. import org.apache.fop.fo.properties.Property;
  40. public class FontSelectorTestCase {
  41. private static final FontTriplet LATIN_FONT_TRIPLET = new FontTriplet("Verdana", "normal", 400);
  42. private static final FontTriplet EMOJI_FONT_TRIPLET = new FontTriplet("Emoji", "normal", 400);
  43. private FOText foText;
  44. private PercentBaseContext context;
  45. private Font latinFont;
  46. private Font emojiFont;
  47. @Before
  48. public void setUp() throws Exception {
  49. FontTriplet[] fontState = new FontTriplet[] { LATIN_FONT_TRIPLET, EMOJI_FONT_TRIPLET };
  50. foText = mock(FOText.class);
  51. context = mock(PercentBaseContext.class);
  52. FOEventHandler eventHandler = mock(FOEventHandler.class);
  53. FontInfo fontInfo = mock(FontInfo.class);
  54. CommonFont commonFont = makeCommonFont();
  55. latinFont = mock(Font.class, "Latin Font");
  56. emojiFont = mock(Font.class, "Emoji Font");
  57. when(eventHandler.getFontInfo()).thenReturn(fontInfo);
  58. when(foText.getFOEventHandler()).thenReturn(eventHandler);
  59. when(foText.getCommonFont()).thenReturn(commonFont);
  60. when(commonFont.getFontState(fontInfo)).thenReturn(fontState);
  61. when(fontInfo.getFontInstance(eq(LATIN_FONT_TRIPLET), anyInt())).thenReturn(latinFont);
  62. when(fontInfo.getFontInstance(eq(EMOJI_FONT_TRIPLET), anyInt())).thenReturn(emojiFont);
  63. when(latinFont.hasCodePoint(anyInt())).thenAnswer(new LatinFontAnswer());
  64. when(emojiFont.hasCodePoint(anyInt())).thenAnswer(new EmojiFontAnswer());
  65. }
  66. @Test
  67. public void selectFontForCharactersInText() throws Exception {
  68. String latinText = "Hello FontSelector";
  69. String emojiText = "\uD83D\uDE48\uD83D\uDE49\uD83D\uDE4A";
  70. String mixedText = latinText + emojiText;
  71. Font f = FontSelector.selectFontForCharactersInText(latinText, 0, latinText.length(), foText, context);
  72. assertEquals(latinFont, f);
  73. f = FontSelector.selectFontForCharactersInText(emojiText, 0, emojiText.length(), foText, context);
  74. assertEquals(emojiFont, f);
  75. // When the text is mixed the font that can cover most chars should be returned
  76. f = FontSelector.selectFontForCharactersInText(mixedText, 0, mixedText.length(), foText, context);
  77. assertEquals(latinFont, f);
  78. f = FontSelector.selectFontForCharactersInText(mixedText, latinText.length() - 1, mixedText.length(), foText,
  79. context);
  80. assertEquals(emojiFont, f);
  81. }
  82. private static class LatinFontAnswer implements Answer<Boolean> {
  83. @Override
  84. public Boolean answer(InvocationOnMock invocation) throws Throwable {
  85. int codepoint = (Integer) invocation.getArguments()[0];
  86. return codepoint <= 0xFFFF;
  87. }
  88. }
  89. private static class EmojiFontAnswer 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 CommonFont makeCommonFont() throws PropertyException {
  97. PropertyList pList = mock(PropertyList.class);
  98. String fontFamilyVal = LATIN_FONT_TRIPLET.getName() + "," + EMOJI_FONT_TRIPLET.getName();
  99. Property fontFamilyProp = new FontFamilyProperty.Maker(Constants.PR_FONT_FAMILY).make(pList, fontFamilyVal,
  100. null);
  101. Property fontWeightProp = EnumProperty.getInstance(Constants.PR_FONT_WEIGHT, "400");
  102. Property fontStyle = EnumProperty.getInstance(Constants.PR_FONT_STYLE, "normal");
  103. Property fontSizeAdjustProp = NumberProperty.getInstance(1);
  104. Property fontSizeProp = FixedLength.getInstance(12);
  105. when(pList.get(Constants.PR_FONT_FAMILY)).thenReturn(fontFamilyProp);
  106. when(pList.get(Constants.PR_FONT_WEIGHT)).thenReturn(fontWeightProp);
  107. when(pList.get(Constants.PR_FONT_STYLE)).thenReturn(fontStyle);
  108. when(pList.get(Constants.PR_FONT_SIZE_ADJUST)).thenReturn(fontSizeAdjustProp);
  109. when(pList.get(Constants.PR_FONT_SIZE)).thenReturn(fontSizeProp);
  110. return CommonFont.getInstance(pList);
  111. }
  112. }