首页 > 编程语言 >C++字体库开发之fontconfig使用五

C++字体库开发之fontconfig使用五

时间:2024-07-21 15:26:57浏览次数:16  
标签:fonts 00000000 cache C++ fontconfig 字体库 FC config dir

代码 


#include <cassert>
#include <algorithm>
#include "fontconfig/fontconfig.h"
#include <stdexcept>
#include <iostream>

#define HAS_FALLBACK_CONFIGURATION

enum FontStyle : uint16_t {
    Regular = 0,
    Italic = 0x01,
    Bold = 0x02,
};

enum class FontConfigSearchFlags : uint16_t {
    None = 0,
    MatchPostScriptName =
    0x01,  ///< Match postscript font name. The default is match family name. This search may be more specific
};

struct FontConfigSearchParams {
    uint16_t Style;
    uint16_t Flags = 0;
};

FcConfig* m_FcConfig;

void createDefaultConfig()
{
#ifdef _WIN32
    const char* fontconf =
        R"(<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
    <dir>WINDOWSFONTDIR</dir>
    <dir>WINDOWSUSERFONTDIR</dir>
    <dir prefix="xdg">fonts</dir>
    <cachedir>LOCAL_APPDATA_FONTCONFIG_CACHE</cachedir>
    <cachedir prefix="xdg">fontconfig</cachedir>
</fontconfig>
)";
#elif __ANDROID__
    // On android fonts are located in /system/fonts
    const char* fontconf =
        R"(<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
    <dir>/system/fonts</dir>
    <dir prefix="xdg">fonts</dir>
    <cachedir prefix="xdg">fontconfig</cachedir>
</fontconfig>
)";
#elif __LINUX__
    const char* fontconf =
        R"(<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
    <dir>/usr/share/fonts</dir>
    <dir>/usr/local/share/fonts</dir>
    <dir>/usr/X11R6/lib/X11/fonts</dir>
    <dir>/usr/X11/lib/X11/fonts</dir>
    <dir>/usr/lib/X11/fonts</dir>
    <dir prefix="xdg">fonts</dir>
    <cachedir prefix="xdg">fontconfig</cachedir>
</fontconfig>
)";
#elif __APPLE__
    // Fonts location https://stackoverflow.com/a/2557291/213871
    const char* fontconf =
        R"(<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
    <dir>/System/Library/Fonts</dir>
    <dir>/Library/Fonts</dir>
    <dir>~/Library/Fonts</dir>
    <dir>/System/Library/Assets/com_apple_MobileAsset_Font3</dir>
    <dir>/System/Library/Assets/com_apple_MobileAsset_Font4"</dir>
    <dir prefix="xdg">fonts</dir>
    <cachedir prefix="xdg">fontconfig</cachedir>
</fontconfig>
)";
#endif

#ifdef HAS_FALLBACK_CONFIGURATION
    // Implement the fallback as discussed in fontconfig mailing list
    // https://lists.freedesktop.org/archives/fontconfig/2022-February/006883.html

    auto config = FcConfigCreate();
    if (config == nullptr)
        throw std::runtime_error("Could not allocate font config");

    // Manually try to load the config to determine
    // if a system configuration exists. Tell FontConfig
    // to not complain if it doesn't
    (void)FcConfigParseAndLoad(config, nullptr, FcFalse);

    auto configFiles = FcConfigGetConfigFiles(config);
    if (FcStrListNext(configFiles) == nullptr) {
        // No system config found, supply a fallback configuration
        if (!FcConfigParseAndLoadFromMemory(config, (const FcChar8*)fontconf, true)) {
            FcConfigDestroy(config);
            throw std::runtime_error("Could not parse font config");
        }

        // Load fonts for the config
        if (!FcConfigBuildFonts(config)) {
            FcConfigDestroy(config);
            throw std::runtime_error("Could not load fonts in fontconfig");
        }

        m_FcConfig = config;
    }
    else {
        // Destroy the temporary config
        FcStrListDone(configFiles);
        FcConfigDestroy(config);
#endif
        // Default initialize a local FontConfig configuration
        // http://mces.blogspot.com/2015/05/how-to-use-custom-application-fonts.html
        m_FcConfig = FcInitLoadConfigAndFonts();
        assert(m_FcConfig != nullptr);

#ifdef HAS_FALLBACK_CONFIGURATION
    }
