首页 > 分享 > C++字体库开发之freetype vs harfbuzz sfnt表,字体名称,可变轴八

C++字体库开发之freetype vs harfbuzz sfnt表,字体名称,可变轴八

freetype sfnt表

FT_ULong tableCount = 0;

FT_Error error;

error = FT_Sfnt_Table_Info(face, 0, nullptr, &tableCount);

if (error)

return false;

for (FT_ULong tableIndex = 0; tableIndex < tableCount; ++tableIndex) {

FT_ULong tableTag;

FT_ULong tableLength;

error = FT_Sfnt_Table_Info(face, tableIndex, &tableTag, &tableLength);

if (error)

return false;

FontInfo::Table table{(uint32_t)tableTag, (uint32_t)tableLength};

m_fontInfo.tables.emplace_back(table);

}

字体名称

const FT_UInt numNames = FT_Get_Sfnt_Name_Count(face);

FT_SfntName sfntName;

for (FT_UInt i = 0; i < numNames; ++i) {

if (FT_Get_Sfnt_Name(face, i, &sfntName))

continue;

auto name = sfntNameTranscodeUnicode(sfntName);

if (name.empty())

continue;

if (sfntName.name_id > TT_NAME_ID_VARIATIONS_PREFIX)

continue;

FontInfo::Encode encoding{sfntName.language_id, sfntName.platform_id, sfntName.encoding_id};

FontInfo::Name nameStu{name, encoding};

m_fontInfo.nameTables.at(sfntName.name_id).emplace_back(nameStu);

}

可变轴

FT_CharMap *charMap = face->charmaps;

std::set<int> encodingSet;

for (int i = 0; i < face->num_charmaps; i++) {

auto cur = charMap[i];

if (encodingSet.count(cur->encoding))

continue;

encodingSet.insert(cur->encoding);

FT_Select_Charmap(face, cur->encoding);

unsigned short langID = FT_Get_CMap_Language_ID(cur);

auto weight = getWeight();

auto width = getWidth();

auto slant = getSlant();

auto variations = getSupportVariations();

FontInfo::Encode anEncode{langID, cur->platform_id, cur->encoding_id};

FontInfo::Axes axes{weight, width, slant, variations, anEncode};

m_fontInfo.axes.emplace_back(axes);

}

harfbuzz sfnt表

unsigned int num_tags = 0;

hb_blob_t *blob;

num_tags = hb_face_get_table_tags(m_face, NULL, &num_tags, nullptr);

if (0 == num_tags)

return false;

hb_tag_t *tags = new hb_tag_t[num_tags];

hb_face_get_table_tags(m_face, NULL, &num_tags, tags);

for (unsigned int i = 0; i < num_tags; i++) {

blob = hb_face_reference_table(m_face, tags[i]);

auto tabLen = hb_blob_get_length(blob);

FontInfo::Table table{(uint32_t)tags[i], (uint32_t)tabLen};

m_fontInfo.tables.emplace_back(table);

}

delete[] tags;

字体名称

const auto &allLanguages = FontLanguage::getAllLanguages();

std::map<unsigned short, hb_language_t> languages;

for (const auto &lang : allLanguages) {

languages.emplace(lang.first, hb_language_from_string(lang.second.code.c_str(), lang.second.code.size()));

}

char buf[1024] = {

0,

};

for (int i = HB_OT_NAME_ID_COPYRIGHT; i <= HB_OT_NAME_ID_VARIATIONS_PS_PREFIX; ++i) {

for (const auto &lang : languages) {

unsigned int size = 1024;

if (hb_ot_name_get_utf8(m_face, i, lang.second, &size, buf) > 0) {

FontInfo::Encode encoding{lang.first, 2, 0};

FontInfo::Name nameStu{buf, encoding};

m_fontInfo.nameTables.at(i).emplace_back(nameStu);

}

}

}

可变轴

auto font = hb_font_create(m_face);

unsigned int length;

const int *coords = hb_font_get_var_coords_normalized(font, &length);

if (!coords)

return;

auto denorm_coord = [](hb_ot_var_axis_info_t *axis, int coord) -> float {

float r = coord / 16384.0;

if (coord < 0)

return axis->default_value + r * (axis->default_value - axis->min_value);

else

return axis->default_value + r * (axis->max_value - axis->default_value);

};

unsigned int n_axes;

hb_ot_var_axis_info_t *axes = new hb_ot_var_axis_info_t[length];

FONTVIEW_CLASS(ShareData<hb_ot_var_axis_info_t>) axisInfo(axes, length);

n_axes = length;

hb_ot_var_get_axis_infos(m_face, 0, &n_axes, axes);

auto weight = getWeight();

auto width = getWidth();

auto slant = getSlant();

FontInfo::Encode anEncode{2057, 2, 0};

for (unsigned int i = 0; i < length; ++i) {

char name[20];

unsigned int namelen = 20;

hb_ot_name_get_utf8(m_face, axes[i].name_id, hb_language_from_string("en", 2), &namelen, name);

FontVariation fontVariation(FontVariation::toUInt32((const char *)name));

fontVariation.cur = denorm_coord(&axes[i], coords[i]);

fontVariation.min = axes[i].min_value;

fontVariation.max = axes[i].max_value;

fontVariation.def = axes[i].default_value;

FontInfo::Axes axes{(size_t)weight, width, slant, {std::move(fontVariation)}, anEncode};

m_fontInfo.axes.emplace_back(axes);

}

hb_font_destroy(font);

参考

C++字体库开发之字体列表设计七-CSDN博客 

创作不易,小小的支持一下吧!

相关知识

SquareLine Studio 1.3.1 添加中文字体库并生成嵌入式点阵字体
Android 字体库的使用。引入外部字体
微软字体库下载,探索字体的世界
cad字体库大全2485种字体
字体库大全
移动应用中字体库的设计与实现
【花案图形字体和CAD字体库大全哪个好用】花案图形字体和CAD字体库大全对比-ZOL下载
word字体库
字体库管理
前端工程配置字体库

网址: C++字体库开发之freetype vs harfbuzz sfnt表,字体名称,可变轴八 https://m.huajiangbk.com/newsview742078.html

所属分类:花卉
上一篇: 花体的编程字体是什么
下一篇: 前端开发项目中使用字体库