1. /*
  2. * @(#)TextJustifier.java 1.18 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. /*
  8. * (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
  9. * (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved
  10. *
  11. * The original version of this source code and documentation is
  12. * copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
  13. * of IBM. These materials are provided under terms of a License
  14. * Agreement between Taligent and Sun. This technology is protected
  15. * by multiple US and International patents.
  16. *
  17. * This notice and attribution to Taligent may not be removed.
  18. * Taligent is a registered trademark of Taligent, Inc.
  19. *
  20. */
  21. package java.awt.font;
  22. /*
  23. * one info for each side of each glyph
  24. * separate infos for grow and shrink case
  25. * !!! this doesn't really need to be a separate class. If we keep it
  26. * separate, probably the newJustify code from TextLayout belongs here as well.
  27. */
  28. class TextJustifier {
  29. private GlyphJustificationInfo[] info;
  30. private int start;
  31. private int limit;
  32. static boolean DEBUG = false;
  33. /**
  34. * Initialize the justifier with an array of infos corresponding to each
  35. * glyph. Start and limit indicate the range of the array to examine.
  36. */
  37. TextJustifier(GlyphJustificationInfo[] info, int start, int limit) {
  38. this.info = info;
  39. this.start = start;
  40. this.limit = limit;
  41. if (DEBUG) {
  42. System.out.println("start: " + start + ", limit: " + limit);
  43. for (int i = start; i < limit; i++) {
  44. GlyphJustificationInfo gji = info[i];
  45. System.out.println("w: " + gji.weight + ", gp: " +
  46. gji.growPriority + ", gll: " +
  47. gji.growLeftLimit + ", grl: " +
  48. gji.growRightLimit);
  49. }
  50. }
  51. }
  52. public static final int MAX_PRIORITY = 3;
  53. /**
  54. * Return an array of deltas twice as long as the original info array,
  55. * indicating the amount by which each side of each glyph should grow
  56. * or shrink.
  57. *
  58. * Delta should be positive to expand the line, and negative to compress it.
  59. */
  60. public float[] justify(float delta) {
  61. float[] deltas = new float[info.length * 2];
  62. boolean grow = delta > 0;
  63. if (DEBUG)
  64. System.out.println("delta: " + delta);
  65. // make separate passes through glyphs in order of decreasing priority
  66. // until justifyDelta is zero or we run out of priorities.
  67. int fallbackPriority = -1;
  68. for (int p = 0; delta != 0; p++) {
  69. /*
  70. * special case 'fallback' iteration, set flag and recheck
  71. * highest priority
  72. */
  73. boolean lastPass = p > MAX_PRIORITY;
  74. if (lastPass)
  75. p = fallbackPriority;
  76. // pass through glyphs, first collecting weights and limits
  77. float weight = 0;
  78. float gslimit = 0;
  79. float absorbweight = 0;
  80. for (int i = start; i < limit; i++) {
  81. GlyphJustificationInfo gi = info[i];
  82. if ((grow ? gi.growPriority : gi.shrinkPriority) == p) {
  83. if (fallbackPriority == -1) {
  84. fallbackPriority = p;
  85. }
  86. if (i != start) { // ignore left of first character
  87. weight += gi.weight;
  88. if (grow) {
  89. gslimit += gi.growLeftLimit;
  90. if (gi.growAbsorb) {
  91. absorbweight += gi.weight;
  92. }
  93. } else {
  94. gslimit += gi.shrinkLeftLimit;
  95. if (gi.shrinkAbsorb) {
  96. absorbweight += gi.weight;
  97. }
  98. }
  99. }
  100. if (i + 1 != limit) { // ignore right of last character
  101. weight += gi.weight;
  102. if (grow) {
  103. gslimit += gi.growRightLimit;
  104. if (gi.growAbsorb) {
  105. absorbweight += gi.weight;
  106. }
  107. } else {
  108. gslimit += gi.shrinkRightLimit;
  109. if (gi.shrinkAbsorb) {
  110. absorbweight += gi.weight;
  111. }
  112. }
  113. }
  114. }
  115. }
  116. // did we hit the limit?
  117. if (!grow) {
  118. gslimit = -gslimit; // negative for negative deltas
  119. }
  120. boolean hitLimit = (weight == 0) || (!lastPass && ((delta < 0) == (delta < gslimit)));
  121. boolean absorbing = hitLimit && absorbweight > 0;
  122. // predivide delta by weight
  123. float weightedDelta = delta / weight; // not used if weight == 0
  124. float weightedAbsorb = 0;
  125. if (hitLimit && absorbweight > 0) {
  126. weightedAbsorb = (delta - gslimit) / absorbweight;
  127. }
  128. if (DEBUG) {
  129. System.out.println("pass: " + p +
  130. ", d: " + delta +
  131. ", l: " + gslimit +
  132. ", w: " + weight +
  133. ", aw: " + absorbweight +
  134. ", wd: " + weightedDelta +
  135. ", wa: " + weightedAbsorb +
  136. ", hit: " + (hitLimit ? "y" : "n"));
  137. }
  138. // now allocate this based on ratio of weight to total weight
  139. int n = start * 2;
  140. for (int i = start; i < limit; i++) {
  141. GlyphJustificationInfo gi = info[i];
  142. if ((grow ? gi.growPriority : gi.shrinkPriority) == p) {
  143. if (i != start) { // ignore left
  144. float d;
  145. if (hitLimit) {
  146. // factor in sign
  147. d = grow ? gi.growLeftLimit : -gi.shrinkLeftLimit;
  148. if (absorbing) {
  149. // sign factored in already
  150. d += gi.weight * weightedAbsorb;
  151. }
  152. } else {
  153. // sign factored in already
  154. d = gi.weight * weightedDelta;
  155. }
  156. deltas[n] += d;
  157. }
  158. n++;
  159. if (i + 1 != limit) { // ignore right
  160. float d;
  161. if (hitLimit) {
  162. d = grow ? gi.growRightLimit : -gi.shrinkRightLimit;
  163. if (absorbing) {
  164. d += gi.weight * weightedAbsorb;
  165. }
  166. } else {
  167. d = gi.weight * weightedDelta;
  168. }
  169. deltas[n] += d;
  170. }
  171. n++;
  172. } else {
  173. n += 2;
  174. }
  175. }
  176. if (!lastPass && hitLimit && !absorbing) {
  177. delta -= gslimit;
  178. } else {
  179. delta = 0; // stop iteration
  180. }
  181. }
  182. if (DEBUG) {
  183. float total = 0;
  184. for (int i = 0; i < deltas.length; i++) {
  185. total += deltas[i];
  186. System.out.print(deltas[i] + ", ");
  187. if (i % 20 == 9) {
  188. System.out.println();
  189. }
  190. }
  191. System.out.println("\ntotal: " + total);
  192. System.out.println();
  193. }
  194. return deltas;
  195. }
  196. }