#endif
}

bool getFontInfo(FcPattern* font, std::string& fontFamily, std::string& fontPath, uint16_t& style)
{
    FcChar8* family = nullptr;
    FcChar8* path = nullptr;
    int      slant;
    int      weight;
    style = FontStyle::Regular;

    if (FcPatternGetString(font, FC_FAMILY, 0, &family) == FcResultMatch) {
        fontFamily = reinterpret_cast<char*>(family);
        if (FcPatternGetString(font, FC_FILE, 0, &path) == FcResultMatch) {
            fontPath = reinterpret_cast<char*>(path);

            if (FcPatternGetInteger(font, FC_SLANT, 0, &slant) == FcResultMatch) {
                if (slant == FC_SLANT_ITALIC || slant == FC_SLANT_OBLIQUE)
                    style |= FontStyle::Italic;

                if (FcPatternGetInteger(font, FC_WEIGHT, 0, &weight) == FcResultMatch) {
                    if (weight >= FC_WEIGHT_BOLD)
                        style |= FontStyle::Bold;

                    return true;
                }
            }
            // free( file );
        }
        // free( family );
    }

    return false;
}

std::string SearchFontPath(const std::string_view fontPattern, const FontConfigSearchParams& params, unsigned& faceIndex)
{
    FcPattern* pattern;
    FcPattern* matched;
    FcResult   result = FcResultMatch;
    FcValue    value;

    pattern = FcPatternCreate();
    if (pattern == nullptr)
        return ("FcPatternCreate returned NULL");

    // Build a pattern to search using postscript name, bold and italic
    if (params.Flags != (uint16_t)FontConfigSearchFlags::MatchPostScriptName)
        FcPatternAddString(pattern, FC_FAMILY, (const FcChar8*)fontPattern.data());
    else
        FcPatternAddString(pattern, FC_POSTSCRIPT_NAME, (const FcChar8*)fontPattern.data());

    if (params.Style) {
        bool isItalic = (params.Style & FontStyle::Italic) == FontStyle::Italic;
        bool isBold = (params.Style & FontStyle::Bold) == FontStyle::Bold;

        FcPatternAddInteger(pattern, FC_WEIGHT, (isBold ? FC_WEIGHT_BOLD : FC_WEIGHT_MEDIUM));
        FcPatternAddInteger(pattern, FC_SLANT, (isItalic ? FC_SLANT_ITALIC : FC_SLANT_ROMAN));
    }

    // Follow fc-match procedure which proved to be more reliable
    // https://github.com/freedesktop/fontconfig/blob/e291fda7d42e5d64379555097a066d9c2c4efce3/fc-match/fc-match.c#L188
    if (!FcConfigSubstitute(m_FcConfig, pattern, FcMatchPattern)) {
        FcPatternDestroy(pattern);
        faceIndex = 0;
        return {};
    }

    FcDefaultSubstitute(pattern);

    std::string path;
    matched = FcFontMatch(m_FcConfig, pattern, &result);
    if (result != FcResultNoMatch) {
        (void)FcPatternGet(matched, FC_FILE, 0, &value);
        path = reinterpret_cast<const char*>(value.u.s);
        (void)FcPatternGet(matched, FC_INDEX, 0, &value);
        faceIndex = (unsigned)value.u.i;
    }

    FcPatternDestroy(pattern);
    FcPatternDestroy(matched);

#if _WIN32
    // Font config in Windows returns unix conventional path
    // separator. Fix it
    std::replace(path.begin(), path.end(), '/', '\\');
#endif
    return path;
}

