sgerrand / alpine-pkg-glibc

A glibc compatibility layer package for Alpine Linux
2.05k stars 280 forks source link

java.awt.geom.Rectangle2D in glibc do not work or support? #74

Closed liuwenru closed 6 years ago

liuwenru commented 6 years ago

Hi, when i use the images to write a string in picture,the method can run and no errors , but can not write string to picture , the code like this

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
public class AppsData {
  public static void main(String[] args) throws IOException,FontFormatException,InterruptedException {
    Font font=Font.createFont(Font.TRUETYPE_FONT,new File(args[0]));
    int with=300;
    int hight=300;
    BufferedImage image=new BufferedImage(with,hight,BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d=image.createGraphics();
    image=g2d.getDeviceConfiguration().createCompatibleImage(with,hight,3);
    g2d.dispose();
    g2d=image.createGraphics();
    g2d.setColor(new Color(10));
    g2d.setStroke(new BasicStroke(1.0F));
    FontRenderContext context=g2d.getFontRenderContext();
    Rectangle2D bounds=font.getStringBounds(“中文”,context);
    double x=(with-bounds.getWidth())/2.0;
    double y=(hight-bounds.getHeight())/2.0;
    double ascent=-bounds.getCenterY();
    double baseY=y+ascent;
    g2d.rotate(Math.toRadians(-45.0D),with/2,hight/2);
    g2d.drawString(args[0],(int)x,(int)baseY);
    g2d.dispose();
    ImageIO.write(image,"png",new File(args[1]));
  }
}

the resoult is like

image

but when i add the font file in ${JRE_HOME}/lib/fonts/fallback/ , then use Font font =new Font("宋体",1,24); it works

i want this image

when i do not in docker all of the method is work ~

ENV:

please give some suggest to solve this problem ~

thinks ~

frol commented 6 years ago

Try using frolvlad/alpine-oraclejre8 / frolvlad/alpine-oraclejdk8 images instead of building your own image. Where did you get glibc 1.2?

liuwenru commented 6 years ago

sorry, my error , i use the repository glibc and the version is glibc-2.23-r3.apk, i want to know why it can not work? thinks

frol commented 6 years ago

I have no idea, but you should try the latest glibc, which is already 2.27-r0. Give a try to the Docker images I referred above.

liuwenru commented 6 years ago

@frol i use the frolvlad/alpine-oraclejdk8 images to test code the same with worn

liuwenru commented 6 years ago

thks, i also test the 2.27-r0 version , it also do not work !

frol commented 6 years ago

I have just read your comment between the two pictures and realized that there is no issue with the glibc, you are just missing the font. Alpine just doesn't ship too many fonts by default. You may try installing various ttf* packages from apk and see if that helps. Otherwise, just bundle your favorite font into the image.

liuwenru commented 6 years ago

@frol yes , i test the way that i drop the font file (ttf) to ${JRE_HOME}/lib/fonts/fallback, it can works but i want use my own ttf in my program, now i want know why it can not work , can give some debug methods ? thinks , my english is poor.

frol commented 6 years ago

I want use my own ttf in my program

So you do, you use your own font from what I see, and it is working fine, isn't it? I don't understand what is your problem then.

liuwenru commented 6 years ago

the problem is :


PS: i use the sgerrand/alpine-pkg-glibc with 2.27-r0 and use this images https://github.com/oracle/docker-images , all of them is error!

frol commented 6 years ago

This just means that CentOS has the necessary font by default and Alpine does not. You should create an image with the necessary font. You may want to start with copying all the fonts from the CentOS image to Alpine image and then, once you know which font you need, you may try to find a package in Alpine repository and install it in your Dockerfile.

liuwenru commented 6 years ago

but the font file is my own design, alpine repository do not have , so i will use Font.createFont("my own font file") method to create it, but now it do not work

sgerrand commented 6 years ago

Thank you for filing this issue and apologies for the late reply. Unfortunately this issue as stated is related solely with your Java code and runtime environment, not this package.