dromara / hutool

🍬A set of tools that keep Java sweet.
https://hutool.cn
Other
29.17k stars 7.52k forks source link

使用Font.createFont(InputStream fontStream)方法创建的字体 加文字水印时,程序卡死在pressText方法 #2965

Closed mowCrow closed 1 year ago

mowCrow commented 1 year ago

版本情况

JDK版本: dk_8_201 hutool版本:5.8.4

问题描述(包括截图)

  1. 复现代码

        Font font2 = null;
        try {
            font2 = Font.createFont(Font.TRUETYPE_FONT, ResourceUtil.getResourceObj("template/simsun.ttc").getStream());
        } catch (FontFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Img img = Img.from(cn.hutool.core.io.FileUtil.file(imageFile));
        img.pressTextFull(pressText, Color.LIGHT_GRAY,
                font2,
                4,
                -30,
                Float.parseFloat(alpha));
mowCrow commented 1 year ago

问题已解决; 问题原因为当自定义字体未能注册大小时, 执行 FontUtil.getDimension(g.getFontMetrics(font), pressText); 时,获取的height 值为0 ; intervalHeight 也将等于0 ;( int intervalHeight = dimension.height lineHeight;) 当代码执行到下列循环语句时,会陷入死循环,本处无任何异常抛出。 for(int y = -targetHeight >> 1; (double)y < (double)targetHeight 1.5D; y += intervalHeight) { for(int x = -targetWidth >> 1; (double)x < (double)targetWidth * 1.5D; x += dimension.width) { GraphicsUtil.drawString(g, pressText, font, color, new Point(x, y)); } }

mowCrow commented 1 year ago