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.

Java2DUtilTestCase.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.render.java2d;
  19. import java.awt.Graphics2D;
  20. import java.awt.font.FontRenderContext;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import org.junit.Test;
  24. import org.mockito.invocation.InvocationOnMock;
  25. import org.mockito.stubbing.Answer;
  26. import static org.mockito.ArgumentMatchers.nullable;
  27. import static org.mockito.Matchers.anyInt;
  28. import static org.mockito.Matchers.eq;
  29. import static org.mockito.Mockito.mock;
  30. import static org.mockito.Mockito.verify;
  31. import static org.mockito.Mockito.when;
  32. import org.apache.fop.fonts.Font;
  33. import org.apache.fop.fonts.FontInfo;
  34. import org.apache.fop.fonts.MultiByteFont;
  35. import org.apache.fop.fonts.SingleByteFont;
  36. import org.apache.fop.fonts.Typeface;
  37. import org.apache.fop.util.CharUtilities;
  38. public class Java2DUtilTestCase {
  39. private static final String MULTI_BYTE_FONT_NAME = "multi";
  40. private static final String SINGLE_BYTE_FONT_NAME = "single";
  41. private static final String TEXT = "Hello World!\uD83D\uDCA9";
  42. private static final String EXPECTED_TEXT_SINGLE = "Hello World!#";
  43. private static final String EXPECTED_TEXT_MULTI = "Hello World!\uD83D\uDCA9";
  44. @Test
  45. public void createGlyphVectorMultiByte() throws Exception {
  46. Graphics2D g2d = mock(Graphics2D.class);
  47. java.awt.Font awtFont = mock(java.awt.Font.class);
  48. Font font = makeFont(MULTI_BYTE_FONT_NAME);
  49. FontInfo fontInfo = makeFontInfo();
  50. int[] codepoints = new int[EXPECTED_TEXT_MULTI.codePointCount(0, EXPECTED_TEXT_MULTI.length())];
  51. int i = 0;
  52. for (int cp : CharUtilities.codepointsIter(EXPECTED_TEXT_MULTI)) {
  53. codepoints[i++] = cp;
  54. }
  55. when(g2d.getFont()).thenReturn(awtFont);
  56. Java2DUtil.createGlyphVector(TEXT, g2d, font, fontInfo);
  57. verify(awtFont).createGlyphVector(nullable(FontRenderContext.class), eq(codepoints));
  58. }
  59. @Test
  60. public void createGlyphVectorSingleByte() throws Exception {
  61. Graphics2D g2d = mock(Graphics2D.class);
  62. java.awt.Font awtFont = mock(java.awt.Font.class);
  63. Font font = makeFont(SINGLE_BYTE_FONT_NAME);
  64. FontInfo fontInfo = makeFontInfo();
  65. when(g2d.getFont()).thenReturn(awtFont);
  66. Java2DUtil.createGlyphVector(TEXT, g2d, font, fontInfo);
  67. verify(awtFont).createGlyphVector(nullable(FontRenderContext.class), eq(EXPECTED_TEXT_SINGLE));
  68. }
  69. private FontInfo makeFontInfo() {
  70. Map<String, Typeface> fonts = new HashMap<String, Typeface>();
  71. SingleByteFont singleByteFont = mock(SingleByteFont.class);
  72. MultiByteFont multiByteFont = mock(MultiByteFont.class);
  73. FontInfo fontInfo = mock(FontInfo.class);
  74. fonts.put(MULTI_BYTE_FONT_NAME, multiByteFont);
  75. fonts.put(SINGLE_BYTE_FONT_NAME, singleByteFont);
  76. when(multiByteFont.findGlyphIndex(anyInt())).thenAnswer(new FindGlyphIndexAnswer());
  77. when(fontInfo.getFonts()).thenReturn(fonts);
  78. return fontInfo;
  79. }
  80. private Font makeFont(String fontName) {
  81. Font font = mock(Font.class);
  82. when(font.getFontName()).thenReturn(fontName);
  83. return font;
  84. }
  85. private static class FindGlyphIndexAnswer implements Answer<Integer> {
  86. @Override
  87. public Integer answer(InvocationOnMock invocation) throws Throwable {
  88. return (Integer) invocation.getArguments()[0];
  89. }
  90. }
  91. }