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.

PostscriptParserTestCase.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.type1;
  19. import java.io.IOException;
  20. import java.util.List;
  21. import org.junit.Before;
  22. import org.junit.Test;
  23. import static org.junit.Assert.assertEquals;
  24. import static org.junit.Assert.assertTrue;
  25. import org.apache.fop.fonts.type1.PostscriptParser.PSDictionary;
  26. import org.apache.fop.fonts.type1.PostscriptParser.PSElement;
  27. import org.apache.fop.fonts.type1.PostscriptParser.PSFixedArray;
  28. import org.apache.fop.fonts.type1.PostscriptParser.PSSubroutine;
  29. import org.apache.fop.fonts.type1.PostscriptParser.PSVariable;
  30. import org.apache.fop.fonts.type1.PostscriptParser.PSVariableArray;
  31. public class PostscriptParserTestCase {
  32. private PostscriptParser parser;
  33. private String eol = new String(new byte[] {13});
  34. private String postscriptElements =
  35. "/myVariable 100 def" + eol
  36. + "/-| {def} executeonly def" + eol
  37. + "/myFixedArray 6 array" + eol
  38. + "0 1 5 {1 index exch /.notdef put } for" + eol
  39. + "dup 1 /a put" + eol
  40. + "dup 2 /b put" + eol
  41. + "dup 3 /c put" + eol
  42. + "dup 4 /d put" + eol
  43. + "readonly def" + eol
  44. + "/myVariableArray [ { this } { is } { a } { test } ] no access def" + eol
  45. + "/refVarSubr myValue -|";
  46. @Before
  47. public void setUp() {
  48. parser = new PostscriptParser();
  49. }
  50. /**
  51. * Tests parsing an example Postscript document and verifying what
  52. * has been read.
  53. * @throws IOException
  54. */
  55. @Test
  56. public void testPostscriptParsing() throws IOException {
  57. List<PSElement> elements = parser.parse(postscriptElements.getBytes());
  58. assertEquals(elements.size(), 5);
  59. assertTrue(elements.get(0) instanceof PSVariable);
  60. assertTrue(elements.get(2) instanceof PSFixedArray);
  61. assertTrue(elements.get(3) instanceof PSVariableArray);
  62. PSFixedArray fixedArray = (PSFixedArray)elements.get(2);
  63. assertEquals(fixedArray.getEntries().size(), 4);
  64. assertEquals(fixedArray.getEntries().get(2), "dup 2 /b put ");
  65. PSVariableArray variableArray = (PSVariableArray)elements.get(3);
  66. assertEquals(variableArray.getEntries().size(), 4);
  67. /* Currently only variable arrays containing subroutines are supported, though
  68. * this can be modified to support single values and also strip out unnecessary
  69. * characters like the { } below. */
  70. assertEquals(variableArray.getEntries().get(0).trim(), "{ this }");
  71. }
  72. /**
  73. * Tests that the correct element is returned given the operator and element ID provided
  74. */
  75. @Test
  76. public void testCreateElement() {
  77. assertTrue(parser.createElement("/custDictionary", "dict", -1) instanceof PSDictionary);
  78. assertEquals(parser.createElement("/Private", "dict", -1), null);
  79. assertTrue(parser.createElement("/aFixedArray", "array", -1) instanceof PSFixedArray);
  80. assertTrue(parser.createElement("/aVariableArray", "[", -1) instanceof PSVariableArray);
  81. assertTrue(parser.createElement("/aSubroutine", "{", -1) instanceof PSSubroutine);
  82. }
  83. }