FontConfig 获取字体属性
#include <fontconfig/fontconfig.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include<string>
using namespace std;
void writeUnicodeToFile(const FcCharSet* charset, const std::string& filename) {
std::ofstream unicodeFile(filename);
if (!unicodeFile.is_open()) {
std::cerr << "Failed to open Unicode output file: " << filename << std::endl;
return;
}
FcChar32 map[FC_CHARSET_MAP_SIZE];
FcChar32 next = 0;
FcChar32 base = FcCharSetFirstPage(charset, map, &next);
if (base == FC_CHARSET_DONE) {
std::cout << "字符集为空!" << std::endl;
return;
}
do {
for (int i = 0; i < FC_CHARSET_MAP_SIZE; ++i) {
FcChar32 bits = map[i];
if (bits == 0)
continue;
for (int j = 0; j < 32; ++j) {
if (bits & (1u << j)) {
FcChar32 unicodeChar = base + i * 32 + j;
if (unicodeChar == 0xFFFFFFFF || unicodeChar > 0x10FFFF) {
std::cerr << "检测到无效码点 U+" << std::hex << unicodeChar << ", 跳过该码点。" << std::endl;
continue;
}
unicodeFile << "U+" << std::hex << std::setw(4) << std::setfill('0') << unicodeChar << "\n";
}
}
}
base = FcCharSetNextPage(charset, map, &next);
} while (base != FC_CHARSET_DONE);
unicodeFile.close();
}
int main() {
if (!FcInit()) {
std::cerr << "Failed to initialize FontConfig!" << std::endl;
return 1;
}
FcPattern* pat = FcPatternCreate();
if (!pat) {
std::cerr << "Failed to create pattern!" << std::endl;
return 1;
}
const char* properties[] = {
FC_FAMILY, FC_STYLE, FC_FILE, FC_CHARSET, FC_SLANT, FC_WEIGHT, FC_SIZE,
FC_ASPECT, FC_PIXEL_SIZE, FC_SPACING, FC_FOUNDRY, FC_ANTIALIAS, FC_HINTING,
FC_HINT_STYLE, FC_VERTICAL_LAYOUT, FC_AUTOHINT, FC_WIDTH, FC_INDEX,
FC_OUTLINE, FC_SCALABLE, FC_COLOR, FC_VARIABLE, FC_SYMBOL, FC_DPI, FC_RGBA,
FC_MINSPACE, FC_FONTVERSION, FC_FULLNAME, FC_FAMILYLANG, FC_STYLELANG,
FC_FULLNAMELANG, FC_CAPABILITY, FC_FONTFORMAT, FC_EMBOLDEN, FC_EMBEDDED_BITMAP,
FC_DECORATIVE, FC_LCD_FILTER, FC_FONT_FEATURES, FC_FONT_VARIATIONS, FC_NAMELANG,
FC_POSTSCRIPT_NAME, FC_FONT_HAS_HINT, FC_ORDER, FC_DESKTOP_NAME, FC_NAMED_INSTANCE,
FC_FONT_WRAPPER
};
FcObjectSet* os = FcObjectSetCreate();
for (const char* prop : properties) {
FcObjectSetAdd(os, prop);
}
FcFontSet* fs = FcFontList(NULL, pat, os);
if (!fs) {
std::cerr << "Failed to get font list!" << std::endl;
FcPatternDestroy(pat);
FcObjectSetDestroy(os);
return 1;
}
std::ofstream outFile("D:/code_pdf/out/font_properties.txt");
if (!outFile.is_open()) {
std::cerr << "Failed to open output file!" << std::endl;
FcFontSetDestroy(fs);
FcObjectSetDestroy(os);
FcPatternDestroy(pat);
return 1;
}
for (int i = 0; i < fs->nfont; ++i) {
FcPattern* font = fs->fonts[i];
outFile << "Font " << i + 1 << ":\n";
for (const char* prop : properties) {
FcValue value;
if (FcPatternGet(font, prop, 0, &value) == FcResultMatch) {
outFile << " " << prop << ": ";
switch (value.type) {
case FcTypeInteger:
outFile << value.u.i;
break;
case FcTypeDouble:
outFile << value.u.d;
break;
case FcTypeString:
outFile << value.u.s;
break;
case FcTypeBool:
outFile << (value.u.b ? "True" : "False");
break;
case FcTypeCharSet: {
const FcCharSet* charset = value.u.c;
int count = FcCharSetCount(charset);
outFile << "CharSet (Count: " << count << ")";
if (count != 0)
{
std::string unicodeFilename = "D:/code_pdf/out/font_" + std::to_string(i + 1) + "_unicode.txt";
writeUnicodeToFile(charset, unicodeFilename);
outFile << " (Unicode saved to " << unicodeFilename << ")";
}
break;
}
case FcTypeMatrix:
outFile << "Matrix";
break;
case FcTypeLangSet:
outFile << "LangSet";
break;
case FcTypeRange:
outFile << "Range";
break;
case FcTypeFTFace:
outFile << "FTFace";
break;
default:
outFile << "Unknown";
break;
}
outFile << "\n";
}
}
outFile << "\n";
}
outFile.close();
FcFontSetDestroy(fs);
FcObjectSetDestroy(os);
FcPatternDestroy(pat);
FcFini();
std::cout << "Font properties have been written to font_properties.txt" << std::endl;
std::cout << "Unicode characters for each font have been saved to separate files." << std::endl;
return 0;
}
FontConfig 判断字体是否包含unicode
#if 0
#include <iostream>
#include <podofo/podofo.h>
using namespace std;
using namespace PoDoFo;
void HelloWorld(const string_view& filename) {
PdfMemDocument document;
PdfPainter painter;
try {
auto& page = document.GetPages().CreatePage(PdfPageSize::A4);
painter.SetCanvas(page);
PdfFontSearchParams searchParams;
searchParams.AutoSelect = PdfFontAutoSelectBehavior::Standard14Alt;
PdfFont* font = document.GetFonts().SearchFont("YuMincho-Regular", searchParams);
if (!font)
throw runtime_error("Font not found");
auto& metrics = font->GetMetrics();
cout << "Font Name: " << metrics.GetFontName() << endl;
cout << "Font Family: " << metrics.GetFontFamilyName() << endl;
painter.TextState.SetFont(*font, 18);
painter.DrawText("ABCDEFGHIKLMNOPQRSTVXYZ", 56.69, page.GetRect().Height - 56.69);
try {
painter.DrawText("АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЫЭЮЯ", 56.69, page.GetRect().Height - 80);
}
catch (PdfError& err) {
if (err.GetCode() == PdfErrorCode::InvalidFontData)
cout << "WARNING: Font does not support Cyrillic characters" << endl;
}
painter.FinishDrawing();
document.Save(filename);
}
catch (PdfError& err) {
painter.FinishDrawing();
throw;
}
}
int main() {
try {
HelloWorld("output.pdf");
cout << "PDF created successfully: output.pdf" << endl;
}
catch (PdfError& err) {
err.PrintErrorMsg();
return (int)err.GetCode();
}
return 0;
}
#endif