public static int getBaseline(JComponent c,
                                  FontMetrics fm,
                                  String text,
                                  int verticalAlignment,
                                  int verticalTextPosition,
                                  Rectangle viewR) {
        /* Initialize the text bounds rectangle textR.  If a null
         * or and empty String was specified we substitute "" here
         * and use 0,0,0,0 for textR.
         */

        Rectangle textR = new Rectangle();
        boolean textIsEmpty = (text == null) || text.equals("");

        View v = null;
        if (textIsEmpty) {
            textR.height = 0;
            text = "";
        } else {
            v = (c != null) ? (View) c.getClientProperty("html") : null;
            if (v != null) {
                textR.height = (int) v.getPreferredSpan(View.Y_AXIS);
            } else {
                textR.height = fm.getHeight();
            }
        }


        /* Compute textR.x,y given the verticalTextPosition and
        * horizontalTextPosition properties
        */

        if (verticalTextPosition == SwingConstants.TOP) {
            textR.y = -textR.height;
        } else if (verticalTextPosition == SwingConstants.CENTER) {
            textR.y = -(textR.height / 2);
        } else { // (verticalTextPosition == BOTTOM)
            textR.y = 0;
        }

        /* labelR is the rectangle that contains iconR and textR.
        * Move it to its proper position given the labelAlignment
        * properties.
        *
        * To avoid actually allocating a Rectangle, Rectangle.union
        * has been inlined below.
        */
        int labelR_y = 0;
        int labelR_height = textR.y + textR.height - labelR_y;

        int dy;

        if (verticalAlignment == SwingConstants.TOP) {
            dy = viewR.y - labelR_y;
        } else if (verticalAlignment == SwingConstants.CENTER) {
            // Fix the problem for buttons.
            int tmp = ((2 * viewR.y + viewR.height) - (2 * labelR_y + labelR_height));
            if (tmp / 2 * 2 != tmp) tmp++;
            dy = tmp / 2;
        } else { // (verticalAlignment == BOTTOM)
            dy = (viewR.y + viewR.height) - (labelR_y + labelR_height);
        }

        /* Translate textR and glypyR by dx,dy.
        */

        textR.y += dy;

        return textR.y + textR.height;
    }