void testSingleFont(FcPattern* font)
{
    std::string            fontFamily;
    std::string            fontPath;
    uint16_t               style;
    FontConfigSearchParams fcParams;

    if (getFontInfo(font, fontFamily, fontPath, style)) {
        unsigned faceIndex;
        fcParams.Style = style;
        fontPath = SearchFontPath(fontFamily, fcParams, faceIndex);
        if (fontPath.length() != 0) {
            std::cout << "fontPath:" << fontPath << ",family:" << fontFamily << ",style:" << style << std::endl;
        }
    }

    // 简单打印
    FcPatternPrint(font);

    //自定义格式打印
    const FcChar8* format = (const FcChar8*)"%{=fclist}\n";
    //const FcChar8* format = (const FcChar8*)"%{=unparse}\n";
    //const FcChar8* format = (const FcChar8*)"%{=fcmatch}\n";
    auto s = FcPatternFormat(font, format);
    std::cout << s << "\n";
    FcStrFree(s);
}

// 测试字体安装目录
void listInstallFonts() {
    // Get all installed fonts
    auto pattern = FcPatternCreate();
    auto objectSet = FcObjectSetBuild(FC_FAMILY, FC_STYLE, FC_FILE, FC_SLANT, FC_WEIGHT, nullptr);
    auto fontSet = FcFontList(nullptr, pattern, objectSet);

    FcObjectSetDestroy(objectSet);
    FcPatternDestroy(pattern);

    if (fontSet == nullptr) {
        printf("Unable to search for fonts");
        return;
    }

    std::cout << "font count " << fontSet->nfont << std::endl;
    for (int i = 0; i < fontSet->nfont; i++)
        testSingleFont(fontSet->fonts[i]);

    FcFontSetDestroy(fontSet);
}


static FcStrSet* processed_dirs;

static int
scanDirs(FcStrList* list, FcConfig* config, FcBool force, FcBool really_force, FcBool verbose, FcBool error_on_no_fonts, int* changed)
{
    int		    ret = 0;
    const FcChar8* dir;
    FcStrSet* subdirs;
    FcStrList* sublist;
    FcCache* cache;
    struct stat	    statb;
    FcBool	    was_valid, was_processed = FcFalse;
    int		    i;
    const FcChar8* sysroot = FcConfigGetSysRoot(config);

    /*
     * Now scan all of the directories into separate databases
     * and write out the results
     */
    while ((dir = FcStrListNext(list)))
    {
        if (verbose)
        {
            if (sysroot)
                printf("[%s]", sysroot);
            printf("%s: ", dir);
            fflush(stdout);
        }

        if (FcStrSetMember(processed_dirs, dir))
        {
            if (verbose)
                printf(("skipping, looped directory detected\n"));
            continue;
        }

        FcChar8* rooted_dir = NULL;
        if (sysroot)
        {
            rooted_dir = FcStrPlus(sysroot, dir);
        }
        else {
            rooted_dir = FcStrCopy(dir);
        }

        if (stat((char*)rooted_dir, &statb) == -1)
        {
            switch (errno) {
            case ENOENT:
            case ENOTDIR:
                if (verbose)
                    printf(("skipping, no such directory\n"));
                break;
            default:
                fprintf(stderr, "\"%s\": ", dir);
                perror("");
                ret++;
                break;
            }
            FcStrFree(rooted_dir);
            rooted_dir = NULL;
            continue;
        }

        FcStrFree(rooted_dir);
        rooted_dir = NULL;

        if ((statb.st_mode & 0xF000) != 0x4000)
        {
            fprintf(stderr,  ("\"%s\": not a directory, skipping\n"), dir);
            continue;
        }
        was_processed = FcTrue;

        if (really_force)
        {
            FcDirCacheUnlink(dir, config);
        }

        cache = NULL;
        was_valid = FcFalse;
        if (!force) {
            //if (FcFileIsDir(arg))
            //    cache = FcDirCacheLoad(arg, config, &rooted_dir);
            //else
            //    cache = FcDirCacheLoadFile(arg, &statb);

            cache = FcDirCacheLoad(dir, config, NULL);
            if (cache)
                was_valid = FcTrue;
        }

        if (!cache)
        {
            (*changed)++;
            cache = FcDirCacheRead(dir, FcTrue, config);
            if (!cache)
            {
                fprintf(stderr,  ("\"%s\": scanning error\n"), dir);
                ret++;
                continue;
            }
        }

        if (was_valid)
        {
            if (verbose)
                printf(("skipping, existing cache is valid: %d fonts, %d dirs\n"),
                    FcCacheNumFont(cache), FcCacheNumSubdir(cache));
        }
        else
        {
            if (verbose)
                printf(("caching, new cache contents: %d fonts, %d dirs\n"),
                    FcCacheNumFont(cache), FcCacheNumSubdir(cache));

            if (!FcDirCacheValid(dir))
            {
                fprintf(stderr,  ("%s: failed to write cache\n"), dir);
                (void)FcDirCacheUnlink(dir, config);
                ret++;
            }
        }

        subdirs = FcStrSetCreate();
        if (!subdirs)
        {
            fprintf(stderr,  ("%s: Can't create subdir set\n"), dir);
            ret++;
            FcDirCacheUnload(cache);
            continue;
        }
        for (i = 0; i < FcCacheNumSubdir(cache); i++)
            FcStrSetAdd(subdirs, FcCacheSubdir(cache, i));

        FcDirCacheUnload(cache);

        sublist = FcStrListCreate(subdirs);
        FcStrSetDestroy(subdirs);
        if (!sublist)
        {
            fprintf(stderr,  ("%s: Can't create subdir list\n"), dir);
            ret++;
            continue;
        }
        FcStrSetAdd(processed_dirs, dir);
        ret += scanDirs(sublist, config, force, really_force, verbose, error_on_no_fonts, changed);
        FcStrListDone(sublist);
    }

    if (error_on_no_fonts && !was_processed)
        ret++;
    return ret;
}

