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.

WritingMode.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.traits;
  19. import java.io.ObjectStreamException;
  20. import org.apache.fop.fo.Constants;
  21. /** Enumeration class for writing mode trait. */
  22. public final class WritingMode extends TraitEnum {
  23. private static final long serialVersionUID = 1L;
  24. private static final String[] WRITING_MODE_NAMES = new String[]
  25. {"lr-tb", "rl-tb", "tb-lr", "tb-rl"};
  26. private static final int[] WRITING_MODE_VALUES = new int[]
  27. {Constants.EN_LR_TB, Constants.EN_RL_TB, Constants.EN_TB_LR, Constants.EN_TB_RL};
  28. /** writing mode: lr-tb */
  29. public static final WritingMode LR_TB = new WritingMode(0);
  30. /** writing mode: rl-tb */
  31. public static final WritingMode RL_TB = new WritingMode(1);
  32. /** writing mode: tb-lr */
  33. public static final WritingMode TB_LR = new WritingMode(2);
  34. /** writing mode: tb-rl */
  35. public static final WritingMode TB_RL = new WritingMode(3);
  36. private static final WritingMode[] WRITING_MODES
  37. = new WritingMode[] {LR_TB, RL_TB, TB_LR, TB_RL};
  38. private WritingMode(int index) {
  39. super(WRITING_MODE_NAMES[index], WRITING_MODE_VALUES[index]);
  40. }
  41. /**
  42. * Assign writing mode traits from this trait to the specified
  43. * writing mode traits setter.
  44. * @param wms a writing mode traits setter
  45. * @param explicit true if writing mode property explicitly specified
  46. */
  47. public void assignWritingModeTraits(WritingModeTraitsSetter wms, boolean explicit) {
  48. Direction inlineProgressionDirection;
  49. Direction blockProgressionDirection;
  50. Direction columnProgressionDirection;
  51. Direction rowProgressionDirection;
  52. Direction shiftDirection;
  53. switch (getEnumValue()) {
  54. case Constants.EN_RL_TB:
  55. inlineProgressionDirection = Direction.RL;
  56. blockProgressionDirection = Direction.TB;
  57. columnProgressionDirection = Direction.RL;
  58. rowProgressionDirection = Direction.TB;
  59. shiftDirection = Direction.BT;
  60. break;
  61. case Constants.EN_TB_LR:
  62. inlineProgressionDirection = Direction.TB;
  63. blockProgressionDirection = Direction.LR;
  64. columnProgressionDirection = Direction.TB;
  65. rowProgressionDirection = Direction.LR;
  66. shiftDirection = Direction.RL;
  67. break;
  68. case Constants.EN_TB_RL:
  69. inlineProgressionDirection = Direction.TB;
  70. blockProgressionDirection = Direction.RL;
  71. columnProgressionDirection = Direction.TB;
  72. rowProgressionDirection = Direction.RL;
  73. shiftDirection = Direction.LR;
  74. break;
  75. case Constants.EN_LR_TB:
  76. default:
  77. inlineProgressionDirection = Direction.LR;
  78. blockProgressionDirection = Direction.TB;
  79. columnProgressionDirection = Direction.LR;
  80. rowProgressionDirection = Direction.TB;
  81. shiftDirection = Direction.BT;
  82. break;
  83. }
  84. wms.setInlineProgressionDirection(inlineProgressionDirection);
  85. wms.setBlockProgressionDirection(blockProgressionDirection);
  86. wms.setColumnProgressionDirection(columnProgressionDirection);
  87. wms.setRowProgressionDirection(rowProgressionDirection);
  88. wms.setShiftDirection(shiftDirection);
  89. wms.setWritingMode(this, explicit);
  90. }
  91. /**
  92. * Determine if WM is horizontal or not.
  93. * @return true if horizontal
  94. */
  95. public boolean isHorizontal() {
  96. switch (getEnumValue()) {
  97. case Constants.EN_LR_TB:
  98. case Constants.EN_RL_TB:
  99. return true;
  100. case Constants.EN_TB_LR:
  101. case Constants.EN_TB_RL:
  102. return false;
  103. default:
  104. assert false;
  105. return true;
  106. }
  107. }
  108. /**
  109. * Determine if WM is vertical or not.
  110. * @return true if vertical
  111. */
  112. public boolean isVertical() {
  113. return !isHorizontal();
  114. }
  115. /**
  116. * Returns the enumeration/singleton object based on its name.
  117. * @param name the name of the enumeration value
  118. * @return the enumeration object
  119. */
  120. public static WritingMode valueOf(String name) {
  121. for (WritingMode writingMode : WRITING_MODES) {
  122. if (writingMode.getName().equalsIgnoreCase(name)) {
  123. return writingMode;
  124. }
  125. }
  126. throw new IllegalArgumentException("Illegal writing mode: " + name);
  127. }
  128. /**
  129. * Returns the enumeration/singleton object based on its name.
  130. * @param enumValue the enumeration value
  131. * @return the enumeration object
  132. */
  133. public static WritingMode valueOf(int enumValue) {
  134. for (WritingMode writingMode : WRITING_MODES) {
  135. if (writingMode.getEnumValue() == enumValue) {
  136. return writingMode;
  137. }
  138. }
  139. throw new IllegalArgumentException("Illegal writing mode: " + enumValue);
  140. }
  141. private Object readResolve() throws ObjectStreamException {
  142. return valueOf(getName());
  143. }
  144. /** {@inheritDoc} */
  145. public String toString() {
  146. return getName();
  147. }
  148. }