Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
m4tx committed Jan 21, 2022
0 parents commit 9cf75ec
Show file tree
Hide file tree
Showing 139 changed files with 51,664 additions and 0 deletions.
66 changes: 66 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Rust CI

on:
push:
branches: [ '**' ]
pull_request:
branches: [ '**' ]

env:
CARGO_TERM_COLOR: always

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose

build_raster_renderer:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose --features raster-renderer
- name: Run tests
run: cargo test --verbose --features raster-renderer

build_fluffy_stuff_tile_sets:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose --features raster-renderer,fluffy-stuff-tile-sets
- name: Run tests
run: cargo test --verbose --features raster-renderer,fluffy-stuff-tile-sets

clippy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: rustup component add clippy
- uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --all-features

rustfmt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
components: rustfmt
- name: Run rustfmt check
uses: actions-rs/cargo@v1
with:
command: fmt
args: -- --check
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
Cargo.lock
34 changes: 34 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[package]
name = "riichi_hand"
version = "0.1.0"
authors = ["Mateusz Maćkowski <[email protected]>"]
edition = "2018"
license = "MIT"
description = "A collection of utilities for working with Riichi Mahjong player hands"
homepage = "https://github.com/m4tx/riichi-hand-rs"
repository = "https://github.com/m4tx/riichi-hand-rs"
documentation = "https://docs.rs/riichi_hand"
readme = "README.md"
categories = ["game-engines", "parser-implementations", "rendering"]
keywords = ["riichi", "mahjong", "renderer"]
exclude = [
".github",
".gitignore",
"examples/"
]

[dependencies]
image = { version = "0.23", default-features = false, optional = true }
lazy_static = { version = "1.4.0", optional = true }

[build-dependencies]
image = { version = "0.23", default-features = false, optional = true }
rayon = { version = "1.5.1", optional = true }
resvg = { version = "0.20.0", optional = true }
tiny-skia = { version = "0.6.2", optional = true }
usvg = { version = "0.20.0", optional = true }

