Skip to content

Commit

Permalink
Merge pull request #36 from hartwork/theme-z-index
Browse files Browse the repository at this point in the history
Migrate annotation themes to use a z-index
  • Loading branch information
hartwork authored Feb 8, 2021
2 parents a6ff83c + 5b1e957 commit c1c3cc6
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 154 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/expected-annotations-colors_alpha-arrows-rook-upwards.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/expected-annotations-gray_alpha-arrows-rook-downwards.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/expected-annotations-gray_alpha-arrows-rook-upwards.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
79 changes: 39 additions & 40 deletions xiangqi_setup/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import configparser
import errno
import os
from collections import defaultdict
from typing import Tuple

import yaml
Expand Down Expand Up @@ -33,6 +34,9 @@
_MAX_X = 8
_MAX_Y = 9

_Z_INDEX_PIECE_LEVEL = 0
_Z_INDEX_DEBUG_DIAMOND = 1000 # i.e. way above piece level and assumed sane annotation levels

_FILENAME_OF_PARTY_PIECE = {
RED: {
CHARIOT: 'red_chariot.svg',
Expand Down Expand Up @@ -121,9 +125,9 @@ def compose_svg(atoms_to_put, options):
output_board_height_pixel = config.getfloat(_BOARD_CONFIG_SECTION, 'height')
output_board_river_height_pixel = config.getfloat(_BOARD_CONFIG_SECTION, 'river')

jobs_below_piece_level = []
jobs_at_piece_level = []
jobs_above_piece_level = []
# Regular pieces are at level 0; level 1 and above is drawn on top of (i.e. after)
# the pieces while -1 and below is drawn below (i.e. before) the pieces.
jobs_at_z_index = defaultdict(list)

annotation_theme_config_filename = os.path.join(options.annotation_theme_dir, 'config.yml')
with open(annotation_theme_config_filename) as f:
Expand All @@ -138,26 +142,22 @@ def compose_svg(atoms_to_put, options):
f'{put_annotation.annotation_name}.svg')
annotation_scale = options.annotation_scale if annotation_theme_config[
'allow_scaling'][put_annotation.annotation_name] else 1.0
annotation_draw_above_pieces = annotation_theme_config['draw_above_pieces'][
put_annotation.annotation_name]
if annotation_draw_above_pieces:
job_container = jobs_above_piece_level
else:
job_container = jobs_below_piece_level
job_container.append((x_rel, y_rel, filename, annotation_scale))
atom_z_index = int(annotation_theme_config['z_index'][put_annotation.annotation_name])
jobs_at_z_index[atom_z_index].append((x_rel, y_rel, filename, annotation_scale))
else:
assert isinstance(put_atom, PutPiece)
put_piece: PutPiece = put_atom
x_rel = float(put_piece.x) / _MAX_X
y_rel = float(_MAX_Y - put_piece.y) / _MAX_Y
basename = _FILENAME_OF_PARTY_PIECE[put_piece.party][put_piece.piece]
filename = os.path.join(options.piece_theme_dir, basename)
jobs_at_piece_level.append((x_rel, y_rel, filename, options.piece_scale))
jobs_at_z_index[_Z_INDEX_PIECE_LEVEL].append(
(x_rel, y_rel, filename, options.piece_scale))

if options.debug:
for x_rel in (0.0, 1.0):
for y_rel in (0.0, 1.0):
jobs_above_piece_level.append(
jobs_at_z_index[_Z_INDEX_DEBUG_DIAMOND].append(
(x_rel, y_rel, os.path.join(options.piece_theme_dir,
_DIAMOND_FILE_NAME), options.piece_scale))

Expand Down Expand Up @@ -185,33 +185,32 @@ def compose_svg(atoms_to_put, options):
board_root,
])

jobs = jobs_below_piece_level + jobs_at_piece_level + jobs_above_piece_level

for (x_rel, y_rel, filename, element_scale) in jobs:
piece_fig = fromfile(filename)
piece_root = piece_fig.getroot()
original_piece_width_pixel, original_piece_height_pixel = \
_pixel_viewbox_of_figure(piece_fig, options.resolution_dpi)[2:]

