簡體   English   中英

figlet 如何適合和弄臟?

[英]How figlet fit and smush?

I am creating a figlet like code in c++ and i am currently doing fiting by inserting null at left side character and find min space and remove minspace and null the character,( for understanding take null as '0' )

" __0          "
"| _|0         "
"| |0    _____ "
"| |0   |_____|"
"| |0      $   "
"|__|0         "

(if any space line i insert null at start) Now here min space is 3 so i remove 3 space and null in that and this code works perfect and smushing by inheriting the fitting class and i will pass the right side char by inserting null like

"       0"
"       0"
" _0____ "
"|0_____|"
"   $0   "
"       0"

它會給出類似的結果

" __       0"
"| _|      0"
"| | _0____ "
"| ||0_____|"
"| |   $0   "
"|__|      0"

Now i will store pos of null and remove it, In next loop check the two character before the null, if any one are HardBlank then i return the function else i smush it in next loop, but above are not smush( not work correctly )由於 HardBlank,所以我想知道 figlet 實際上是如何弄臟的,我從這里下載了 figlet 代碼,但我不明白代碼。

  1. 有沒有比這更好的算法或 figlet 實際上如何進行擬合和弄臟?

歡迎所有建議,在此先感謝。

我很久以前問過這個問題,但我現在為未來的讀者回答這個問題,我寫了一個比這更好的算法,執行以下操作,

字距或擬合:

這個東西的主要部分是修剪,所以讓我們創建一個 function,它接受兩個輸入,figs 是左側的 FIGchar,figc 是右側的 FIGchar。 首先要做的是找到需要從 figs 右側和 figc 左側移除的空間數量,我們可以通過計算 figs 右側和 figc 左側的總空間來輕松找到這一點。 最后取最小的空間計數,這里必須刪除的是實現,

/**
 * @brief trims in a deep
 * 
 * @param figs fig string
 * @param figc fig character
 */
void trim_deep(Figs_type &figs, Figc_type &figc) const
{
    std::vector<size_type> elem;

    for (size_type i = 0; i < figs.size(); ++i)
    {
        int lcount = 0, rcount = 0;

        for (auto itr = figs[i].rbegin(); itr != figs[i].rend(); ++itr)
        {
            if (*itr == ' ')
                ++lcount;
            else
                break;
        }

        for (auto itr = figc[i].begin(); itr != figc[i].end(); ++itr)
        {
            if (*itr == ' ')
                ++rcount;
            else
                break;
        }

        elem.push_back(lcount + rcount);
    }

    size_type space = *std::min_element(elem.begin(), elem.end());

    for (size_type i = 0; i < figs.size(); ++i)
    {
        size_type siz = space;

        while (siz > 0 && figs[i].back() == ' ')
        {
            figs[i].pop_back();
            --siz;
        }

        figc[i].erase(0, siz);
    }
}

糊塗:

通過使用上面的 function 可以很容易地完成這種 smushing,如果它是 smushble,則只有 smush figs 的最右側字符和 figc 的左側字符,這里是實現,

/**
 * @brief smush rules
 * @param lc left character
 * @param rc right character
 * @return smushed character
 */
char_type smush_rules(char_type lc, char_type rc) const
{
    //()
    if (lc == ' ')
    {
        return rc;
    }

    if (rc == ' ')
    {
        return lc;
    }

    //(Equal character smush)
    if (lc == rc)
    {
        return rc;
    }

    //(Underscores smush)
    if (lc == '_' && this->cvt("|/\\[]{}()<>").find(rc) != string_type::npos)
    {
        return rc;
    }

    if (rc == '_' && this->cvt("|/\\[]{}()<>").find(lc) != string_type::npos)
    {
        return lc;
    }

    //(Hierarchy Smushing)
    auto find_class = [](char_type ch) -> size_type
    {
        if (ch == '|')
        {
            return 1;
        }

        if (ch == '/' || ch == '\\')
        {
            return 3;
        }

        if (ch == '[' || ch == ']')
        {
            return 4;
        }

        if (ch == '{' || ch == '}')
        {
            return 5;
        }

        if (ch == '(' || ch == ')')
        {
            return 6;
        }

        return 0;
    };

    size_type c_lc = find_class(lc);
    size_type c_rc = find_class(rc);

    if (c_lc > c_rc)
    {
        return lc;
    }

    if (c_rc > c_lc)
    {
        return rc;
    }

    //(Opposite smush)
    if (lc == '[' && rc == ']')
    {
        return '|';
    }

    if (lc == ']' && rc == '[')
    {
        return '|';
    }

    if (lc == '{' && rc == '}')
    {
        return '|';
    }

    if (lc == '}' && rc == '{')
    {
        return '|';
    }

    if (lc == '(' && rc == ')')
    {
        return '|';
    }

    if (lc == ')' && rc == '(')
    {
        return '|';
    }

    //(Big X smush)
    if (lc == '/' && rc == '\\')
    {
        return '|';
    }

    if (lc == '\\' && rc == '/')
    {
        return 'Y';
    }

    if (lc == '>' && rc == '<')
    {
        return 'X';
    }

    //(universel smush)
    return lc;
}

/**
 * @brief smush algoriths on kerned Fig string and character
 * 
 * @param figs 
 * @param figc 
 */
void smush(Figs_type &figs, Figc_type figc, char_type hb) const
{
    bool smushble = true;

    for (size_type i = 0; i < figs.size(); ++i)
    {
        if (figs[i].size() == 0 || figc[i].size() == 0)
        {
            smushble = false;
        }
        else if ((figs[i].back() == hb) && !(figc[i].front() == hb))
        {
            smushble = false;
        }
    }

    if (smushble)
    {
        for (size_type i = 0; i < figs.size(); ++i)
        {
            char_type val = smush_rules(figs[i].back(), figc[i].front());
            figs[i].pop_back();
            figc[i].erase(0, 1);
            figs[i] += string_type(1, val) + figc[i];
        }
    }
    else
    {
        for (size_type i = 0; i < figs.size(); ++i)
        {
            figs[i] += figc[i];
        }
    }
}

這段代碼是直接從這個文件中復制過來的,所以這里的類型可能會讓人混淆,概述Figs_typeFigc_type就像字符串的向量,其他類型反映在它們的名稱中,repo 可以在這里找到。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM