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

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

Description

get Multi Line Text For B Box

License

Apache License

Declaration

public static Vector<String> getMultiLineTextForBBox(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.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 Vector<String> getMultiLineTextForBBox(JComponent comp, double bboxWth, String txt, Font font) {
        int startPos = 0, currPos = 0;
        String origText = txt;/*  w  ww .  j a v a2s  .  c o  m*/
        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 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;
    }
}

Related

  1. getDefaultLabelBoldFont()
  2. getDefaultLabelFont()
  3. getFont(String name)
  4. getFontColorToUse(boolean isSelected)
  5. getHelpTextArea(String help, Color background, Font font)
  6. increaseDefaultFont(float multiplier)
  7. initGlobalFont(Font font)
  8. installColorsAndFont(Component c, Color background, Color foreground, Font font)
  9. installLargerDefaultFonts()