# Scale and put piece onto board
center_x_pixel = output_board_offset_left_pixel + output_board_width_pixel * x_rel
center_y_pixel = output_board_offset_top_pixel + (output_board_height_pixel - output_board_river_height_pixel) * y_rel \
+ (output_board_river_height_pixel if (y_rel >= 0.5) else 0.0)

maximum_future_piece_width_pixel = output_board_width_pixel / _MAX_X * element_scale
maximum_future_piece_height_pixel = output_board_height_pixel / _MAX_Y * element_scale

scale = min(maximum_future_piece_width_pixel / original_piece_width_pixel,
maximum_future_piece_height_pixel / original_piece_height_pixel)

future_piece_width_pixel = original_piece_width_pixel * scale
future_piece_height_pixel = original_piece_height_pixel * scale

x_pixel = center_x_pixel - future_piece_width_pixel / 2.0
y_pixel = center_y_pixel - future_piece_height_pixel / 2.0
piece_root.moveto(x_pixel, y_pixel, scale_x=scale, scale_y=scale)
output_fig.append([
piece_root,
])
for _z_index, jobs in sorted(jobs_at_z_index.items()):
for (x_rel, y_rel, filename, element_scale) in jobs:
piece_fig = fromfile(filename)
piece_root = piece_fig.getroot()
original_piece_width_pixel, original_piece_height_pixel = \
_pixel_viewbox_of_figure(piece_fig, options.resolution_dpi)[2:]

# Scale and put piece onto board
center_x_pixel = output_board_offset_left_pixel + output_board_width_pixel * x_rel
center_y_pixel = output_board_offset_top_pixel + (output_board_height_pixel - output_board_river_height_pixel) * y_rel \
+ (output_board_river_height_pixel if (y_rel >= 0.5) else 0.0)

maximum_future_piece_width_pixel = output_board_width_pixel / _MAX_X * element_scale
maximum_future_piece_height_pixel = output_board_height_pixel / _MAX_Y * element_scale

scale = min(maximum_future_piece_width_pixel / original_piece_width_pixel,
maximum_future_piece_height_pixel / original_piece_height_pixel)

future_piece_width_pixel = original_piece_width_pixel * scale
future_piece_height_pixel = original_piece_height_pixel * scale

x_pixel = center_x_pixel - future_piece_width_pixel / 2.0
y_pixel = center_y_pixel - future_piece_height_pixel / 2.0
piece_root.moveto(x_pixel, y_pixel, scale_x=scale, scale_y=scale)
output_fig.append([
piece_root,
])

output_fig.save(options.output_file)
114 changes: 57 additions & 57 deletions xiangqi_setup/themes/annotations/colors_alpha/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,60 +59,60 @@ allow_scaling:
piece_good: true
piece_move: false

