Show the flags 🇦🇪🇧🇪🇧🇯🇧🇴🇨🇮🇨🇷🇫🇷🇵🇱🇵🇭🇳🇮🇳🇬🇲🇺🇲🇹

For discussions about programming, and for programming questions and advice


Moderator: Forum moderators

Post Reply
User avatar
MochiMoppel
Posts: 1196
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 19 times
Been thanked: 415 times

Show the flags 🇦🇪🇧🇪🇧🇯🇧🇴🇨🇮🇨🇷🇫🇷🇵🇱🇵🇭🇳🇮🇳🇬🇲🇺🇲🇹

Post by MochiMoppel »

This is a little coding exercise, inspired by this recent thread.
Below script opens the text file /usr/share/zoneinfo/iso3166.tab, which contains all ISO 3166 country codes, generates associated country flags and feeds the result to gtkdialog.

Not tremendously useful but maybe entertaining and educational. Lots of flags I've never seen - and some countries I've never heard of :oops:
Flags can be enlarged and copied and they are surprisingly detailed, given that they are not images but characters.
The script uses only 1 call to sed and 1 call to gtkdialog, which makes it relatively fast.

The script requires a font containing colored emojis. Works best with NotoColorEmoji.ttf. This font is included in Bookworm64, F96-CE and many other "modern" Puppies.
emojione-android.ttf is a font installed in Fossapup64 9.5. Produces round flags :shock:
TwemojiMozilla.ttf is shipped with *fox browsers but would not work work when copied to the TTF directory. Don't know why.

Code: Select all

#! /bin/bash
## Adds flag glyphs to country codes of text file /usr/share/zoneinfo/iso3166.tab
## Requires font NotoColorEmoji.ttf

function make_flags {
    H='\xf0\x9f\x87\x'          #common hex part of "regional indicators" (first letter A = codepoint 1F1E6 = \xf0\x9f\x87\xa6)
    sed -r "
    /^#/d                               #remove comments
    s/&/&/                          #pangofy ampersands
    s/(..)\t(.*)/\1 <b>\1<\/b> \2/      #duplicate country codes
    s/^A(.*)/${H}a6\1/ ; s/^(.)A(.*)/\1${H}a6\2/
    s/^B(.*)/${H}a7\1/ ; s/^(.)B(.*)/\1${H}a7\2/
    s/^C(.*)/${H}a8\1/ ; s/^(.)C(.*)/\1${H}a8\2/
    s/^D(.*)/${H}a9\1/ ; s/^(.)D(.*)/\1${H}a9\2/
    s/^E(.*)/${H}aa\1/ ; s/^(.)E(.*)/\1${H}aa\2/
    s/^F(.*)/${H}ab\1/ ; s/^(.)F(.*)/\1${H}ab\2/
    s/^G(.*)/${H}ac\1/ ; s/^(.)G(.*)/\1${H}ac\2/
    s/^H(.*)/${H}ad\1/ ; s/^(.)H(.*)/\1${H}ad\2/
    s/^I(.*)/${H}ae\1/ ; s/^(.)I(.*)/\1${H}ae\2/
    s/^J(.*)/${H}af\1/ ; s/^(.)J(.*)/\1${H}af\2/
    s/^K(.*)/${H}b0\1/ ; s/^(.)K(.*)/\1${H}b0\2/
    s/^L(.*)/${H}b1\1/ ; s/^(.)L(.*)/\1${H}b1\2/
    s/^M(.*)/${H}b2\1/ ; s/^(.)M(.*)/\1${H}b2\2/
    s/^N(.*)/${H}b3\1/ ; s/^(.)N(.*)/\1${H}b3\2/
    s/^O(.*)/${H}b4\1/ ; s/^(.)O(.*)/\1${H}b4\2/
    s/^P(.*)/${H}b5\1/ ; s/^(.)P(.*)/\1${H}b5\2/
    s/^Q(.*)/${H}b6\1/ ; s/^(.)Q(.*)/\1${H}b6\2/
    s/^R(.*)/${H}b7\1/ ; s/^(.)R(.*)/\1${H}b7\2/
    s/^S(.*)/${H}b8\1/ ; s/^(.)S(.*)/\1${H}b8\2/
    s/^T(.*)/${H}b9\1/ ; s/^(.)T(.*)/\1${H}b9\2/
    s/^U(.*)/${H}ba\1/ ; s/^(.)U(.*)/\1${H}ba\2/
    s/^V(.*)/${H}bb\1/ ; s/^(.)V(.*)/\1${H}bb\2/
    s/^W(.*)/${H}bc\1/ ; s/^(.)W(.*)/\1${H}bc\2/
    s/^X(.*)/${H}bd\1/ ; s/^(.)X(.*)/\1${H}bd\2/
    s/^Y(.*)/${H}be\1/ ; s/^(.)Y(.*)/\1${H}be\2/
    s/^Z(.*)/${H}bf\1/ ; s/^(.)Z(.*)/\1${H}bf\2/
    s/../<span font=\"$1\">&<\/span>/   #enclose first 2 letters (=flags) in pango span tag
    " /usr/share/zoneinfo/iso3166.tab
}  ;export -f make_flags