[features]
default = ["raster-renderer", "fluffy-stuff-tile-sets"]
raster-renderer = ["image"]
fluffy-stuff-tile-sets = ["image/png", "rayon", "resvg", "tiny-skia", "usvg", "lazy_static"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Mateusz Maćkowski

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
riichi-hand-rs
==============

[![crates.io](https://img.shields.io/crates/v/riichi_hand.svg)](https://crates.io/crates/riichi_hand)
[![Documentation](https://docs.rs/riichi_hand/badge.svg)](https://docs.rs/riichi_hand)
[![Build Status](https://github.com/m4tx/riichi-hand-rs/workflows/Rust%20CI/badge.svg)](https://github.com/m4tx/riichi-hand-rs/actions)
[![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/m4tx/riichi-hand-rs/blob/master/LICENSE)

A collection of utilities for working with Riichi Mahjong player hands to use
with Rust programming language.

This currently includes:

* Hand representation object
* Parser that allows to quickly create a hand using human-readable string, such
as `123m456p_7*77z`
* Renderer that allows to draw a hand to a raster image (along with a few
ready-to-use sets of tile images)

## Usage

Add the following to your `Cargo.toml`:

```toml
[dependencies]
riichi_hand = "0.1.0"
```

On the feature flags overview, please refer to
the [crate documentation](http://docs.rs/riichi_hand/).

## Example

```rust
use riichi_hand::parser::HandParser;
use riichi_hand::raster_renderer::fluffy_stuff_tile_sets::YELLOW_FLUFFY_STUFF_TILE_SET;
use riichi_hand::raster_renderer::{RasterRenderer, RenderOptions};

fn main() {
let hand = HandParser::parse("123m123p123sEESS").unwrap();
let image = RasterRenderer::render(&hand, &*YELLOW_FLUFFY_STUFF_TILE_SET, RenderOptions::default());
image.save("/tmp/hand.png").unwrap();
}
```

## Uses

* [chombot](https://github.com/riichi/chombot) - Discord bot for Krakow Chombo
Club's Discord server

## License

The project is licensed under the [MIT license](LICENSE).

## Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the project by you shall be licensed as MIT, without any
additional terms or conditions.

## Attribution

This project uses modified
[riichi-mahjong-tiles](https://github.com/FluffyStuff/riichi-mahjong-tiles)
by [FluffyStuff](https://github.com/FluffyStuff), licensed
under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/).
114 changes: 114 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#[cfg(all(not(feature = "raster-renderer"), feature = "fluffy-stuff-tile-sets"))]
compile_error!("feature \"fluffy-stuff-tile-sets\" must be used with \"raster-renderer\"");

fn main() {
#[cfg(feature = "fluffy-stuff-tile-sets")]
tile_set_render::render_tile_sets();

println!("cargo:rerun-if-changed=build.rs");
}

#[cfg(feature = "fluffy-stuff-tile-sets")]
mod tile_set_render {
use std::path::Path;
use std::{env, fs};

use image::RgbaImage;
use rayon::prelude::IntoParallelIterator;
use rayon::prelude::ParallelIterator;

const TILE_SETS: [&str; 3] = ["Yellow", "Red", "Black"];
const TILE_NAMES: [&str; 37] = [
"Man1",
"Man2",
"Man3",
"Man4",
"Man5-Dora",
"Man5",
"Man6",
"Man7",
"Man8",
"Man9",
"Pin1",
"Pin2",
"Pin3",
"Pin4",
"Pin5-Dora",
"Pin5",
"Pin6",
"Pin7",
"Pin8",
"Pin9",
"Sou1",
"Sou2",
"Sou3",
"Sou4",
"Sou5-Dora",
"Sou5",
"Sou6",
"Sou7",
"Sou8",
"Sou9",
"Ton",
"Nan",
"Pei",
"Shaa",
"Haku",
"Hatsu",
"Chun",
];

const BACKGROUND_TILE_MARGIN: f32 = 0.0;
const FOREGROUND_TILE_MARGIN: f32 = 0.05;

pub fn render_tile_sets() {
let out_dir = env::var_os("OUT_DIR").unwrap();

for tile_set in TILE_SETS {
let tile_set_path = Path::new(&out_dir).join("tilesets").join(tile_set);
fs::create_dir_all(&tile_set_path).expect("could not create tile set output directory");

render_and_save(tile_set, &tile_set_path, "Front", BACKGROUND_TILE_MARGIN);
render_and_save(tile_set, &tile_set_path, "Back", BACKGROUND_TILE_MARGIN);

TILE_NAMES.into_par_iter().for_each(|tile_name| {
render_and_save(tile_set, &tile_set_path, tile_name, FOREGROUND_TILE_MARGIN);
});
}
}

fn render_and_save(tile_set: &str, tile_set_path: &Path, name: &str, margin: f32) {
let back_tile = render_svg(&format!("tilesets/{}/{}.svg", tile_set, name), margin);
let dest_path = Path::new(&tile_set_path).join(format!("{}.png", name));
back_tile.save(dest_path).expect("could not save file");
}

fn render_svg(path: &str, margin: f32) -> RgbaImage {
println!("cargo:rerun-if-changed={}", path);

let opt = usvg::Options::default();
let svg_data = fs::read(path).unwrap();
let rtree = usvg::Tree::from_data(&svg_data, &opt.to_ref()).unwrap();

let pixmap_size = rtree.svg_node().size.to_screen_size();
let mut pixmap = tiny_skia::Pixmap::new(pixmap_size.width(), pixmap_size.height()).unwrap();
resvg::render(
&rtree,
usvg::FitTo::Original,
tiny_skia::Transform::from_scale(1.0 - 2.0 * margin, 1.0 - 2.0 * margin)
.post_translate(
pixmap_size.width() as f32 * margin,
pixmap_size.height() as f32 * margin,
),
pixmap.as_mut(),
)
.unwrap();

image::RgbaImage::from_raw(
pixmap_size.width(),
pixmap_size.height(),
pixmap.data().to_vec(),
)
.expect("could not construct an image")
}
}
28 changes: 28 additions & 0 deletions examples/render_hand.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use riichi_hand::parser::HandParser;
use riichi_hand::raster_renderer::fluffy_stuff_tile_sets::YELLOW_FLUFFY_STUFF_TILE_SET;
use riichi_hand::raster_renderer::{RasterRenderer, RenderOptions};
use std::io;

fn main() {
println!("Loading the tile set...");
let tile_set = &*YELLOW_FLUFFY_STUFF_TILE_SET;

println!("Please provide hand string representation (e.g. 123m456p789sEEES):");
let hand = HandParser::parse(read_line().trim()).expect("could not parse hand");

println!("Where do you want to save the image?");
let path = read_line();

println!("Rendering the hand...");
let image = RasterRenderer::render(&hand, tile_set, RenderOptions::default());
image.save(path.trim()).expect("could not save image");
println!("Successfully rendered the hand");
}

fn read_line() -> String {
let mut input = String::new();
io::stdin()
.read_line(&mut input)
.expect("failed to read input");
input
}
2 changes: 2 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
format_strings = false
reorder_imports = true
Loading

0 comments on commit 9cf75ec

Please sign in to comment.