0N/A/*
2362N/A * Copyright (c) 1997, 1999, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
0N/A * (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved
0N/A *
0N/A * The original version of this source code and documentation is
0N/A * copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
0N/A * of IBM. These materials are provided under terms of a License
0N/A * Agreement between Taligent and Sun. This technology is protected
0N/A * by multiple US and International patents.
0N/A *
0N/A * This notice and attribution to Taligent may not be removed.
0N/A * Taligent is a registered trademark of Taligent, Inc.
0N/A *
0N/A */
0N/A
0N/Apackage java.awt.font;
0N/A
0N/A/*
0N/A * one info for each side of each glyph
0N/A * separate infos for grow and shrink case
0N/A * !!! this doesn't really need to be a separate class. If we keep it
0N/A * separate, probably the newJustify code from TextLayout belongs here as well.
0N/A */
0N/A
0N/Aclass TextJustifier {
0N/A private GlyphJustificationInfo[] info;
0N/A private int start;
0N/A private int limit;
0N/A
0N/A static boolean DEBUG = false;
0N/A
0N/A /**
0N/A * Initialize the justifier with an array of infos corresponding to each
0N/A * glyph. Start and limit indicate the range of the array to examine.
0N/A */
0N/A TextJustifier(GlyphJustificationInfo[] info, int start, int limit) {
0N/A this.info = info;
0N/A this.start = start;
0N/A this.limit = limit;
0N/A
0N/A if (DEBUG) {
0N/A System.out.println("start: " + start + ", limit: " + limit);
0N/A for (int i = start; i < limit; i++) {
0N/A GlyphJustificationInfo gji = info[i];
0N/A System.out.println("w: " + gji.weight + ", gp: " +
0N/A gji.growPriority + ", gll: " +
0N/A gji.growLeftLimit + ", grl: " +
0N/A gji.growRightLimit);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public static final int MAX_PRIORITY = 3;
0N/A
0N/A /**
0N/A * Return an array of deltas twice as long as the original info array,
0N/A * indicating the amount by which each side of each glyph should grow
0N/A * or shrink.
0N/A *
0N/A * Delta should be positive to expand the line, and negative to compress it.
0N/A */
0N/A public float[] justify(float delta) {
0N/A float[] deltas = new float[info.length * 2];
0N/A
0N/A boolean grow = delta > 0;
0N/A
0N/A if (DEBUG)
0N/A System.out.println("delta: " + delta);
0N/A
0N/A // make separate passes through glyphs in order of decreasing priority
0N/A // until justifyDelta is zero or we run out of priorities.
0N/A int fallbackPriority = -1;
0N/A for (int p = 0; delta != 0; p++) {
0N/A /*
0N/A * special case 'fallback' iteration, set flag and recheck
0N/A * highest priority
0N/A */
0N/A boolean lastPass = p > MAX_PRIORITY;
0N/A if (lastPass)
0N/A p = fallbackPriority;
0N/A
0N/A // pass through glyphs, first collecting weights and limits
0N/A float weight = 0;
0N/A float gslimit = 0;
0N/A float absorbweight = 0;
0N/A for (int i = start; i < limit; i++) {
0N/A GlyphJustificationInfo gi = info[i];
0N/A if ((grow ? gi.growPriority : gi.shrinkPriority) == p) {
0N/A if (fallbackPriority == -1) {
0N/A fallbackPriority = p;
0N/A }
0N/A
0N/A if (i != start) { // ignore left of first character
0N/A weight += gi.weight;
0N/A if (grow) {
0N/A gslimit += gi.growLeftLimit;
0N/A if (gi.growAbsorb) {
0N/A absorbweight += gi.weight;
0N/A }
0N/A } else {
0N/A gslimit += gi.shrinkLeftLimit;
0N/A if (gi.shrinkAbsorb) {
0N/A absorbweight += gi.weight;
0N/A }
0N/A }
0N/A }
0N/A
0N/A if (i + 1 != limit) { // ignore right of last character
0N/A weight += gi.weight;
0N/A if (grow) {
0N/A gslimit += gi.growRightLimit;
0N/A if (gi.growAbsorb) {
0N/A absorbweight += gi.weight;
0N/A }
0N/A } else {
0N/A gslimit += gi.shrinkRightLimit;
0N/A if (gi.shrinkAbsorb) {
0N/A absorbweight += gi.weight;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A // did we hit the limit?
0N/A if (!grow) {
0N/A gslimit = -gslimit; // negative for negative deltas
0N/A }
0N/A boolean hitLimit = (weight == 0) || (!lastPass && ((delta < 0) == (delta < gslimit)));
0N/A boolean absorbing = hitLimit && absorbweight > 0;
0N/A
0N/A // predivide delta by weight
0N/A float weightedDelta = delta / weight; // not used if weight == 0
0N/A
0N/A float weightedAbsorb = 0;
0N/A if (hitLimit && absorbweight > 0) {
0N/A weightedAbsorb = (delta - gslimit) / absorbweight;
0N/A }
0N/A
0N/A if (DEBUG) {
0N/A System.out.println("pass: " + p +
0N/A ", d: " + delta +
0N/A ", l: " + gslimit +
0N/A ", w: " + weight +
0N/A ", aw: " + absorbweight +
0N/A ", wd: " + weightedDelta +
0N/A ", wa: " + weightedAbsorb +
0N/A ", hit: " + (hitLimit ? "y" : "n"));
0N/A }
0N/A
0N/A // now allocate this based on ratio of weight to total weight
0N/A int n = start * 2;
0N/A for (int i = start; i < limit; i++) {
0N/A GlyphJustificationInfo gi = info[i];
0N/A if ((grow ? gi.growPriority : gi.shrinkPriority) == p) {
0N/A if (i != start) { // ignore left
0N/A float d;
0N/A if (hitLimit) {
0N/A // factor in sign
0N/A d = grow ? gi.growLeftLimit : -gi.shrinkLeftLimit;
0N/A if (absorbing) {
0N/A // sign factored in already
0N/A d += gi.weight * weightedAbsorb;
0N/A }
0N/A } else {
0N/A // sign factored in already
0N/A d = gi.weight * weightedDelta;
0N/A }
0N/A
0N/A deltas[n] += d;
0N/A }
0N/A n++;
0N/A
0N/A if (i + 1 != limit) { // ignore right
0N/A float d;
0N/A if (hitLimit) {
0N/A d = grow ? gi.growRightLimit : -gi.shrinkRightLimit;
0N/A if (absorbing) {
0N/A d += gi.weight * weightedAbsorb;
0N/A }
0N/A } else {
0N/A d = gi.weight * weightedDelta;
0N/A }
0N/A
0N/A deltas[n] += d;
0N/A }
0N/A n++;
0N/A } else {
0N/A n += 2;
0N/A }
0N/A }
0N/A
0N/A if (!lastPass && hitLimit && !absorbing) {
0N/A delta -= gslimit;
0N/A } else {
0N/A delta = 0; // stop iteration
0N/A }
0N/A }
0N/A
0N/A if (DEBUG) {
0N/A float total = 0;
0N/A for (int i = 0; i < deltas.length; i++) {
0N/A total += deltas[i];
0N/A System.out.print(deltas[i] + ", ");
0N/A if (i % 20 == 9) {
0N/A System.out.println();
0N/A }
0N/A }
0N/A System.out.println("\ntotal: " + total);
0N/A System.out.println();
0N/A }
0N/A
0N/A return deltas;
0N/A }
0N/A}