Aros/开发者/文档/库/DiskFont
AROS 可以使用两种不同的字体以及其他字体类型,例如更常见的 TrueType 字体(AROS 使用 FreeType 等效字体),您需要使用另一个库
字体基本上是字形(字母形状)、样式(粗体、普通、斜体)和大小(以字体大小为单位)的混合体。AROS 内置了一个名为 Topaz 的字体,可以随时访问。如果使用基于磁盘的字体,则需要使用 diskfont.library 从磁盘加载字体。
自定义格式的位平面数据,以及每个字符的水平偏移量和左右侧宽度的元数据。加载器位于 Diskfont.library 中,名为 OpenDiskFont()。加载后,位图字体由 OpenFont() 处理
Wanderer 使用 NULL 字体调用 IconList,因为 diskfont.library 无法打开“arial.font”。另一方面,这是因为 diskfont.library 无法在 FONTS: 中找到 arial.font,因为 ExAll 调用在其第一次调用时返回 0 (== 完成)。这意味着并非所有字体都被返回,因为传递的 1024 字节缓冲区太小,无法一次性获取所有字体。
加载磁盘字体
要加载磁盘字体,请使用 OpenDiskFont() 函数。您需要指定一个 TextAttr 结构来指定您需要的字体,命令的格式是
TextFont font = OpenDiskFont(struct TextAttr textAttr)
TextAttr 的格式是
struct TextAttr {
STRPTR ta_Name; /* name of the font */
UWORD ta_YSize; /* height of the font */
UBYTE ta_Style; /* intrinsic font style */
UBYTE ta_Flags; /* font preferences and flags */
};
例如,要指定 Topaz 字体,大小为 11,粗体和斜体
struct TextAttr myta = {
"topaz.font"
11,
FSF_ITALIC | FSF_BOLD,
NULL
};
如果您使用此字体进行绘图,可以使用 SetFont() 命令更改默认 Rastport 字体,并在使用完后使用 CloseFont() 函数:例如
struct TextFont *myfont, *oldfont;
struct RastPort *myrp;
struct Window *mywin;
if (myfont = OpenDiskFont(&myta))
{
/* you would probably set the font of the rastport you are going to use */
myrp = mywin->RPort
oldfont = myrp->Font;
SetFont(myrp, myfont);
/* perform whatever drawing you need to do */
/* time to clean up. If the rastport is not exclusively yours,
you may need to restore the original font or other Rasport values */
SetFont(myrp, oldfont);
CloseFont(myfont);
}
如果您想在尝试从磁盘加载字体之前知道程序中有哪些可用字体,可以使用 AvailFonts() 函数,该函数可以读取内存和磁盘中的所有字体,并将它们显示在 AvailFontsHeader 结构中,随后是一系列 AvailFonts 结构,供您的程序读取。在调用此函数之前,您需要分配一些内存来存储字体信息。
LONG error = AvailFonts(struct AvailFontsHeader *buffer, LONG bufBytes, ULONG flags )
例如,
int afShortage, afSize;
struct AvailFontsHeader *afh;
afSize = 400;
do {
afh = (struct AvailFontsHeader *) AllocMem(afSize, 0);
if (afh) {
afShortage = AvailFonts(afh, afSize, AFF_MEMORY|AFF_DISK);
if (afShortage) {
FreeMem(afh, afSize);
afSize += afShortage;
}
}
else {
fail("AllocMem of AvailFonts buffer afh failedn");
break;
}
}while (afShortage);
对于 FontContents(或 TFontContents)结构中描述的每个字体大小,该字体的目录中都存在一个相应的文件,其名称为其大小。例如,对于字体大小 Sapphire-19,Sapphire 目录中有一个名为 19 的文件。该文件基本上是一个 DiskFontHeader,伪装成一个可加载的 DOS 段,被称为字体描述符文件。
对于位图字体,“.font” 文件是一个 FontContentsHeader 结构
struct FontContentsHeader {
UWORD fch_FileID; /* FCH_ID */
UWORD fch_NumEntries; /* the number of FontContents elements */
struct FontContents fch_FC[]; /* or struct TFontContents fch_TFC[]; */
};
#define MAXFONTPATH 256
其中 fch_FileID 字段可以是
FCH_ID 0x0f00 uses FontContents structures to describe the available sizes of this font.
TFCH_ID 0x0f02 uses TFontContents structures to describe the available sizes of this font.
FontContents 结构
struct FontContents {
char fc_FileName[MAXFONTPATH];
UWORD fc_YSize;
UBYTE fc_Style;
UBYTE fc_Flags;
};
struct TFontContents {
char tfc_FileName[MAXFONTPATH-2];
UWORD tfc_TagCount; /* including the TAG_DONE tag */
/*
* if tfc_TagCount is non-zero, tfc_FileName is overlaid with
* Text Tags starting at: (struct TagItem *)
* &tfc_FileName[MAXFONTPATH-(tfc_TagCount*sizeof
* (struct TagItem))]
*/
UWORD tfc_YSize;
UBYTE tfc_Style;
UBYTE tfc_Flags;
};
struct DiskFontHeader {
ULONG dfh_NextSegment;
ULONG dfh_ReturnCode;
STRUCT dfh,LN_SIZE;
UWORD dfh_FileID;
UWORD dfh_Revision;
LONG dfh_Segment;
STRUCT dfh,MAXFONTNAME;
STRUCT dfh-TF,tf_SIZEOF;
};
有人知道使用什么程序生成 ttcourier 字体吗?它们由 Georg 在 2001 年提交。
据我所知,BitLine(参见 Aminet,附带源代码)。使用 diskfont.library 将字体加载到内存中,然后将其保存到磁盘。源字体是 TrueType 字体,diskfont.library 在加载时将其转换为位图字体(如果有像基于 freetype 的字体引擎,可以处理它)。
这些字体存在一个小问题:字形都与它们框的左侧对齐,而不是居中(这是纸张切割错误之一)。
快速查看 ttcourier 字体(使用 sys:tools/fontinfo 和 sys:extras/misc/aminet/ 中的位图字体编辑器“TypeFace”)。似乎只有某些大小(例如 14)存在问题,问题是缺少/为零的字距/间距表。
同样查看工作台/fonts/ 中字体文件(“14”等)的 svn 日志,似乎问题可能是当欧元字符被(手动)添加到字体中时发生的(据我所知,使用在 AROS 下运行的 TypeFace 字体编辑器 -> 可能那里存在一个错误),因为我对“14”文件的初始修订版进行了快速测试,它似乎没有缺少字距/间距表。
请注意,根据样式和其他因素,字体并不总是使用相同的 HIDD 方法渲染。参见 rom/graphics/text.c。截图中不可见的文本是使用 BlitColorexpansion 方法渲染的文本。
#include <diskfont/diskfont.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/diskfont.h>
#include <proto/utility.h>
#include <proto/graphics.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
struct Library *DiskfontBase;
struct UtilityBase *UtilityBase;
UBYTE *buf;
void cleanup(char *msg)
{
if (msg) printf("aftest: %s\n", msg);
if (buf) FreeVec(buf);
if (UtilityBase) CloseLibrary((struct Library *)UtilityBase);
if (DiskfontBase) CloseLibrary(DiskfontBase);
exit(0);
}
void openlibs(void)
{
DiskfontBase = OpenLibrary("diskfont.library", 0);
if (!DiskfontBase) cleanup("Cant open diskfont.library!");
UtilityBase = (struct UtilityBase *) OpenLibrary("utility.library", 0);
if (!UtilityBase) cleanup("Cant open utility.library!");
}
void action(void)
{
struct TextFont *font;
struct TextAttr ta;
ta.ta_Name = "Vera Sans Bold Italic.font";
ta.ta_Name = "xhelvetica.font";
ta.ta_YSize = 11;
ta.ta_Style = 0;
ta.ta_Flags = 0;
font = OpenDiskFont(&ta);
if (font)
{
CloseFont(font);
}
}
int main(void)
{
openlibs();
action();
cleanup(0);
return 0; /* keep compiler happy */
}
struct TextFont *OpenDiskFont(struct TextAttr *textAttr) LONG AvailFonts(STRPTR buffer, LONG bufBytes, LONG flags) AFF_MEMORY AFF_DISK AFF_SCALED AFF_TAGGED struct FontContentsHeader *NewFontContents(BPTR fontsLock, STRPTR fontName) struct DiskFont *NewScaledDiskFont(struct TextFont *sourceFont, struct TextAttr *destTextAttr) void DisposeFontContents(struct FontContentsHeader *fontContentsHeader)