如何引入自定义字体重量? [英] How to introduce a custom font weight?

查看:253
本文介绍了如何引入自定义字体重量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



现在我可以像这样设置粗体字体:

  iTextSharp.text.Font font_main = new iTextSharp.text.Font(someBaseFont,12,iTextSharp.text.Font.BOLD); 

所以,只有一个胆量是可用的。但是我想要这样的东西:

  font_main.weight = 600; 

是否可以?

解决方案

当你选择一个字体系列,例如Arial,你还需要选择一个字体,例如Arial Regular( arial.ttf ),Arial Bold( arialbd.ttf ),Arial Italic( ariali.ttf ),Arial Bold-Italic( arialbi.ttf ),...

当您在代码片段中切换样式时,iText会切换字体。换句话说,您不使用Arial Regular来为其应用样式,而是切换到同一族中的另一种字体:Arial Bold。

这样的字体程序作为存储在 arialbd.tff 中的一个,包含绘制一系列字形轮廓(又名路径)的指令。这些字形通过使用填充颜色填充路径来呈现。了解这一点对于实现您的要求非常重要。您可以通过更改线宽渲染模式来控制字体的粗体。 这是特别有用的,如果你正在使用字体系列,没有可用的字体的粗体版本。
$ b iText的

code> object有一个名为 setTextRenderMode()的方法,在 SayPeace 的例子。允许我在iTextSharp中显示相应的方法:

  chunk bold = new Chunk(Bold,font); 
bold.SetTextRenderMode(
PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE,0.5f,
GrayColor.GRAYBLACK
);

第一种方法告诉 Chunk 字形应该像往常一样填充和抚摸(不正常)。笔画的宽度应该为 0.5 用户单位(改变这个以获得不同的 boldness ),最后一个参数定义了文字的颜色。

为了完整性,另外还有两个注释


  1. 有一种类似的方法来模仿斜体字体: setSkew()方法。使用这种方法,你可以引入两个角度。例如 setSkew(0,25)将会写出25度角的字形,或多或少地与斜体字体中使用的角度相对应。
  2. 在过去(很久以前),一些PDF制作者通过在相同的位置一遍又一遍地写同一个字符来模仿大胆。结果,这个角色被渲染成了粗体。这有一些缺点:在ISO标准中没有记录,所以你不能指望所有的观众都会以同样的方式支持这一点。从PDF中提取文本时,会得到同一个字符的多个实例。


Is there a way to set font weight in iTextSharp as a number?

Now I can set bold font like this:

iTextSharp.text.Font font_main = new iTextSharp.text.Font(someBaseFont, 12, iTextSharp.text.Font.BOLD);

So, only one "boldness" is available. But I want something like this:

font_main.weight = 600;

Is it possible?

解决方案

When you pick a font family, for instance Arial, you also need to pick a font, e.g. Arial regular (arial.ttf), Arial Bold (arialbd.ttf), Arial Italic (ariali.ttf), Arial Bold-Italic (arialbi.ttf),...

When you switch styles the way you do in your code snippet, iText switches fonts. In other words: you don't use Arial regular to which you apply a style, you switch to another font in the same family: Arial Bold.

A font program such as the one stored in arialbd.tff, contains the instructions to draw the outlines (aka path) of a series of glyphs. These glyphs are rendered by filling the path using a fill color. Knowing this is important to achieve your requirement.

You can control the boldness of your font by changing the line-width render-mode. This is especially useful in case you are working with a font family for which there is no bold version of the font available.

iText's Chunk object has a method called setTextRenderMode() that is demonstrated in the SayPeace example. Allow me to show the corresponding method in iTextSharp:

Chunk bold = new Chunk("Bold", font);
bold.SetTextRenderMode(
    PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 0.5f,
    GrayColor.GRAYBLACK
);

The first method tells the Chunk that the glyphs should be filled (as usual) and stroked (not usual). The strokes should have a width of 0.5 user units (change this to get a different boldness), the final parameter defines the color of the text.

Two extra remarks for the sake of completeness

  1. There is a similar method to mimic an italic font: the setSkew() method. Using this method, you can introduce two angles. For instance setSkew(0, 25) will write the glyphs with an angle of 25 degrees which more or less corresponds with the angle used in italic fonts.
  2. In the old days (a really long time ago), some PDF producers mimicked boldness by writing the same character over and over again at the same position. As a result, this character was rendered as if it were bold. This has some disadvantages: it is not documented in the ISO standard, so you can't expect that all viewers will support this in the same way. When you extract text from the PDF, you get multiple instances of the same character.

这篇关于如何引入自定义字体重量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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