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.

PCLTTFCharacterWriterTestCase.java 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.pcl.fonts.truetype;
  19. import java.io.File;
  20. import java.io.FileInputStream;
  21. import org.junit.Test;
  22. import static org.junit.Assert.assertArrayEquals;
  23. import static org.mockito.Mockito.mock;
  24. import static org.mockito.Mockito.when;
  25. import org.apache.fop.fonts.CustomFont;
  26. import org.apache.fop.fonts.truetype.FontFileReader;
  27. import org.apache.fop.fonts.truetype.OFFontLoader;
  28. import org.apache.fop.fonts.truetype.TTFFile;
  29. import org.apache.fop.render.java2d.CustomFontMetricsMapper;
  30. import org.apache.fop.render.pcl.fonts.PCLByteWriterUtil;
  31. import org.apache.fop.render.pcl.fonts.PCLSoftFont;
  32. public class PCLTTFCharacterWriterTestCase {
  33. private PCLTTFCharacterWriter characterWriter;
  34. private PCLSoftFont softFont;
  35. private CustomFontMetricsMapper customFont = mock(CustomFontMetricsMapper.class);
  36. private static final String TEST_FONT_A = "./test/resources/fonts/ttf/DejaVuLGCSerif.ttf";
  37. @Test
  38. public void verifyCharacterDefinition() throws Exception {
  39. CustomFont sbFont = mock(CustomFont.class);
  40. when(customFont.getRealFont()).thenReturn(sbFont);
  41. softFont = new PCLSoftFont(1, customFont, false);
  42. TTFFile openFont = new TTFFile();
  43. FontFileReader reader = new FontFileReader(new FileInputStream(new File(TEST_FONT_A)));
  44. String header = OFFontLoader.readHeader(reader);
  45. openFont.readFont(reader, header);
  46. softFont.setOpenFont(openFont);
  47. softFont.setReader(reader);
  48. characterWriter = new PCLTTFCharacterWriter(softFont);
  49. byte[] charDefinition = characterWriter.writeCharacterDefinitions("f");
  50. PCLByteWriterUtil pclByteWriter = new PCLByteWriterUtil();
  51. // Character command
  52. byte[] command = pclByteWriter.writeCommand(String.format("*c%dE", 32));
  53. assertArrayEquals(getBytes(charDefinition, 0, 6), command);
  54. // Character definition command
  55. byte[] charDefCommand = pclByteWriter.writeCommand(String.format("(s%dW", 210));
  56. assertArrayEquals(getBytes(charDefinition, 6, 7), charDefCommand);
  57. }
  58. private byte[] getBytes(byte[] byteArray, int offset, int length) {
  59. byte[] result = new byte[length];
  60. int count = 0;
  61. for (int i = offset; i < offset + length; i++) {
  62. result[count++] = byteArray[i];
  63. }
  64. return result;
  65. }
  66. }