Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ title: EquityStack
description: Python scripts and notebooks for development sector data workflows.
url: https://varnasr.github.io
baseurl: /EquityStack
# Default link-preview card, drawn by scripts/make_share_card.py. A page can set image and image_alt.
share_image: /assets/share-card.jpg
share_image_alt: "EquityStack. Python for distributional analysis and design-based survey estimation in development research. Part of the OpenStacks family."
markdown: kramdown

# GitHub Pages runs this plugin, which turns each folder's README.md into that
Expand Down
8 changes: 7 additions & 1 deletion _layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
<meta property="og:title" content="{{ page_title }}">
<meta property="og:description" content="{{ page.description | default: site.description }}">
<meta property="og:url" content="{{ page.url | absolute_url }}">
<meta name="twitter:card" content="summary">
{% assign share_image = page.image | default: site.share_image | absolute_url %}
<meta property="og:image" content="{{ share_image }}">
<meta property="og:image:width" content="1200">
<meta property="og:image:height" content="630">
<meta property="og:image:alt" content="{{ page.image_alt | default: site.share_image_alt | escape }}">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="{{ share_image }}">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Bricolage+Grotesque:opsz,wght@12..96,700;12..96,800&family=Work+Sans:wght@400;500;600&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet">
Expand Down
Binary file added assets/share-card.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
<meta property="og:title" content="EquityStack · Inequality measurement and survey estimation in Python">
<meta property="og:description" content="Python for distributional analysis and design-based survey estimation in development research. Part of the OpenStacks family.">
<meta property="og:url" content="https://varnasr.github.io/EquityStack/">
<meta name="twitter:card" content="summary">
<meta property="og:image" content="https://varnasr.github.io/EquityStack/assets/share-card.jpg">
<meta property="og:image:width" content="1200">
<meta property="og:image:height" content="630">
<meta property="og:image:alt" content="EquityStack. Python for distributional analysis and design-based survey estimation in development research. Part of the OpenStacks family.">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://varnasr.github.io/EquityStack/assets/share-card.jpg">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Bricolage+Grotesque:opsz,wght@12..96,700;12..96,800&family=Work+Sans:wght@400;500;600&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet">
Expand Down
Binary file added scripts/fonts/Inter-Regular.otf
Binary file not shown.
Binary file added scripts/fonts/Inter-SemiBold.otf
Binary file not shown.
Binary file added scripts/fonts/InterDisplay-ExtraBold.otf
Binary file not shown.
88 changes: 88 additions & 0 deletions scripts/fonts/OFL-Inter.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
Copyright 2016-2023 The Inter Project Authors (https://github.com/rsms/inter)
Subset to Basic Latin and a few punctuation marks for the share card script.
Licensed under the SIL Open Font License, Version 1.1.

PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font
creation efforts of academic and linguistic communities, and to
provide a free and open framework in which fonts may be shared and
improved in partnership with others.

The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any
reserved names are not used by derivative works. The fonts and
derivatives, however, cannot be released under any other type of
license. The requirement for fonts to remain under this license
does not apply to any document created using the fonts or their
derivatives.

DEFINITIONS
Font Software refers to the set of files released by the
Copyright Holder(s) under this license and clearly marked as such.
This may include source files, build scripts and documentation.

Reserved Font Name refers to any names specified as such after
the copyright statement(s).

Original Version refers to the collection of Font Software
components as distributed by the Copyright Holder(s).

Modified Version refers to any derivative made by adding to,
deleting, or substituting in part or in whole any of the components
of the Original Version, by changing formats or by porting the Font
Software to a new environment.

Author refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.

PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person
obtaining a copy of the Font Software, to use, study, copy, merge,
embed, modify, redistribute, and sell modified and unmodified copies
of the Font Software, subject to the following conditions:

1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.

2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each
copy contains the above copyright notice and this license. These
can be included either as stand-alone text files, human-readable
headers or in the appropriate machine-readable metadata fields
within text or binary files as long as those fields can be easily
viewed by the user.

3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the
corresponding Copyright Holder. This restriction only applies to
the primary font name as presented to the users.

4) The name(s) of the Copyright Holder(s) or the Author(s) of the
Font Software shall not be used to promote, endorse or advertise
any Modified Version, except to acknowledge the contribution(s)
of the Copyright Holder(s) and the Author(s) or with their
explicit written permission.

5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.

TERMINATION
This license becomes null and void if any of the above conditions
are not met.

DISCLAIMER
THE FONT SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL
THE COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR
CONSEQUENTIAL DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF THE USE OR INABILITY TO USE THE FONT
SOFTWARE OR FROM OTHER DEALINGS IN THE FONT SOFTWARE.
143 changes: 143 additions & 0 deletions scripts/make_share_card.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#!/usr/bin/env python3
"""Draws the 1200x630 link-preview card, assets/share-card.jpg.

Run it after changing the title, line or colours below:

python3 scripts/make_share_card.py

The words are copied from the page's own meta description, so change them there
first. Needs Pillow. Fonts are Inter (SIL Open Font License, see
fonts/OFL-Inter.txt), subset and kept beside this script. Style follows the
OpenStacks house style: 2px rules, no shadows, no rounded corners, saffron as
the single accent. Text colours are measured against what they sit on and the
script stops below 4.5:1.
"""
import io
from pathlib import Path
from PIL import Image, ImageDraw, ImageFont

