Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Units of measure in boxable #238

Open
aycoholleman opened this issue Aug 20, 2021 · 0 comments
Open

Units of measure in boxable #238

aycoholleman opened this issue Aug 20, 2021 · 0 comments

Comments

@aycoholleman
Copy link

I am a bit confused about the unit of measure generally used as input or output for the API. More concretely:

I want to get thewidth of a string, given a font and font size, in millimeters. I also want to express the font size itself in millimeters.

If I look into the source code of your FontUtils class, I see this method:

  public static float getStringWidth(final PDFont font, final String text, final float fontSize) {
    try {
      return font.getStringWidth(text) / 1000 * fontSize;
    } catch (final IOException e) {
      // turn into runtime exception
      throw new IllegalStateException("Unable to determine text width", e);
    }
  }

I assume the unit of measure for fontSize is points, and what's returned is also the string width in points, but I am not sure.

So to create an equivalent millimeter-based method:

  public static double getStringWidthMillimeters(String text, PDFont font, double fontSizeMM)
      throws IOException {
    double fontSizePts = fontSizeMM * POINTS_PER_MM;
    double widthPts = (font.getStringWidth(text) / 1000) * fontSizePts;
    double widthMM = widthPts / POINTS_PER_MM;
    return widthMM;
  }

This feels very fishy, because the multiplication and subsequent division by POINTS_PER_MM really cancel out:

return ((font.getStringWidth(text) / 1000) * (fontSizeMM * POINTS_PER_MM)) / POINTS_PER_MM;

Could you help me out here?

Regards,

Ayco

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant