repobadge/src/repoBadge.cpp

93 lines
3.6 KiB
C++

/*
* This file is part of the BuildIt software distribution (https://gitea.federationhq.de/byterazor/repobadge)
* Copyright (c) 2020 Dominik Meyer <dmeyer@federationhq.de>.
*
* 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, version 3.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
/** @file */
#include <repoBadge.hpp>
#include <sstream>
repoBadge::Badge::Badge(const std::string &left, const std::string &right, BadgeColor color) : left_(left), right_(right)
{
switch(color)
{
case BadgeColor::BLUE:
color_="#007ec6";
break;
case BadgeColor::BRIGHTGREEN:
color_="#4c1";
break;
case BadgeColor::GREEN:
color_="#97CA00";
break;
case BadgeColor::YELLOWGREEN:
color_="#a4a61d";
break;
case BadgeColor::LIGHTGREY:
color_="#9f9f9f";
break;
case BadgeColor::ORANGE:
color_="#fe7d37";
break;
case BadgeColor::RED:
color_="#e05d44";
break;
case BadgeColor::YELLOW:
color_="#dfb317";
break;
};
}
std::string repoBadge::Badge::create()
{
std::stringstream stream;
// we use 82.5 pixes as the maxim character with for the used font-family
// hopefully this will be enough
int widthL = (82.5 * (float)left_.length())/ 11.0;
int widthR = (82.5 * (float)right_.length())/ 11.0;
int width = widthL + widthR + 20;
/*
* simple generation of the image
*/
stream << "<?xml version=\"1.0\"?>\n";
stream << "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\""<<width<<"\" height=\"20\">\n";
stream << "<linearGradient id=\"a\" x2=\"0\" y2=\"100%\">\n";
stream << " <stop offset=\"0\" stop-color=\"#bbb\" stop-opacity=\".1\"/>\n";
stream << " <stop offset=\"2\" stop-opacity=\".1\"/>\n";
stream << "</linearGradient>\n";
stream << "\n";
stream << "<rect rx=\"3\" width=\""<<(widthL+10)<<"\" height=\"20\" fill=\"#555\"/> <!-- Comment -->\n";
stream << "<rect rx=\"3\" x=\""<<(widthL+10)<<"\" width=\""<<(widthR+10)<<"\" height=\"20\" fill=\""<<color_<<"\"/>\n";
stream << "\n";
stream << "<path fill=\""<<color_<<"\" d=\"M"<<widthL+10-2 << " 0h4v20h-4z\"/>\n";
stream << "<rect rx=\"3\" width=\""<<width<<"\" height=\"20\" fill=\"url(#a)\"/>\n";
stream << "<g fill=\"#fff\" text-anchor=\"middle\" font-family=\"DejaVu Sans,Verdana,Geneva,sans-serif\" font-size=\"11\">\n";
stream << " <text x=\""<< (widthL+10)/2 <<"\" y=\"15\" fill=\"#010101\" fill-opacity=\".3\">"<<left_<<"</text>\n";
stream << " <text x=\""<< (widthL+10)/2 <<"\" y=\"14\">"<<left_<<"</text>\n";
stream << " <text x=\""<< (widthL+10) + ((widthR+10)/2) << "\" y=\"15\" fill=\"#010101\" fill-opacity=\".3\">"<<right_<<"</text>\n";
stream << " <text x=\""<< (widthL+10) + ((widthR+10)/2) << "\" y=\"14\">"<<right_<<"</text>\n";
stream << "</g>\n";
stream << "</svg>\n";
return stream.str();
}