export MAIN_DIALOG='<window title="ISO 3166 country codes" allow-shrink="true" width-request="400" height-request="650" window_position="1">
<vbox>
    <vbox scrollable="true">
        <text use-markup="true" xalign="0" wrap="false" selectable="true" can-focus="false">
            <variable export="false">vTEXT</variable>
            <input>make_flags ${vSPIN:-58}</input>
        </text>
    </vbox>
    <hbox>
        <text label="Flag size:"></text>
        <spinbutton space-expand="false"  range-min="10" range-step="8" range-max="200" >
            <variable>vSPIN</variable>
            <default>58</default>
            <action>refresh:vTEXT</action>
        </spinbutton>
        <button label="Close" has-focus="true" width-request="100"></button>
    </hbox>
</vbox>
</window>'

gtkdialog
iso3166flags.png
iso3166flags.png (73 KiB) Viewed 570 times

[Edit] Clarified font requirements

Last edited by MochiMoppel on Wed Aug 14, 2024 10:49 am, edited 1 time in total.
User avatar
rockedge
Site Admin
Posts: 6352
Joined: Mon Dec 02, 2019 1:38 am
Location: Connecticut,U.S.A.
Has thanked: 2538 times
Been thanked: 2513 times
Contact:

Re: Show the flags 🇦🇪🇧🇪🇧🇯🇧🇴🇨🇮🇨🇷🇫🇷🇵🇱🇵🇭🇳🇮🇳🇬🇲🇺🇲🇹

Post by rockedge »

Thanks @MochiMoppel, the script works great in KLV-Airedale! :thumbup2: :goodpost:

I downloaded the required font from here -> https://github.com/googlefonts/noto-emo ... rEmoji.ttf and placed in /usr/share/fonts/TFF.

Once in the font was in place it looks really good, and very useful!

Attachments
Screenshot.jpg
Screenshot.jpg (47.38 KiB) Viewed 456 times
keniv
Posts: 647
Joined: Mon Jul 13, 2020 2:18 pm
Location: Scotland
Has thanked: 104 times
Been thanked: 67 times

Re: Show the flags 🇦🇪🇧🇪🇧🇯🇧🇴🇨🇮🇨🇷🇫🇷🇵🇱🇵🇭🇳🇮🇳🇬🇲🇺🇲🇹

Post by keniv »

Copied the script to a blank file and named it "flags". Downloaded and installed NotoColorEmoji.ttf as outlined by @rockedge. Right clicked on the "flags" file and chose run in terminal and got a permission denied error. Did this in Bookworm 10.0.7. Any ideas as to what I did wrong?

Regards,

Ken.

User avatar
rockedge
Site Admin
Posts: 6352
Joined: Mon Dec 02, 2019 1:38 am
Location: Connecticut,U.S.A.
Has thanked: 2538 times
Been thanked: 2513 times
Contact:

Re: Show the flags 🇦🇪🇧🇪🇧🇯🇧🇴🇨🇮🇨🇷🇫🇷🇵🇱🇵🇭🇳🇮🇳🇬🇲🇺🇲🇹

Post by rockedge »

you need to make the script executable!
You can use the file manager to do this or the terminal:

Code: Select all

chmod +x <my_script.sh>

or:

Code: Select all

chmod u+x <file>
Post Reply

Return to “Programming”