static FcBool
cleanCacheDirectories(FcConfig* config, FcBool verbose)
{
    FcStrList* cache_dirs = FcConfigGetCacheDirs(config);
    FcChar8* cache_dir;
    FcBool	ret = FcTrue;

    if (!cache_dirs)
        return FcFalse;
    while ((cache_dir = FcStrListNext(cache_dirs)))
    {
        if (!FcDirCacheClean(cache_dir, verbose))
        {
            ret = FcFalse;
            break;
        }
    }
    FcStrListDone(cache_dirs);
    return ret;
}

// 测试字体缓存目录
int testFontCache(const char* fontdirs) {
    //if (!FcFileIsDir((const FCChar8*)fontdirs))
    //    return 0;

        FcStrSet* dirs;
        FcStrList* list;
        FcBool    	verbose = FcFalse;
        FcBool	force = FcFalse;
        FcBool	really_force = FcFalse;
        FcBool	systemOnly = FcFalse;
        FcBool	error_on_no_fonts = FcFalse;
        FcConfig* config;
        FcChar8* sysroot = NULL;
        int		changed;
        int		ret;

        if (systemOnly)
            FcConfigEnableHome(FcFalse);
        if (sysroot)
        {
            FcConfigSetSysRoot(NULL, sysroot);
            FcStrFree(sysroot);
            config = FcConfigGetCurrent();
        }
        else
        {
            config = FcInitLoadConfig();
        }
        if (!config)
        {
            fprintf(stderr,  ("%s: Can't initialize font config library\n"), fontdirs);
            return 1;
        }
        FcConfigSetCurrent(config);

        if (fontdirs)
        {
            dirs = FcStrSetCreate();
            if (!dirs)
            {
                fprintf(stderr,  ("%s: Can't create list of directories\n"),
                    fontdirs);
                return 1;
            }

            if (!FcStrSetAddFilename(dirs, (FcChar8*)fontdirs))
            {
                fprintf(stderr,  ("%s: Can't add directory\n"), fontdirs);
                return 1;
            }
            list = FcStrListCreate(dirs);
            FcStrSetDestroy(dirs);
        }
        else
            list = FcConfigGetFontDirs(config);

        if ((processed_dirs = FcStrSetCreate()) == NULL) {
            fprintf(stderr,  ("Out of Memory\n"));
            return 1;
        }

        if (verbose)
        {
            const FcChar8* dir;

            printf("Font directories:\n");
            while ((dir = FcStrListNext(list)))
            {
                printf("\t%s\n", dir);
            }
            FcStrListFirst(list);
        }
        changed = 0;
        ret = scanDirs(list, config, force, really_force, verbose, error_on_no_fonts, &changed);
        FcStrListDone(list);

        /*
         * Try to create CACHEDIR.TAG anyway.
         * This expects the fontconfig cache directory already exists.
         * If it doesn't, it won't be simply created.
         */
        FcCacheCreateTagFile(config);

        FcStrSetDestroy(processed_dirs);

        cleanCacheDirectories(config, verbose);

        FcConfigDestroy(config);
        FcFini();
        /*
         * Now we need to sleep a second  (or two, to be extra sure), to make
         * sure that timestamps for changes after this run of fc-cache are later
         * then any timestamps we wrote.  We don't use gettimeofday() because
         * sleep(3) can't be interrupted by a signal here -- this isn't in the
         * library, and there aren't any signals flying around here.
         */
         /* the resolution of mtime on FAT is 2 seconds */
        //if (changed)
        //    sleep(2);
        if (verbose)
            printf("%s: %s\n", fontdirs, ret ? "failed" : "succeeded");
        return ret;
}