HERE = Path(__file__).resolve().parent
OUT = HERE.parent / "assets" / "share-card.jpg"

TITLE = 'EquityStack'
SUBTITLE = None # optional second line under the title, or None
LINE = 'Python for distributional analysis and design-based survey estimation in development research.'
LABEL = 'Part of the OpenStacks family' # small label, top left
ADDRESS = 'varnasr.github.io/EquityStack/'
BAND = '#b23a2a' # colour of the bottom band

BLACK, WHITE, SAFFRON, ASH = "#0d0d0f", "#f6f2ea", "#f2a541", "#d8d3c8"
S = 2 # draw at 2x, then reduce, for smooth edges
W, H = 1200, 630


def lum(c):
c = c.lstrip("#")
ch = [int(c[i:i + 2], 16) / 255 for i in (0, 2, 4)]
ch = [x / 12.92 if x <= 0.03928 else ((x + 0.055) / 1.055) ** 2.4 for x in ch]
return 0.2126 * ch[0] + 0.7152 * ch[1] + 0.0722 * ch[2]


def ratio(a, b):
hi, lo = sorted((lum(a), lum(b)), reverse=True)
return (hi + 0.05) / (lo + 0.05)


for ink, ground, what in [(WHITE, BLACK, "title"), (SAFFRON, BLACK, "subtitle and label"),
(ASH, BLACK, "line"), (WHITE, BAND, "address")]:
assert ratio(ink, ground) >= 4.5, f"{what}: {ratio(ink, ground):.2f}:1 is below 4.5:1"


def font(name, px):
return ImageFont.truetype(str(HERE / "fonts" / name), px * S)


def width(d, text, f, track=0):
return d.textlength(text, font=f) + track * S * max(len(text) - 1, 0)


def spaced(d, xy, text, f, fill, track):
x, y = xy
for ch in text:
d.text((x, y), ch, font=f, fill=fill)
x += d.textlength(ch, font=f) + track * S


def wrap(d, text, f, maxw):
lines, cur = [], ""
for w in text.split():
t = (cur + " " + w).strip()
if width(d, t, f) <= maxw or not cur:
cur = t
else:
lines.append(cur)
cur = w
lines.append(cur)
return lines


def render():
im = Image.new("RGB", (W * S, H * S), BLACK)
d = ImageDraw.Draw(im)
pad, maxw = 72 * S, (W - 144) * S

# top strip
d.rectangle([0, 0, W * S, 76 * S], fill=BLACK)
d.rectangle([0, 74 * S, W * S, 76 * S], fill=WHITE)
d.rectangle([pad, 30 * S, pad + 16 * S, 46 * S], fill=SAFFRON)
spaced(d, (pad + 32 * S, 26 * S), LABEL.upper(), font("Inter-SemiBold.otf", 22), SAFFRON, 3)

# bottom band
d.rectangle([0, (H - 84) * S, W * S, H * S], fill=BAND)
d.rectangle([0, (H - 86) * S, W * S, (H - 84) * S], fill=WHITE)
f = font("Inter-SemiBold.otf", 26)
d.text((pad, (H - 84) * S + 26 * S), ADDRESS, font=f, fill=WHITE)

# title: one line if it fits at 112px or more, otherwise two lines
title = TITLE.upper()
for px in range(150, 111, -2):
f = font("InterDisplay-ExtraBold.otf", px)
if width(d, title, f) <= maxw:
tl = [title]
break
else:
for px in range(120, 70, -2):
f = font("InterDisplay-ExtraBold.otf", px)
tl = wrap(d, title, f, maxw)
if len(tl) <= 2 and all(width(d, x, f) <= maxw for x in tl):
break
lh = int(px * 0.98)
y = 100 * S
for ln in tl:
d.text((pad, y), ln, font=f, fill=WHITE)
y += lh * S
y += 18 * S
if SUBTITLE:
fs = font("InterDisplay-ExtraBold.otf", 40)
d.text((pad, y), SUBTITLE, font=fs, fill=SAFFRON)
y += 68 * S
else:
d.rectangle([pad, y + 6 * S, pad + 120 * S, y + 14 * S], fill=SAFFRON)
y += 46 * S

# one line from the page's own description
fl = font("Inter-Regular.otf", 38)
for ln in wrap(d, LINE, fl, maxw - 40 * S):
d.text((pad, y), ln, font=fl, fill=ASH)
y += 52 * S
assert y <= (H - 92) * S, f"text runs into the bottom band ({y // S}px)"
return im.resize((W, H), Image.LANCZOS)


def jpeg():
b = io.BytesIO()
render().save(b, "JPEG", quality=90, optimize=True, subsampling=0)
return b.getvalue()


if __name__ == "__main__":
data = jpeg()
assert len(data) <= 250_000, f"{len(data)} bytes is over the 250 KB limit"
OUT.parent.mkdir(parents=True, exist_ok=True)
OUT.write_bytes(data)
print(f"wrote {OUT} ({len(data)} bytes, {W}x{H})")
Loading