Java Swing Font Width getHTMLTextForWidth(JComponent comp, double bboxWth, String txt, Font font)

Here you can find the source of getHTMLTextForWidth(JComponent comp, double bboxWth, String txt, Font font)

Description

get HTML Text For Width

License

Apache License

Declaration

public static String getHTMLTextForWidth(JComponent comp, double bboxWth, String txt, Font font) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.awt.Font;
import java.awt.Graphics2D;

import java.awt.Toolkit;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextAttribute;

import java.text.AttributedCharacterIterator;
import java.text.AttributedString;

import java.util.Hashtable;

import java.util.Vector;

import javax.swing.JComponent;

public class Main {
    public static String getHTMLTextForWidth(JComponent comp, double bboxWth, String txt, Font font) {
        boolean useJComp = false;
        Vector<String> multiLineText = null;
        int htmlTextIndex = txt.indexOf("<html>");
        if (htmlTextIndex == -1)
            htmlTextIndex = txt.indexOf("<HTML>");
        if (htmlTextIndex != -1)
            return txt;
        if (useJComp)
            multiLineText = getMultiLineTextForBBox(comp, bboxWth, txt, font);
        else//from www .  j  a  va 2 s . co m
            multiLineText = getMultiLineTextForWidth(bboxWth, txt, font);
        StringBuffer htmlText = new StringBuffer("<html>");
        for (int i = 0; i < multiLineText.size(); i++) {
            htmlText.append(multiLineText.elementAt(i));
            if (i < multiLineText.size() - 1)
                htmlText.append("<br>");
        }
        htmlText.append("</html>");
        return htmlText.toString();
    }

    public static Vector<String> getMultiLineTextForBBox(JComponent comp, double bboxWth, String txt, Font font) {
        int startPos = 0, currPos = 0;
        String origText = txt;
        Graphics2D g2d = (Graphics2D) comp.getGraphics();
        Hashtable<TextAttribute, Object> fontAttrib = new Hashtable<TextAttribute, Object>();
        fontAttrib.put(TextAttribute.FONT, font);

        Vector<String> multiLine = new Vector<String>();
        while (true) {
            currPos = getText4BBox(g2d, txt, startPos, bboxWth, fontAttrib, true);
            System.out.println("Start Pos: " + startPos + " :Curr Pos: " + currPos);
            String newText = origText.substring(startPos, currPos);
            multiLine.addElement(newText);
            if (currPos == origText.length())
                break;
            startPos = currPos;
            if (origText.charAt(startPos) == '\n')
                startPos = startPos + 1;
        }
        return multiLine;
    }

    public static Vector<String> getMultiLineTextForWidth(double bboxWth, String txt, Font font) {
        Vector<String> multiLine = new Vector<String>();
        while (true) {
            String newText = getTextForWidth(bboxWth, txt, font);
            multiLine.addElement(newText);
            if (newText.length() == txt.length())
                break;
            txt = txt.substring(newText.length(), txt.length());
        }
        return multiLine;
    }

    public static String toString(String[] arrStrs) {
        StringBuffer strBuf = new StringBuffer();
        for (int i = 0; i < arrStrs.length; i++) {
            strBuf.append(arrStrs[i] + ", ");
        }
        return strBuf.toString();
    }

    public static int getText4BBox(Graphics2D g2d, String text, int startPos, double width, Hashtable fontAttrib,
            Boolean isParaSelection) {

        AttributedString textString = new AttributedString(text, fontAttrib);
        AttributedCharacterIterator charIter = textString.getIterator();
        int paragraphStart = charIter.getBeginIndex();
        int paragraphEnd = charIter.getEndIndex();
        LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(charIter, g2d.getFontRenderContext());

        // This Margin is provided to avoid text overlap. If 1.0 No Margin set.
        double margin = 1.0;
        double formatWidth = width * margin;

        // Get line offset from lineMeasurer until the entire paragraph has been
        // displayed. This is added to UserSetValue to get the Exact Text during
        // PDF Generation
        int currPos = 0;
        String userSelText;
        if (startPos >= paragraphEnd)
            return -1;
        lineMeasurer.setPosition(startPos);
        while (lineMeasurer.getPosition() < paragraphEnd) {
            // Retrieve next layout.
            lineMeasurer.setPosition(startPos);
            currPos = lineMeasurer.nextOffset((float) formatWidth);
            // userSelText = vdpText.substring(startPos, currPos);
            System.out.println(
                    "Text Start: " + startPos + " :CurrPos: " + currPos + " VDP Text Length: " + text.length());
            break;
        }

        // The New Lines are supported only when the VDP Text Mode is Para.
        if (!isParaSelection) {
            return currPos;
        }
        // Checking for New Line
        int charIndex = 0;
        for (char c = charIter.first(); c != charIter.DONE; c = charIter.next()) {
            charIndex = charIter.getIndex();
            if (charIndex <= startPos)
                continue;
            if (charIndex >= currPos) {
                return currPos;
            }
            if (c == '\n') {
                System.out.println("Found New Line at Char Index: " + charIndex);
                return charIndex;
            }
        }

        return currPos;
    }

    public static String getTextForWidth(Vector<String> splitTxt, double bboxWth, Font font) {
        String selTxt = "", prevSelTxt = "";
        StringBuffer txt4Wt = new StringBuffer();
        for (int i = 0; i < splitTxt.size(); i++) {
            txt4Wt.append(splitTxt.elementAt(i));
            selTxt = getTextForWidth(bboxWth, txt4Wt.toString(), font);
            if (selTxt.length() < txt4Wt.toString().length()) {
                selTxt = prevSelTxt;
                break;
            }
            prevSelTxt = selTxt;
        }
        // System.out.println("Prev Sel Txt: " + prevSelTxt + " selTxt: " +
        // selTxt);
        return selTxt;
    }

    public static String getTextForWidth(double bboxWth, String txt, Font font) {
        java.awt.FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(font);
        int width = fm.stringWidth(txt);
        String origText = txt;
        while (true) {
            if (width < bboxWth)
                break;
            txt = txt.substring(0, txt.length() - 1);
            width = fm.stringWidth(txt);
        }

        return txt;
    }
}

Related

  1. estimateWidth(Font font, String msg)
  2. getEditor(String html, int width, Color transparentColor, Font font)
  3. getStringWidth(final Font aFont, final String aString)
  4. getStringWidth(Font font, String str)
  5. getTextWidth(final String text, final Font font)
  6. getTextWidth(Font font, String text)