// 配置列表
void testConfigList() {
    FcConfig* config;
    FcConfigFileInfoIter iter;
    config = FcConfigGetCurrent();
    FcConfigFileInfoIterInit(config, &iter);
    do
    {
        FcChar8* name, * desc;
        FcBool enabled;

        if (FcConfigFileInfoIterGet(config, &iter, &name, &desc, &enabled))
        {
            printf("%c %s: %s\n", enabled ? '+' : '-', name, desc);
            FcStrFree(name);
            FcStrFree(desc);
        }
    } while (FcConfigFileInfoIterNext(config, &iter));

    FcFini();
}

// 字体查找
void queryFont(const FcChar8 *fontdir) {

    auto fs = FcFontSetCreate();
    // FcFreeTypeQuery
    if (!FcFreeTypeQueryAll(fontdir, 0, NULL, NULL, fs))
    {
        fprintf(stderr, "Can't query face %u of font file %s\n", -1, fontdir);
        return;
    }

    for (int i = 0; i < fs->nfont; i++)
    {
        FcPattern* pat = fs->fonts[i];
        FcPatternPrint(pat);
        //FcPatternDel(pat, FC_CHARSET);
        //FcPatternDel(pat, FC_LANG);
    }
    FcFontSetDestroy(fs);

    FcFini();
}


// 字体目录解析
void scanFont(const FcChar8* file) {

    auto fs = FcFontSetCreate();

    if (!FcFileIsDir(file))
        FcFileScan(fs, NULL, NULL, NULL, file, FcTrue);
    else
    {
        FcStrSet* dirs = FcStrSetCreate();
        FcStrList* strlist = FcStrListCreate(dirs);
        do
        {
            FcDirScan(fs, dirs, NULL, NULL, file, FcTrue);
        } while ((file = FcStrListNext(strlist)));
        FcStrListDone(strlist);
        FcStrSetDestroy(dirs);
    }

    for (int i = 0; i < fs->nfont; i++)
    {
        FcPattern* pat = fs->fonts[i];
        FcPatternPrint(pat);
        //FcPatternDel(pat, FC_CHARSET);
        //FcPatternDel(pat, FC_LANG);
    }
    FcFontSetDestroy(fs);

    FcFini();
}

void test()
{
    //listInstallFonts();
    //testFontCache("C:\\Fonts");
    //testConfigList();
    //queryFont((const FcChar8*)"C:\\Fonts\\LiberationSans.ttc");
    //scanFont((const FcChar8*)"C:\\Fonts");
    // 待续......
    // FcConfigSetCurrent FcConfigUptoDate  FcConfigGetCache  FcConfigGetRescanInterval  FcConfigGetFonts   FcConfigAppFontAddFile   FcConfigAppFontAddDir  
    // FcConfigAppFontClear     FcDirSave   FcDirCacheRescan    FcFontSetList   FcAtomicLock    FcFontSetMatch  FcFontRenderPrepare     FcFontSetSort   
    // FcFontSetSortDestroy     FcPatternHash
}
输出


    fontPath:C:\Windows\fonts\GOTHIC.TTF,family:Century Gothic,style:0
