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.

TransparentDataControlSequence.java 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.afp.ptoca;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import java.util.ArrayList;
  22. import java.util.Collections;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import org.apache.fop.afp.fonts.CharactersetEncoder.EncodedChars;
  26. import org.apache.fop.afp.ptoca.TransparentDataControlSequence.TransparentData;
  27. import static org.apache.fop.afp.ptoca.PtocaConstants.TRANSPARENT_DATA_MAX_SIZE;
  28. /**
  29. * This object represents a series of PTOCA TransparentData (TRN) control sequences. This implements
  30. * {@link Iterable} to enable iteration through the TRNs.
  31. */
  32. final class TransparentDataControlSequence implements Iterable<TransparentData> {
  33. private static final int MAX_SBCS_TRN_SIZE = TRANSPARENT_DATA_MAX_SIZE;
  34. // The maximum size of a TRN must be an EVEN number so that we're splitting TRNs on character
  35. // boundaries rather than in the middle of a double-byte character
  36. private static final int MAX_DBCS_TRN_SIZE = MAX_SBCS_TRN_SIZE - 1;
  37. static final class TransparentData {
  38. private final int offset;
  39. private final int length;
  40. private final EncodedChars encodedChars;
  41. private TransparentData(int offset, int length, EncodedChars encChars) {
  42. this.offset = offset;
  43. this.length = length;
  44. this.encodedChars = encChars;
  45. }
  46. void writeTo(OutputStream outStream) throws IOException {
  47. encodedChars.writeTo(outStream, offset, length);
  48. }
  49. }
  50. private final List<TransparentData> trns;
  51. /**
  52. * Converts an encoded String wrapped in an {@link EncodedChars} into a series of
  53. * {@link TransparentData} control sequences.
  54. *
  55. * @param encChars the encoded characters to convert to TRNs
  56. */
  57. public TransparentDataControlSequence(EncodedChars encChars) {
  58. int maxTrnLength = encChars.isDBCS() ? MAX_DBCS_TRN_SIZE : MAX_SBCS_TRN_SIZE;
  59. int numTransData = encChars.getLength() / maxTrnLength;
  60. int currIndex = 0;
  61. List<TransparentData> trns = new ArrayList<TransparentData>();
  62. for (int transDataCnt = 0; transDataCnt < numTransData; transDataCnt++) {
  63. trns.add(new TransparentData(currIndex, maxTrnLength, encChars));
  64. currIndex += maxTrnLength;
  65. }
  66. int left = encChars.getLength() - currIndex;
  67. trns.add(new TransparentData(currIndex, left, encChars));
  68. this.trns = Collections.unmodifiableList(trns);
  69. }
  70. /**
  71. * The {@link Iterator} for retrieving the series of TRN control sequences.
  72. */
  73. public Iterator<TransparentData> iterator() {
  74. return trns.iterator();
  75. }
  76. }