#!/usr/bin/php
<?php
/*
 * meg4/misc/emoji.php
 *
 * Copyright (C) 2023 bzt
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * @brief A shamefully quick'n'dirty tool to fetch emoticons from UNICODE's website
 *
 */
$f=@fopen("https://unicode.org/emoji/charts/full-emoji-list.html","r");
if(!$f) die("Unable to open UNICODE's website\r\n");
//$from="0"; $to="B";
$from="6"; $to="8";     // interval to fetch, codepoints U+1F?00
$coloumn = 1;           // table coloumn to get icons from
$first = 8;             // first emoji icon id
$code = "";
$img = [];
while(!feof($f)) {
    $line = fgets($f);
    if(!empty($code) && strpos($line, "base64") > 0) { $col++;
        if($col == $coloumn) {
            preg_match("/base64,([^\"]+)/", $line, $ret);
            if(!empty($ret[1])) { $i = hexdec($code) - hexdec("0x1f".$from."00"); $img[$i] = imagecreatefromstring(base64_decode($ret[1])); }
            $code = "";
        }
    }
    if(substr($line, 0, 4) == "</tr") $code == "";
    if(substr($line, 0, 15) == "<td class=\"code" && substr($line, 27, 2) == "1f" && ($line[29]>=$from||$line[29]<$to) && $line[32]=="\"") {
        $code =substr($line, 27, 5); $col = 0;
    }
}
fclose($f);
for($j = $first, $i = 0; $i < hexdec("0x1f".$to."00") - hexdec("0x1f".$from."00"); $i++) if(!empty($img[$i])) $j++;
$t = $j > 253 ? "16" : "8";
$ic = imagecreatefrompng("icons.png");
$N = imagecreatetruecolor(104, 64 + 8 + intval($j / 13) * 8);
imagealphablending($N, 0);
$a = imagecolorallocatealpha($N, 255, 255, 255, 127);
imagesavealpha($N, 1);
imagefill($N, 0, 0, $a);
imagecopy($N, $ic, 0, 0, 0, 0, 104, 64);
imagecopy($N, $ic, 0, 64, 0, 64, $first*8, 8);
imagedestroy($ic);
$num = hexdec("0x1f".$to."00") - hexdec("0x1f".$from."00");
$str="/* generated by emoji.php, do not edit */\n\n#define EMOJI_FIRST 0x1f".$from."00\n#define EMOJI_LAST  0x1f".$to."00\n".
"/* number of icons: ".($j - 4)." */\nextern uint".$t."_t emoji[".$num."];\n#ifdef EMOJI_IMPL\nuint".$t."_t emoji[".$num."] = ".($t=="8"?" ":"")."{ ";
for($j = $first, $i = 0; $i < $num; $i++) { if(empty($img[$i])) $str.=($i?", ":"")."0"; else {
    imagecopyresampled($N, $img[$i], intval($j % 13) * 8, 64 + intval($j / 13) * 8, 0, 0, 8, 8, imagesx($img[$i]), imagesy($img[$i]));
    $str.=sprintf(($i?", ":"")."0x%0".($t=="8"?"2":"4")."x",$j); $j++;
}}
$str.="};\n#endif\n";
imagepng($N, "emoji".$coloumn.".png", 9);
file_put_contents("emoji.h", wordwrap($str, 132));
echo("number of icons: ".$j.", emoji".$coloumn.".png saved.\r\n");