Pattern has 5 elts (size 16)
        family: "Century Gothic"(s)
        style: "Regular"(s) "Normal"(s) "obyčejné"(s) "Standard"(s) "Κανονικά"(s) "Normaali"(s) "Normál"(s) "Normale"(s) "Standaard"(s) "Normalny"(s) "Обычный"(s) "Normálne"(s) "Navadno"(s) "Arrunta"(s)
        slant: 0(i)(s)
        weight: 80(f)(s)
        file: "C:/Windows/fonts\GOTHIC.TTF"(s)

C:/Windows/fonts\GOTHIC.TTF: Century Gothic:style=Regular,Normal,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta:slant=0:weight=80

Pattern has 26 elts (size 32)
    family: "Liberation Sans"(s)
    familylang: "en"(s)
    style: "Bold"(s)
    stylelang: "en"(s)
    fullname: "Liberation Sans Bold"(s)
    fullnamelang: "en"(s)
    slant: 0(i)(s)
    weight: 200(f)(s)
    width: 100(f)(s)
    foundry: "1ASC"(s)
    file: "C:\Fonts\LiberationSans-Bold.ttf"(s)
    index: 0(i)(s)
    outline: True(s)
    scalable: True(s)
    charset:
    0000: 00000000 ffffffff ffffffff 7fffffff 00000000 ffffffff ffffffff ffffffff
    0001: ffffffff ffffffff ffffffff ffffffff 00040000 00000000 00000000 fc000000
    0002: 0f000000 00000000 00000000 00000000 00000000 00000000 3f0002c0 00000000
    0003: 00000000 00000000 00000000 40000000 ffffd7f0 fffffffb 00007fff 00000000
    0004: ffffffff ffffffff ffffffff 000c0000 00030000 00000000 00000000 00000000
    001e: 00000000 00000000 00000000 00000000 0000003f 00000000 00000000 000c0000
    0020: 7fbb0000 560d0047 00000010 80000000 00000000 00001098 00000000 00000000
    0021: 00480020 00004044 78000000 00000000 003f0000 00000100 00000000 00000000
    0022: c6268044 00000a00 00000100 00000033 00000000 00000000 00000000 00000000
    0023: 00010004 00000003 00000000 00000000 00000000 00000000 00000000 00000000
    0025: 11111005 10101010 ffff0000 00001fff 000f1111 14041c03 03008c10 00000040
    0026: 00000000 1c000000 00000005 00001c69 00000000 00000000 00000000 00000000
    00f0: 00000026 00000000 00000000 00000000 00000000 00000000 00000000 00000000
    00fb: 00000006 00000000 00000000 00000000 00000000 00000000 00000000 00000000
(s)
    lang: aa|af|av|ay|be|bg|bi|br|bs|ca|ce|ch|co|cs|cy|da|de|el|en|eo|es|et|eu|fi|fj|fo|fr|fur|fy|gd|gl|gv|ho|hr|hu|ia|id|ie|ik|io|is|it|ki|kl|kum|la|lb|lez|lt|lv|mg|mh|mk|mo|mt|nb|nds|nl|nn|no|nr|nso|ny|oc|om|os|pl|pt|rm|ro|ru|se|sel|sk|sl|sma|smj|smn|so|sq|sr|ss|st|sv|sw|tk|tl|tn|tr|ts|uk|uz|vo|vot|wa|wen|wo|xh|yap|zu|an|crh|csb|fil|hsb|ht|jv|kj|ku-tr|kwm|lg|li|ms|na|ng|pap-an|pap-aw|rn|rw|sc|sg|sn|su|za(s)
    fontversion: 70123(i)(s)
    capability: "otlayout:DFLT otlayout:cyrl otlayout:grek otlayout:latn"(s)
    fontformat: "TrueType"(s)
    decorative: False(s)
    postscriptname: "LiberationSans-Bold"(s)
    color: False(s)
    symbol: False(s)
    variable: False(s)
    fonthashint: True(s)
    order: 0(i)(s)

参考

C++字体库开发之字符显示四-CSDN博客

https://github.com/freedesktop/fontconfig

https://github.com/ShiftMediaProject/fontconfig

https://github.com/benoitkugler/textlayout

https://github.com/benoitkugler/textprocessing

