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.

PDFWArray.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.pdf;
  19. import java.util.List;
  20. /**
  21. * Class representing a <b>W</b> array for CID fonts.
  22. */
  23. public class PDFWArray {
  24. /**
  25. * The metrics
  26. */
  27. private List entries = new java.util.ArrayList();
  28. /**
  29. * Default constructor
  30. */
  31. public PDFWArray() {
  32. }
  33. /**
  34. * Convenience constructor
  35. * @param metrics the metrics array to initially add
  36. */
  37. public PDFWArray(int[] metrics) {
  38. addEntry(0, metrics);
  39. }
  40. /**
  41. * Add an entry for single starting CID.
  42. * i.e. in the form "c [w ...]"
  43. *
  44. * @param start the starting CID value.
  45. * @param metrics the metrics array.
  46. */
  47. public void addEntry(int start, int[] metrics) {
  48. entries.add(new Entry(start, metrics));
  49. }
  50. /**
  51. * Add an entry for a range of CIDs (/W element on p 213)
  52. *
  53. * @param first the first CID in the range
  54. * @param last the last CID in the range
  55. * @param width the width for all CIDs in the range
  56. */
  57. public void addEntry(int first, int last, int width) {
  58. entries.add(new int[] {
  59. first, last, width
  60. });
  61. }
  62. /**
  63. * Add an entry for a range of CIDs (/W2 element on p 210)
  64. *
  65. * @param first the first CID in the range
  66. * @param last the last CID in the range
  67. * @param width the width for all CIDs in the range
  68. * @param posX the x component for the vertical position vector
  69. * @param posY the y component for the vertical position vector
  70. */
  71. public void addEntry(int first, int last, int width, int posX, int posY) {
  72. entries.add(new int[] {
  73. first, last, width, posX, posY
  74. });
  75. }
  76. /**
  77. * Convert this object to PDF code.
  78. * @return byte[] the PDF code
  79. */
  80. public byte[] toPDF() {
  81. return PDFDocument.encode(toPDFString());
  82. }
  83. /**
  84. * Convert this object to PDF code.
  85. * @return String the PDF code
  86. */
  87. public String toPDFString() {
  88. StringBuffer p = new StringBuffer();
  89. p.append("[ ");
  90. int len = entries.size();
  91. for (Object entry : entries) {
  92. if (entry instanceof int[]) {
  93. int[] line = (int[]) entry;
  94. for (int aLine : line) {
  95. p.append(aLine);
  96. p.append(" ");
  97. }
  98. } else {
  99. ((Entry) entry).fillInPDF(p);
  100. }
  101. }
  102. p.append("]");
  103. return p.toString();
  104. }
  105. /**
  106. * Inner class for entries in the form "c [w ...]"
  107. */
  108. private static class Entry {
  109. private int start;
  110. private int[] metrics;
  111. public Entry(int s, int[] m) {
  112. start = s;
  113. metrics = m;
  114. }
  115. public void fillInPDF(StringBuffer p) {
  116. // p.setLength(0);
  117. p.append(start);
  118. p.append(" [");
  119. for (int metric : metrics) {
  120. p.append(metric);
  121. p.append(" ");
  122. }
  123. p.append("] ");
  124. }
  125. }
  126. }