@Override
    public void drawText(Graphics2D graphics, int width, int height) {
        if(StringUtils.isBlank(text)) {
            return ;
        }
        
        int x = 0, y = 0;
        int fontsize = 1;
        if (position == Position.CENTER) {
            // 计算水印文字总长度
            int textLength = (int) (width * textWidthPercent);
            // 计算水印字体大小
            fontsize = textLength / text.length();
            // 太小了.....不显示
            if (fontsize < minFontSize) {
                return;
            }

            float fsize = (float)fontsize;
            Font font = defaultFont.deriveFont(fsize);
            graphics.setFont(font);
            FontRenderContext context = graphics.getFontRenderContext();
            int sw = (int) font.getStringBounds(text, context).getWidth();

            // 计算字体的坐标
            x = (width - sw) / 2;
            y = height / 2 + fontsize / 2;
        } else if (position == Position.TOP_LEFT) {
            fontsize = ((int)(width * textWidthPercent)) / text.length();
            if (fontsize < minFontSize) {
                return;
            }

            float fsize = (float)fontsize;
            Font font = defaultFont.deriveFont(fsize);
            graphics.setFont(font);

            x = fontsize;
            y = fontsize * 2;
        } else if (position == Position.TOP_RIGHT) {
            fontsize = ((int)(width * textWidthPercent)) / text.length();
            if (fontsize < minFontSize) {
                return;
            }

            float fsize = (float)fontsize;
            Font font = defaultFont.deriveFont(fsize);
            graphics.setFont(font);
            FontRenderContext context = graphics.getFontRenderContext();
            int sw = (int) font.getStringBounds(text, context).getWidth();

            x = width - sw - fontsize;
            y = fontsize * 2;
        } else if (position == Position.BOTTOM_LEFT) {
            fontsize = ((int)(width * textWidthPercent)) / text.length();
            if (fontsize < minFontSize) {
                return;
            }

            float fsize = (float)fontsize;
            Font font = defaultFont.deriveFont(fsize);
            graphics.setFont(font);

            x = fontsize / 2;
            y = height - fontsize;
        } else if (position == Position.BOTTOM_RIGHT) {
            fontsize = ((int)(width * textWidthPercent)) / text.length();
            if (fontsize < minFontSize) {
                return;
            }
            
            float fsize = (float)fontsize;
            Font font = defaultFont.deriveFont(fsize);
            graphics.setFont(font);
            FontRenderContext context = graphics.getFontRenderContext();
            int sw = (int) font.getStringBounds(text, context).getWidth();

            x = width - sw - fontsize;
            y = height - fontsize;
        } else {
            throw new IllegalArgumentException("Unknown position : " + position);
        }

        if(x <= 0 || y <= 0) {
            return ;
        }
        
        if (fontShadowColor != null) {
            graphics.setColor(fontShadowColor);
            graphics.drawString(text, x + getShadowTranslation(fontsize), y + getShadowTranslation(fontsize));
        }

        graphics.setColor(fontColor);
        graphics.drawString(text, x, y);
    }
@ Override public void drawText ( Graphics2D graphics , int width , int height ) { if ( StringUtils . isBlank ( text ) ) { return ; } int x = 0 , y = 0 ; int fontsize = 1 ; if ( position == Position . CENTER ) { int textLength = ( int ) ( width * textWidthPercent ) ; fontsize = textLength / text . length ( ) ; if ( fontsize < minFontSize ) { return ; } float fsize = ( float ) fontsize ; Font font = defaultFont . deriveFont ( fsize ) ; graphics . setFont ( font ) ; FontRenderContext context = graphics . getFontRenderContext ( ) ; int sw = ( int ) font . getStringBounds ( text , context ) . getWidth ( ) ; x = ( width - sw ) / 2 ; y = height / 2 + fontsize / 2 ; } else if ( position == Position . TOP_LEFT ) { fontsize = ( ( int ) ( width * textWidthPercent ) ) / text . length ( ) ; if ( fontsize < minFontSize ) { return ; } float fsize = ( float ) fontsize ; Font font = defaultFont . deriveFont ( fsize ) ; graphics . setFont ( font ) ; x = fontsize ; y = fontsize * 2 ; } else if ( position == Position . TOP_RIGHT ) { fontsize = ( ( int ) ( width * textWidthPercent ) ) / text . length ( ) ; if ( fontsize < minFontSize ) { return ; } float fsize = ( float ) fontsize ; Font font = defaultFont . deriveFont ( fsize ) ; graphics . setFont ( font ) ; FontRenderContext context = graphics . getFontRenderContext ( ) ; int sw = ( int ) font . getStringBounds ( text , context ) . getWidth ( ) ; x = width - sw - fontsize ; y = fontsize * 2 ; } else if ( position == Position . BOTTOM_LEFT ) { fontsize = ( ( int ) ( width * textWidthPercent ) ) / text . length ( ) ; if ( fontsize < minFontSize ) { return ; } float fsize = ( float ) fontsize ; Font font = defaultFont . deriveFont ( fsize ) ; graphics . setFont ( font ) ; x = fontsize / 2 ; y = height - fontsize ; } else if ( position == Position . BOTTOM_RIGHT ) { fontsize = ( ( int ) ( width * textWidthPercent ) ) / text . length ( ) ; if ( fontsize < minFontSize ) { return ; } float fsize = ( float ) fontsize ; Font font = defaultFont . deriveFont ( fsize ) ; graphics . setFont ( font ) ; FontRenderContext context = graphics . getFontRenderContext ( ) ; int sw = ( int ) font . getStringBounds ( text , context ) . getWidth ( ) ; x = width - sw - fontsize ; y = height - fontsize ; } else { throw new IllegalArgumentException ( "Unknown position : " + position ) ; } if ( x <= 0 || y <= 0 ) { return ; } if ( fontShadowColor != null ) { graphics . setColor ( fontShadowColor ) ; graphics . drawString ( text , x + getShadowTranslation ( fontsize ) , y + getShadowTranslation ( fontsize ) ) ; } graphics . setColor ( fontColor ) ; graphics . drawString ( text , x , y ) ; }
/*
(non-Javadoc)
@see com.alibaba.simpleimage.render.DrawTextItem#drawText(java.awt.Graphics2D, int, int)