/* FText.java * * Copyright (c) 2000, Ted Nelson and Tuomas Lukka * * You may use and distribute under the terms of either the GNU Lesser * General Public License, either version 2 of the license or, * at your choice, any later version. Alternatively, you may use and * distribute under the terms of the XPL. * * See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of * the licenses. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README * file for more details. * */ /* * Written by Benjamin Fallenstein */ package org.gzigzag; import java.awt.*; import java.util.*; /** A class representing some formatted text. * The text is represented as FText.Part objects, which * contain a single string of text in a given font, from a given cell * (possibly with an offset and length). * These parts may be created by any view. * @see FTextLayouter */ public class FText { public static final String rcsid = "$Id: FText.java,v 1.21 2001/04/02 20:02:51 bfallenstein Exp $"; public static boolean dbg = false; static final void p(String s) { if(dbg) System.out.println(s); } static final void pa(String s) { System.out.println(s); } public static abstract class Part { public Font f; public FontMetrics fm; public Color bg, fg; public String s; public int n; /** Make a flob showing this part on the screen. */ public abstract Renderable rend(int x, int y, int d, Renderable prev); /** Create another instance of the same class. */ public abstract Part subpart(int first, int last); public Part[] split(int at) { return new Part[] { to(at), from(at) }; } public Part to(int at) { return subpart(0, at); } public Part from(int at) { return subpart(at, n); } public int getStrX(int at) { return fm.stringWidth(s.substring(0, at)); } public int getHit(int x) { return ZZUtil.findStringHit(s, x, fm, true); } public int getHitX(int x) { return getStrX(getHit(x)); } private int wd = -1; public int width() { if(wd == -1) wd = fm.stringWidth(s); return wd; } private int ht = -1; public int height() { if(ht == -1) ht = fm.getHeight(); return ht; } public static String join(Part[] ps) { StringBuffer sb = new StringBuffer(); for(int i=0; i res) res = h; } return res; } public static int len(Part[] ps) { int res = 0; for(int i=0; i= at) { return new int[] { i, p.n-(here-at) }; } } return new int[] { parts.length-1, parts[parts.length-1].n }; } /** Return lines laid out at a given width. * The dimensions of the array are lines[line][part] */ public Part[][] getLines(int width) { return FirstMatchBreaker.stupidBreak(this, width); } public static int findLine(Part[][] lines, int offs) { int here = 0; for(int i=0; i= offs) return i; } return lines.length-1; } /** Capitalize the sentences in the whole FText. */ public void capitalize() { Capitalizer c = new Capitalizer(); for(int i=0; i