draw_above_pieces:
arrow_minus_1_minus_1: true
arrow_minus_1_minus_2: true
arrow_minus_1_plus_0: true
arrow_minus_1_plus_1: true
arrow_minus_1_plus_2: true
arrow_minus_2_minus_1: true
arrow_minus_2_minus_2: true
arrow_minus_2_plus_0: true
arrow_minus_2_plus_1: true
arrow_minus_2_plus_2: true
arrow_minus_3_plus_0: true
arrow_minus_4_plus_0: true
arrow_minus_5_plus_0: true
arrow_minus_6_plus_0: true
arrow_minus_7_plus_0: true
arrow_minus_8_plus_0: true
arrow_plus_0_minus_1: true
arrow_plus_0_minus_2: true
arrow_plus_0_minus_3: true
arrow_plus_0_minus_4: true
arrow_plus_0_minus_5: true
arrow_plus_0_minus_6: true
arrow_plus_0_minus_7: true
arrow_plus_0_minus_8: true
arrow_plus_0_minus_9: true
arrow_plus_0_plus_1: true
arrow_plus_0_plus_2: true
arrow_plus_0_plus_3: true
arrow_plus_0_plus_4: true
arrow_plus_0_plus_5: true
arrow_plus_0_plus_6: true
arrow_plus_0_plus_7: true
arrow_plus_0_plus_8: true
arrow_plus_0_plus_9: true
arrow_plus_1_minus_1: true
arrow_plus_1_minus_2: true
arrow_plus_1_plus_0: true
arrow_plus_1_plus_1: true
arrow_plus_1_plus_2: true
arrow_plus_2_minus_1: true
arrow_plus_2_minus_2: true
arrow_plus_2_plus_0: true
arrow_plus_2_plus_1: true
arrow_plus_2_plus_2: true
arrow_plus_3_plus_0: true
arrow_plus_4_plus_0: true
arrow_plus_5_plus_0: true
arrow_plus_6_plus_0: true
arrow_plus_7_plus_0: true
arrow_plus_8_plus_0: true
blank_bad: true
blank_good: true
blank_move: true
piece_bad: false
piece_good: false
piece_move: false
z_index:
arrow_minus_1_minus_1: 1
arrow_minus_1_minus_2: 1
arrow_minus_1_plus_0: 1
arrow_minus_1_plus_1: 1
arrow_minus_1_plus_2: 1
arrow_minus_2_minus_1: 1
arrow_minus_2_minus_2: 1
arrow_minus_2_plus_0: 1
arrow_minus_2_plus_1: 1
arrow_minus_2_plus_2: 1
arrow_minus_3_plus_0: 1
arrow_minus_4_plus_0: 1
arrow_minus_5_plus_0: 1
arrow_minus_6_plus_0: 1
arrow_minus_7_plus_0: 1
arrow_minus_8_plus_0: 1
arrow_plus_0_minus_1: 1
arrow_plus_0_minus_2: 1
arrow_plus_0_minus_3: 1
arrow_plus_0_minus_4: 1
arrow_plus_0_minus_5: 1
arrow_plus_0_minus_6: 1
arrow_plus_0_minus_7: 1
arrow_plus_0_minus_8: 1
arrow_plus_0_minus_9: 1
arrow_plus_0_plus_1: 1
arrow_plus_0_plus_2: 1
arrow_plus_0_plus_3: 1
arrow_plus_0_plus_4: 1
arrow_plus_0_plus_5: 1
arrow_plus_0_plus_6: 1
arrow_plus_0_plus_7: 1
arrow_plus_0_plus_8: 1
arrow_plus_0_plus_9: 1
arrow_plus_1_minus_1: 1
arrow_plus_1_minus_2: 1
arrow_plus_1_plus_0: 1
arrow_plus_1_plus_1: 1
arrow_plus_1_plus_2: 1
arrow_plus_2_minus_1: 1
arrow_plus_2_minus_2: 1
arrow_plus_2_plus_0: 1
arrow_plus_2_plus_1: 1
arrow_plus_2_plus_2: 1
arrow_plus_3_plus_0: 1
arrow_plus_4_plus_0: 1
arrow_plus_5_plus_0: 1
arrow_plus_6_plus_0: 1
arrow_plus_7_plus_0: 1
arrow_plus_8_plus_0: 1
blank_bad: 0
blank_good: 0
blank_move: 0
piece_bad: -1
piece_good: -1
piece_move: -1
114 changes: 57 additions & 57 deletions xiangqi_setup/themes/annotations/gray_alpha/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,60 +59,60 @@ allow_scaling:
piece_good: true
piece_move: false

