File indexing completed on 2025-03-02 04:03:56

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #pragma once
0008 
0009 #include <vector>
0010 #include <map>
0011 
0012 #include <QString>
0013 #include <QUrl>
0014 #include <QDir>
0015 #include <QJsonObject>
0016 #include <QJsonArray>
0017 
0018 #include "emoji_data.hpp"
0019 
0020 
0021 namespace glaxnimate::emoji {
0022 
0023 struct EmojiSetDirectory
0024 {
0025     static constexpr const int Scalable = -1;
0026     QString path;
0027     QString format;
0028     int size = Scalable;
0029 
0030     static EmojiSetDirectory load(const QJsonObject& json)
0031     {
0032         EmojiSetDirectory obj;
0033         obj.path = json["path"].toString();
0034         obj.format = json["format"].toString();
0035         if ( obj.format != "svg" )
0036             obj.size = json["size"].toInt();
0037         return obj;
0038     }
0039 };
0040 
0041 struct EmojiSetDownload
0042 {
0043     QUrl url;
0044     std::map<int, EmojiSetDirectory> paths;
0045 
0046     static EmojiSetDownload load(const QJsonObject& json)
0047     {
0048         EmojiSetDownload obj;
0049         obj.url = QUrl(json["url"].toString());
0050         for ( const auto& val : json["paths"].toArray() )
0051         {
0052             auto path = EmojiSetDirectory::load(val.toObject());
0053             int size = path.size;
0054             obj.paths.emplace(size, std::move(path));
0055         }
0056         return obj;
0057     }
0058 };
0059 
0060 struct EmojiSetSlugFormat
0061 {
0062     bool upper = false;
0063     QChar separator = '-';
0064     QString prefix;
0065 
0066     QString slug(const Emoji& emoji) const
0067     {
0068         return slug(emoji.hex_slug);
0069     }
0070 
0071     QString slug(QString slug) const
0072     {
0073         if ( upper )
0074             slug = slug.toUpper();
0075         if ( separator != '-' )
0076             slug = slug.replace('-', separator);
0077         return prefix + slug;
0078     }
0079 };
0080 
0081 struct GitHubTemplate
0082 {
0083     enum RefType
0084     {
0085         Branch,
0086         Tag
0087     };
0088     QString repo;
0089     QString org;
0090     QString ref;
0091     RefType ref_type = Branch;
0092 
0093     static GitHubTemplate load(const QJsonObject& json);
0094 
0095     void apply(struct EmojiSet& set) const;
0096 };
0097 
0098 struct EmojiSet
0099 {
0100     QString name;
0101     QUrl url;
0102     QString license;
0103     QString preview_template;
0104     EmojiSetSlugFormat slug;
0105     EmojiSetDownload download;
0106     QDir path;
0107 
0108     static EmojiSet load(const QJsonObject& json);
0109 
0110     QString image_path(int size, const Emoji& emoji) const
0111     {
0112         return image_path(size, emoji.hex_slug);
0113     }
0114 
0115     QString image_path(int size, const QString& hex_slug) const
0116     {
0117         const auto& path_data = download.paths.at(size);
0118         return path.absoluteFilePath(path_data.path + "/" + slug.slug(hex_slug) + "." + path_data.format);
0119     }
0120 
0121     QDir image_path(int size) const
0122     {
0123         return path.absoluteFilePath(download.paths.at(size).path);
0124     }
0125 
0126     QUrl preview_url(const Emoji& emoji) const
0127     {
0128         return preview_url(emoji.hex_slug);
0129     }
0130 
0131     QUrl preview_url(const QString& hex_slug) const
0132     {
0133         return QUrl(preview_template.arg(slug.slug(hex_slug)));
0134     }
0135 };
0136 
0137 } // namespace glaxnimate::emoji