Skip to content

Commit

Permalink
Add type sets for repeating caption settings
Browse files Browse the repository at this point in the history
Rather than declaring the same settings (font, size, margin…) over
and over, a type set can be created in `_site.toml` and referenced
in the GIF source.
  • Loading branch information
norm committed Jul 1, 2021
1 parent 723ee4f commit ee4eccd
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 9 deletions.
21 changes: 21 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,31 @@ array, supporting multiple captions in a GIF.
`caption` reports the x,y position actually used
* `anchor` is the [alignment of the text][an] relative to the `placement`;
default is `la`
* `type` is the caption type set to use, described below

[col]: https://pillow.readthedocs.io/en/stable/reference/ImageColor.html
[an]: https://pillow.readthedocs.io/en/stable/handbook/text-anchors.html

#### Caption type sets

Rather than multiple GIF configuration files all having identical settings for
font, size, margin…, type sets can be used. Caption type sets are defined in
the `_site.toml`:

[type_set.zim_shout]
align = 'center'
font = 'anteb-extrablack.otf'
margin = '20'
placement = 'bc'
size = '48'

Then a caption only needs to declare the type to have all of the type set
values applied. Note, any values in the source TOML take precedence, acting
as overrides to the type set.

[[caption]]
type = 'zim_shout'
placement = 'br'

### Note on colours

Expand Down
33 changes: 24 additions & 9 deletions bin/make_gif
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ DEFAULT_WIDTH=480
DEFAULT_FPS=10
DEFAULT_COLOURS=64
DEBUG="${GIF_DEBUG:-}"
TYPE_SETS="${GIF_TYPE_SETS:-source/_site.toml}"
green=$'\e'[32m
cyan=$'\e'[36m
magenta=$'\e'[35m
Expand Down Expand Up @@ -72,15 +73,15 @@ function main {
if [ "$num_captions" -gt 0 ]; then
for index in $(seq 1 $num_captions); do
caption_text="$(toml $toml caption.text $index)"
caption_placement="$(toml $toml caption.placement $index)"
caption_size="$(toml $toml caption.size $index)"
caption_margin="$(toml $toml caption.margin $index)"
caption_font="$(toml $toml caption.font $index)"
caption_colour="$(toml $toml caption.colour $index)"
caption_strokew="$(toml $toml caption.stroke_width $index)"
caption_strokecol="$(toml $toml caption.stroke_colour $index)"
caption_align="$(toml $toml caption.align $index)"
caption_anchor="$(toml $toml caption.anchor $index)"
caption_placement="$(get_caption_key $toml $index placement)"
caption_size="$(get_caption_key $toml $index size)"
caption_margin="$(get_caption_key $toml $index margin)"
caption_font="$(get_caption_key $toml $index font)"
caption_colour="$(get_caption_key $toml $index colour)"
caption_strokew="$(get_caption_key $toml $index stroke_width)"
caption_strokecol="$(get_caption_key $toml $index stroke_colour)"
caption_align="$(get_caption_key $toml $index align)"
caption_anchor="$(get_caption_key $toml $index anchor)"
caption \
${output_width:-$DEFAULT_WIDTH} \
$output_height \
Expand Down Expand Up @@ -433,6 +434,20 @@ function get_stats_mode {
echo "stats_mode=${output_mode:-diff}"
}

function get_caption_key {
local toml="$1"
local index="$2"
local key="$3"

local set="$(toml $toml caption.type $index)"
local set_value="$(toml $TYPE_SETS type_set.$set.$key)"
local value="$(toml $toml caption.$key $index)"

[ -n "$value" ] \
&& echo "$value" \
|| echo "$set_value"
}

function size_in_mb {
local bytes="$1"
echo "$(echo 4k $bytes 1048576 / p | dc)"
Expand Down
14 changes: 14 additions & 0 deletions source/_site.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,17 @@ future = false
"/invader-zim/not-now-son.gif" = "/invader-zim/the-nightmare-begins/not-now-son.tn.gif"
"/invader-zim/not-now-son.tn.gif" = "/invader-zim/the-nightmare-begins/not-now-son.tn.gif"
"/tags/dib/" = "/tags/dib-membrane/"

[type_set.zim_shout]
align = 'center'
font = 'anteb-extrablack.otf'
margin = '20'
placement = 'bc'
size = '48'

[type_set.zim_speak]
align = 'center'
font = 'anteb-regular.otf'
margin = '20'
placement = 'bc'
size = '28'
29 changes: 29 additions & 0 deletions tests/config/captions_type.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[video]
source = 'youtube'
file = 'HU2ftCitvyQ'
ext = 'mp4'
start = '120'
duration = '1'

[output]
colours = '96'
fps = '18'

[[caption]]
text = 'And the climb'
from = '0'
to = '0.3'
type = 'normal'

[[caption]]
text = 'is going'
from = '0.31'
to = '0.6'
type = 'large'

[[caption]]
text = 'Where no man has gone before'
from = '0.61'
to = '0.9'
type = 'normal'
font = 'assistant-extrabold.ttf'
9 changes: 9 additions & 0 deletions tests/config/type_sets.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[type_set.normal]
font = 'assistant-semibold.ttf'
placement = 'bc'
size = '40'

[type_set.large]
font = 'assistant-semibold.ttf'
placement = 'bc'
size = '100'
Binary file added tests/gifs/captions_type.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions tests/make_gifs.bats
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@ reset=$'\e'[0m
diff -u $BATS_TMPDIR/output.gif tests/gifs/captions.gif
}

@test generate_captions_type_set {
[ $(uname) != 'Darwin' ] && skip "Not macOS"

GIF_TYPE_SETS=tests/config/type_sets.toml \
run make_gif tests/config/captions_type.toml $BATS_TMPDIR/output.gif
echo "$output"

[ "${lines[1]}" == ' " font_size=40 placement=[121, 305] And the climb' ]
[ "${lines[2]}" == ' " font_size=100 placement=[75, 243] is going' ]
[ "${lines[3]}" == " \" ${magenta}font_size=32${reset} placement=[19, 313] Where no man has gone before" ]
[ "${lines[4]}" == " 102 colours (96 initially)" ]

[ "$status" -eq 0 ]
# cp $BATS_TMPDIR/output.gif tests/gifs/captions_type.gif
diff -u $BATS_TMPDIR/output.gif tests/gifs/captions_type.gif
}

@test generate_captions_noscale {
[ $(uname) != 'Darwin' ] && skip "Not macOS"

Expand Down

0 comments on commit ee4eccd

Please sign in to comment.