draw_above_pieces:
arrow_minus_1_minus_1: true
arrow_minus_1_minus_2: true
arrow_minus_1_plus_0: true
arrow_minus_1_plus_1: true
arrow_minus_1_plus_2: true
arrow_minus_2_minus_1: true
arrow_minus_2_minus_2: true
arrow_minus_2_plus_0: true
arrow_minus_2_plus_1: true
arrow_minus_2_plus_2: true
arrow_minus_3_plus_0: true
arrow_minus_4_plus_0: true
arrow_minus_5_plus_0: true
arrow_minus_6_plus_0: true
arrow_minus_7_plus_0: true
arrow_minus_8_plus_0: true
arrow_plus_0_minus_1: true
arrow_plus_0_minus_2: true
arrow_plus_0_minus_3: true
arrow_plus_0_minus_4: true
arrow_plus_0_minus_5: true
arrow_plus_0_minus_6: true
arrow_plus_0_minus_7: true
arrow_plus_0_minus_8: true
arrow_plus_0_minus_9: true
arrow_plus_0_plus_1: true
arrow_plus_0_plus_2: true
arrow_plus_0_plus_3: true
arrow_plus_0_plus_4: true
arrow_plus_0_plus_5: true
arrow_plus_0_plus_6: true
arrow_plus_0_plus_7: true
arrow_plus_0_plus_8: true
arrow_plus_0_plus_9: true
arrow_plus_1_minus_1: true
arrow_plus_1_minus_2: true
arrow_plus_1_plus_0: true
arrow_plus_1_plus_1: true
arrow_plus_1_plus_2: true
arrow_plus_2_minus_1: true
arrow_plus_2_minus_2: true
arrow_plus_2_plus_0: true
arrow_plus_2_plus_1: true
arrow_plus_2_plus_2: true
arrow_plus_3_plus_0: true
arrow_plus_4_plus_0: true
arrow_plus_5_plus_0: true
arrow_plus_6_plus_0: true
arrow_plus_7_plus_0: true
arrow_plus_8_plus_0: true
blank_bad: true
blank_good: true
blank_move: true
piece_bad: true
piece_good: true
piece_move: false
z_index:
arrow_minus_1_minus_1: 1
arrow_minus_1_minus_2: 1
arrow_minus_1_plus_0: 1
arrow_minus_1_plus_1: 1
arrow_minus_1_plus_2: 1
arrow_minus_2_minus_1: 1
arrow_minus_2_minus_2: 1
arrow_minus_2_plus_0: 1
arrow_minus_2_plus_1: 1
arrow_minus_2_plus_2: 1
arrow_minus_3_plus_0: 1
arrow_minus_4_plus_0: 1
arrow_minus_5_plus_0: 1
arrow_minus_6_plus_0: 1
arrow_minus_7_plus_0: 1
arrow_minus_8_plus_0: 1
arrow_plus_0_minus_1: 1
arrow_plus_0_minus_2: 1
arrow_plus_0_minus_3: 1
arrow_plus_0_minus_4: 1
arrow_plus_0_minus_5: 1
arrow_plus_0_minus_6: 1
arrow_plus_0_minus_7: 1
arrow_plus_0_minus_8: 1
arrow_plus_0_minus_9: 1
arrow_plus_0_plus_1: 1
arrow_plus_0_plus_2: 1
arrow_plus_0_plus_3: 1
arrow_plus_0_plus_4: 1
arrow_plus_0_plus_5: 1
arrow_plus_0_plus_6: 1
arrow_plus_0_plus_7: 1
arrow_plus_0_plus_8: 1
arrow_plus_0_plus_9: 1
arrow_plus_1_minus_1: 1
arrow_plus_1_minus_2: 1
arrow_plus_1_plus_0: 1
arrow_plus_1_plus_1: 1
arrow_plus_1_plus_2: 1
arrow_plus_2_minus_1: 1
arrow_plus_2_minus_2: 1
arrow_plus_2_plus_0: 1
arrow_plus_2_plus_1: 1
arrow_plus_2_plus_2: 1
arrow_plus_3_plus_0: 1
arrow_plus_4_plus_0: 1
arrow_plus_5_plus_0: 1
arrow_plus_6_plus_0: 1
arrow_plus_7_plus_0: 1
arrow_plus_8_plus_0: 1
blank_bad: 0
blank_good: 0
blank_move: 0
piece_bad: 2
piece_good: 2
piece_move: -1

0 comments on commit c1c3cc6

Please sign in to comment.