https://github.com/go-text/typesetting


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

标签:fonts,00000000,cache,C++,fontconfig,字体库,FC,config,dir
From: https://blog.csdn.net/qq_30220519/article/details/140576759

相关文章

  • c++中的文件操作
    前言hello,大家好啊,这里是文宇,不是文字,是文宇哦。C++中的文件操作是用于在程序中读取、写入和操作文件的一种重要功能。文件操作允许程序直接与外部文件进行交互,这对于数据的存储和读取非常有用。在C++中,文件操作主要通过iostream库中的fstream类来实现。fstream类提供了一种......
  • Linux下C++静态链接库的生成以及使用
    目录一.前言二.生成静态链接库三.使用静态链接库一.前言这篇文章简单讨论一下Linux下如何使用gcc/g++生成和使用C++静态链接库(.a文件)。二.生成静态链接库先看下目录结构然后看下代码//demo.h#ifndefDEMO_H#defineDEMO_H#include<string>classDemo{p......
  • C++文件操作-文本文件-读文件
    第一种#include<iostream>//1、包含头文件fstream#include<fstream>usingnamespacestd;voidtest01(){ //2、创建流对象 ifstreamifs; //3、打开文件并且判断是否打开成功 ifs.open("test.txt",ios::in); if(!ifs.is_open()) { cout<<"文件打开失......
  • C++ 鼠标轨迹API【神诺科技SDK】
    一.鼠标轨迹模拟简介传统的鼠标轨迹模拟依赖于简单的数学模型,如直线或曲线路径。然而,这种方法难以捕捉到人类操作的复杂性和多样性。AI大模型的出现,使得神诺科技 能够通过深度学习技术,学习并模拟更自然的鼠标移动行为。二.鼠标轨迹算法实现AI大模型通过学习大量的人类鼠标操......
  • C++鼠标轨迹API - 神诺科技SDK
    一.鼠标轨迹模拟简介传统的鼠标轨迹模拟依赖于简单的数学模型,如直线或曲线路径。然而,这种方法难以捕捉到人类操作的复杂性和多样性。AI大模型的出现,使得神诺科技 能够通过深度学习技术,学习并模拟更自然的鼠标移动行为。二.鼠标轨迹算法实现AI大模型通过学习大量的人类鼠标操......
  • C++之函数
    1概述作用:将一段经常使用的代码封装起来,减少重复代码一个较大的程序,一般分为若干个程序块,每个模块实现特定的功能。2函数的定义函数定义一般主要有5个步骤:返回值类型函数名参数列表函数体语句return表达式语法:返回值类型函数名(参数列表){函数体语句......
  • 【C++】类和对象(中篇)
    类和对象一.类的默认成员函数二.构造函数三.析构函数四.拷贝构造函数五.运算符重载六.赋值运算符重载七.日期类的实现1.Date.h2.Date.cpp2.test.cpp八.取地址运算符重载1.const成员函数2.取地址运算符重载一.类的默认成员函数  默认成员函数就是用户不用实现的函......
  • C++自学笔记1(c++原理)
    Why学习C++?因为C++直接控制硬件。C++的工作原理是:C++代码,代码交给编译器来编译,编译器将代码转换成目标平台的机器码。(机器码:你操作的设备上CPU实际执行的二进制命令)所以我们使用C++可以控制CPU上每条进程的指令。C++可以运用在哪些平台上?几乎任何平台,只要你找到对应的编译器......
  • C++自学笔记2(变量与函数)
    变量(数据)编程的本质实际上就是操纵数据,任何种类的数据。我们想对数据进行修改、读取、写入,我们将数据放在一个叫做变量的东西,他可以允许我们对数据命名。我们需要利用数据、存放数据。当我们创造一个变量时,他被存放在两个地方“堆”和“栈”(再挖个坑)在物理意义上我们的数据......
  • C++自学笔记3(头文件)
    头文件就是各个函数申明存放地(也可以放结构体)现在你有main.c文件、A.c、B.c文件A.c包含A()函数B.c包含B()函数如果想要在main.c中使用A()B()两个函数,那么我们必须在intmain()之上进行申明_________________________________________________________________________//mai......