From 65a545032b102d586ee2b66d6278140a9af6596d Mon Sep 17 00:00:00 2001 From: Yash Ladha <201551061@iiitvadodara.ac.in> Date: Sun, 17 Dec 2017 23:21:02 +0530 Subject: [PATCH] Fixes #374: Add custom font for badges (#384) --- app/generate-badges.py | 30 +++++++++++++++++++++++++----- app/main.py | 19 ++++++++++++++++++- app/merge_badges.py | 2 -- app/static/css/style.css | 5 +++++ app/static/js/form.js | 6 ++++++ app/static/js/script.js | 6 ++++++ app/templates/index.html | 28 ++++++++++++++++++++++++---- 7 files changed, 84 insertions(+), 12 deletions(-) diff --git a/app/generate-badges.py b/app/generate-badges.py index cf7cf8a6..3b4ce116 100755 --- a/app/generate-badges.py +++ b/app/generate-badges.py @@ -17,12 +17,19 @@ paper_sizes['A3'] = ['297mm', '420mm'] paper_sizes['A4'] = ['210mm', '297mm'] -input_files = [file for file in os.listdir(UPLOAD_FOLDER) if file.lower().endswith(".csv")] +input_files = [file for file in os.listdir( + UPLOAD_FOLDER) if file.lower().endswith(".csv")] with open(APP_ROOT + "/../badges/8BadgesOnA3.svg", encoding="UTF-8") as f: CONTENT = f.read() +font_choice = None +if os.path.isfile(os.path.join(UPLOAD_FOLDER, 'fonts.json')): + DATA = json.load(open(os.path.join(UPLOAD_FOLDER, "fonts.json"))) + font_choice = DATA['font'] + + def configure_badge_page(badge_page, options): """ Configure the badge page according to the page @@ -57,6 +64,15 @@ def generate_badges(aggregate, folder, index, picture, paper_size): target = os.path.join(folder, "badges_{}.svg".format(index)) print("Generating {}".format(target)) content = CONTENT + if font_choice: + content = content.replace("font-family:sans-serif", + "font-family:" + font_choice) + content = content.replace("inkscape-font-specification:sans-serif", + "inkscape-font-specification:" + font_choice) + content = content.replace("font-family:ubuntu", + "font-family:" + font_choice) + content = content.replace("inkscape-font-specification:ubuntu", + "inkscape-font-specification:" + font_choice) for i, row in enumerate(aggregate): row = [entry for entry in row if not entry.isspace()] if len(row) == 0: @@ -67,7 +83,8 @@ def generate_badges(aggregate, folder, index, picture, paper_size): row = [""] * (4 - len(row)) + row for j, text in enumerate(row): text = html.escape(text) - content = content.replace("person_{}_line_{}".format(i + 1, j + 1), text) + content = content.replace( + "person_{}_line_{}".format(i + 1, j + 1), text) content = content.replace("badge_{}.png".format(i + 1), picture) with open(target, "w", encoding="UTF-8") as f: f.write(content) @@ -79,7 +96,8 @@ def generate_badges(aggregate, folder, index, picture, paper_size): # check if custom config.json is present for the file config_json_uploaded = os.path.splitext(input_file)[0] + '.json' - config_json_uploaded_path = os.path.join(UPLOAD_FOLDER, config_json_uploaded) + config_json_uploaded_path = os.path.join( + UPLOAD_FOLDER, config_json_uploaded) if os.path.isfile(config_json_uploaded_path): config_json = config_json_uploaded config = json.loads(open(UPLOAD_FOLDER + '/' + config_json).read()) @@ -109,8 +127,10 @@ def generate_badges(aggregate, folder, index, picture, paper_size): if options['badge_wrap']: aggregate.append(row) if len(aggregate) >= NUMBER_OF_BADGES_PER_PAGE: - generate_badges(aggregate, folder, i, badges_background, options) + generate_badges(aggregate, folder, i, + badges_background, options) aggregate = [] i += 1 if aggregate: - generate_badges(aggregate, folder, i, badges_background, options) + generate_badges(aggregate, folder, i, + badges_background, options) diff --git a/app/main.py b/app/main.py index 70c7fe39..dab3a2c7 100644 --- a/app/main.py +++ b/app/main.py @@ -2,6 +2,7 @@ from flask_compress import Compress from werkzeug.utils import secure_filename import os +import json import shutil import traceback from svg_to_png import do_svg2png @@ -25,6 +26,8 @@ # help="Start the server in development mode with debug=True", # action="store_true") # args = parser.parse_args() +CUSTOM_FONTS = ['monospace', 'sans-serif', 'sans', 'Courier 10 Pitch', 'Source Code Pro'] + @app.route('/') def index(): @@ -35,7 +38,7 @@ def index(): for file in os.listdir(UPLOAD_FOLDER): if file.rsplit('.', 1)[1] == 'png' and file != 'user_defined.png': default_background.append(file) - return render_template('index.html', default_background=default_background) + return render_template('index.html', default_background=default_background, custom_fonts=CUSTOM_FONTS) def generate_badges(_pdf=True): @@ -72,6 +75,7 @@ def upload(): empty_directory() csv = request.form['csv'].strip() img = request.form['img-default'] + custom_font = request.form['custfont'] text_on_image = request.form['text_on_image'] file = request.files['file'] @@ -83,6 +87,15 @@ def upload(): do_svg2png(img, 1, bg_color, text_on_image) filename = img + '.csv' + # Custom font is selected for the text + if custom_font != '': + json_str = json.dumps({ + 'font': custom_font + }) + f = open(os.path.join(app.config['UPLOAD_FOLDER'], 'fonts.json'), "w+") + f.write(json_str) + f.close() + # If the textbox is filled if img == '': img = request.files['image'].filename @@ -145,6 +158,8 @@ def upload(): # Remove the uploaded files after job in done os.unlink(os.path.join(app.config['UPLOAD_FOLDER'], filename)) + if os.path.isfile(os.path.join(app.config['UPLOAD_FOLDER'], 'fonts.json')): + os.unlink(os.path.join(app.config['UPLOAD_FOLDER'], 'fonts.json')) try: if 'imgname' in locals(): os.unlink(os.path.join(app.config['UPLOAD_FOLDER'], imgname)) @@ -153,6 +168,8 @@ def upload(): if True: flash(filename.replace('.', '-'), 'success-pdf') + os.rename(os.path.join(BADGES_FOLDER + "/" + filename + ".badges.pdf"), + os.path.join(BADGES_FOLDER + "/" + filename.replace('.', '-') + "-badges.pdf")) return redirect(url_for('index')) diff --git a/app/merge_badges.py b/app/merge_badges.py index 1e0f9931..a3230d83 100755 --- a/app/merge_badges.py +++ b/app/merge_badges.py @@ -14,8 +14,6 @@ if subprocess.call(['which', 'python3']) != 0: raise PackageNotFoundError("Package python3 not found") -if subprocess.call(['which', 'pdftk']) != 0: - raise PackageNotFoundError("Package pdftk not found") APP_ROOT = os.path.dirname(os.path.abspath(__file__)) BADGES_FOLDER = os.path.join(APP_ROOT, 'static/badges') diff --git a/app/static/css/style.css b/app/static/css/style.css index f3a22022..d1bf9d6d 100644 --- a/app/static/css/style.css +++ b/app/static/css/style.css @@ -183,6 +183,11 @@ border-bottom: 1px solid darkgray; } +.font-options { + border-bottom: 1px solid darkgray; + padding: 9px; +} + .dropdown-menu>li:hover { background: #f1f9fd; cursor: pointer; diff --git a/app/static/js/form.js b/app/static/js/form.js index 2f9aac25..3cb048d0 100644 --- a/app/static/js/form.js +++ b/app/static/js/form.js @@ -33,6 +33,12 @@ $(document).ready( $("#cutext-input").css("display", "none"); $("#config-input").css("display", "block"); }); + $("#custfont").click(function(){ + $("#custom-font").css("display", "block"); + }); + $("#deffont").click(function(){ + $("#custom-font").css("display", "none"); + }); $("input[type=file], input[type=hidden], input[type=text], textarea").on("keyup change", function () { var csv = $("textarea[name='csv']").val(); diff --git a/app/static/js/script.js b/app/static/js/script.js index 14b1d87f..2ec7fb0d 100644 --- a/app/static/js/script.js +++ b/app/static/js/script.js @@ -21,6 +21,12 @@ $(document).on("ready", function () { $('input[name="img-default"]').val(i).trigger('change'); }); + $(".font-options").click(function () { + var i = $(this).data("item"); + $(".placeholder2").text(i); + $("input[name='custfont']").val(i); + }); + $("#picker").minicolors({ control: 'hue', diff --git a/app/templates/index.html b/app/templates/index.html index ae95ebd1..b9ed23a9 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -1,5 +1,4 @@ -{% extends "layout.html" %} -{% block body %} +{% extends "layout.html" %} {% block body %}
@@ -73,7 +72,28 @@ -
Please select a default background or upload an image!
+
Please select a default background or upload an image!
+ +
    +
  • + Use Custom font +
  • + + +
  • @@ -105,7 +125,7 @@ {% if cat == 'success-pdf' %}
    Your badges have been created successfully.
    {% else %}
    {{msg}}