如何为字体选择最佳尺寸? [英] How to choose the optimal size for a font?

查看:149
本文介绍了如何为字体选择最佳尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用基于itextpdf中的PdfPTable的网格表创建一个pdf文档。输入数据以java String [] []的形式到达,并填充所有单元格。对于每列,我迭代所有行以标识显示该列数据所需的最大列。这成为列宽。将所有列宽相加以确定整个表的最大列数。此时,我的目的是计算等宽字体的最佳点大小,以完全占据列的宽度。目标是在非常动态的输入数据面前具有最大可读性。对于宽列,我最终会有大量的尾随空格,而不是从左到右很好地填充列。列内的左对齐外观是可取的。运行时环境是RHEL5.X上的openjdk-1.6.0。所使用的任何字体都必须驻留在系统上,因为它没有外部连接。

I'm creating a pdf document with a grid table based on the PdfPTable in itextpdf. The input data arrives as a java String[][] with all the cells filled. For each column, I iterate over all the rows to identify the maximum columns required to display the data for that column. That becomes the column width. All of the column widths are summed to determine the maximum number of columns for the entire table. At this point, my intent is to calculate the optimum point size for a monospace font to fully occupy the width of the columns. The objective is maximum readability in the face of very dynamic input data. For wide columns, I'm ending up with lots of trailing whitespace instead of filling the column nicely from left to right. A left justified appearance inside the column is desirable. The runtime environment is openjdk-1.6.0 on RHEL5.X. Any fonts in use must be resident on the system as it has no outside connectivity.

代码已完全启动并运行,但如果列文本外观可能会更好
完全占据了列fieids。

The code is fully up and running, but the appearance could be better if the column text fully occupied the column fieids.

推荐答案

我已经读了三次你的问题了,我不明白它。

I've read your question three times, and I don't understand it.

让我试着改写它:给定一定的可用宽度,如何定义一段文字的字体大小,以便它匹配可用的宽度。

首先你需要一个 BaseFont 对象。例如:

First you need a BaseFont object. For instance:

BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);

现在你可以在'标准化的1000个单位'中询问基本字体的字符串宽度(这些是'字形空间'中使用的单位;有关详细信息,请参阅ISO-32000-1):

Now you can ask the base font for the width of this String in 'normalized 1000 units' (these are units used in 'Glyph space'; see ISO-32000-1 for more info):

float glyphWidth = bf.getWidth("WHAT IS THE WIDTH OF THIS STRING?");

现在我们可以将这些标准化的1000单位转换为实际的点数(实际上是用户单位,但是为了简单起见,我们假设1个用户单位= 1 pt。)

Now we can convert these 'normalized 1000 units' to an actual size in points (actually user units, but let's assume that 1 user unit = 1 pt for the sake of simplicity).

例如:文本的宽度这条线的宽度是什么?当使用大小为16pt的Courier时:

For instance: the width of the text "WHAT IS THE WIDTH OF THIS STRING?" when using Courier with size 16pt is:

 float width = glyphWidth * 0.001f * 16f;

您的问题不同:您想知道给定的字体大小宽度。这是这样做的:

Your question is different: you want to know the font size for a given width. That's done like this:

 float fontSize = 1000 * width / glyphwidth;

我希望这能回答你的问题。如果没有,请改写。

I hope this answers your question. If not, please rephrase.

这篇关于如何为字体选择最佳尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