diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9ce5521..27800a9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,3 +29,7 @@ jobs: run: pixi run test-lfortran - name: Compare against matplotlib references run: pixi run compare + - name: Compare PNG output against matplotlib + run: pixi run compare-png + - name: Compare PDF output against matplotlib + run: pixi run compare-pdf diff --git a/README.md b/README.md index 30a73c6..00ee5ab 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,8 @@ [![CI](https://github.com/certik/fplot/actions/workflows/ci.yml/badge.svg)](https://github.com/certik/fplot/actions/workflows/ci.yml) -Pure Fortran plotting library that emits SVG text, with a pylab-style API. +Pure Fortran plotting library with a pylab-style API, writing SVG, PNG and PDF. +No external graphics library: the PNG rasterizer and its font are part of fplot. ```fortran use fplot @@ -12,7 +13,7 @@ call xlabel("x") call ylabel("y") call grid(.true.) call legend() -call savefig("out.svg") ! file backend +call savefig("out.svg") ! or "out.png", "out.pdf" call show() ! Jupyter (LFortran) or writes fplot_show.svg ! subplots @@ -43,7 +44,8 @@ call suptitle("figure title") markers `o x . s ^ v < > * + D` - Optional `label=`, `lw=`, `color=`, `marker=`, `alpha=` - Title, axis labels, grid, `xlim` / `ylim`, `clf` / `figure(figsize=, dpi=)` -- `savefig(file, transparent=, facecolor=)` +- `savefig(file, transparent=, facecolor=)`; the extension picks the format, + one of `.svg`, `.png` or `.pdf`, and `dpi=` sizes the PNG raster - `axis("on"|"off"|"equal"|"scaled"|"tight"|"auto")` and `set_aspect` - `set_xscale` / `set_yscale` with `"linear"`, `"log"` or `"symlog"` - `tick_params(axis=, direction=, length=, labelsize=, rotation=)` and `spines` @@ -80,19 +82,24 @@ pixi run demo-flang pixi run build-lfortran pixi run test-lfortran -# Compare against matplotlib reference SVGs -pixi run compare +# Compare against matplotlib references +pixi run compare # SVG, structurally +pixi run compare-png # PNG, pixel by pixel +pixi run compare-pdf # PDF, rasterized by pdftoppm ``` -CI (Linux) runs `pixi run test-flang`, `pixi run test-lfortran` and -`pixi run compare`. +The SVG comparison is structural because a viewer, not fplot, decides what an +SVG looks like. PNG and PDF are compared as pixels, since there fplot decides. + +CI (Linux) runs `pixi run test-flang` and `pixi run test-lfortran`, then all +three comparisons. ## Layout ``` src/ library modules examples/ demo.f90 -tests/ Fortran test plots, matplotlib refs, compare script +tests/ Fortran test plots, matplotlib refs, compare scripts scripts/ build_flang.sh, build_lfortran.sh ``` diff --git a/pixi.lock b/pixi.lock index 03d0cd1..cfd168b 100644 --- a/pixi.lock +++ b/pixi.lock @@ -24,6 +24,7 @@ environments: - conda: https://conda.anaconda.org/conda-forge/linux-64/brotli-1.2.0-h505cf86_3.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/brotli-bin-1.2.0-h9908984_3.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/bzip2-1.0.8-hda65f42_10.conda + - conda: https://conda.anaconda.org/conda-forge/linux-64/c-ares-1.34.8-h280c20c_1.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/cairo-1.18.4-he90730b_1.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/cffi-2.1.1-py314h77c0f9c_1.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/clang-22-22.1.8-default_hd70ba2e_6.conda @@ -69,11 +70,13 @@ environments: - conda: https://conda.anaconda.org/conda-forge/linux-64/libclang13-22.1.8-default_hd70ba2e_6.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/libcompiler-rt-22.1.8-hb700be7_1.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/libcups-2.3.3-h7a8fb5f_6.conda + - conda: https://conda.anaconda.org/conda-forge/linux-64/libcurl-8.21.0-heca4667_4.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/libdeflate-1.25-hd45a770_1.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/libdrm-2.4.129-h7cc23a3_0.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/libedit-3.1.20250104-pl5321h373387f_1.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/libegl-1.7.0-ha4b6fd6_3.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/libegl-devel-1.7.0-ha4b6fd6_3.conda + - conda: https://conda.anaconda.org/conda-forge/linux-64/libev-4.33-h280c20c_3.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/libexpat-2.8.1-hecca717_1.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/libffi-3.7.0-h3435931_0.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/libflang-rt-22.1.8-ha4b6fd6_0.conda @@ -98,15 +101,18 @@ environments: - conda: https://conda.anaconda.org/conda-forge/linux-64/libllvm22-22.1.8-hf7376ad_1.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/liblzma-5.8.3-hb03c661_1.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/libmpdec-4.0.0-hb03c661_2.conda + - conda: https://conda.anaconda.org/conda-forge/linux-64/libnghttp2-1.68.1-h877daf1_0.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/libntlm-1.8-hb9d3cd8_0.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/libopenblas-0.3.34-pthreads_h94d23a6_0.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/libopengl-1.7.0-ha4b6fd6_3.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/libpciaccess-0.19-hb03c661_1.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/libpng-1.6.58-h922cc85_1.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/libpq-18.6-h9d76c99_0.conda + - conda: https://conda.anaconda.org/conda-forge/linux-64/libpsl-0.23.1-hf670292_0.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/libraqm-0.11.0-h6406941_0.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/libsodium-1.0.22-h280c20c_1.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/libsqlite-3.53.4-hf4e2dac_0.conda + - conda: https://conda.anaconda.org/conda-forge/linux-64/libssh2-1.11.1-h6154650_1.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/libstdcxx-16.1.0-h934c35e_1.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/libstdcxx-ng-16.1.0-hdf11a46_1.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/libtiff-4.7.2-h9d88235_0.conda @@ -128,6 +134,8 @@ environments: - conda: https://conda.anaconda.org/conda-forge/linux-64/matplotlib-3.11.1-py314h815e797_2.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/matplotlib-base-3.11.1-py314h9a9c090_2.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/ncurses-6.6-hdb14827_1.conda + - conda: https://conda.anaconda.org/conda-forge/linux-64/nspr-4.40-h29cc59b_0.conda + - conda: https://conda.anaconda.org/conda-forge/linux-64/nss-3.118-h445c969_0.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/numpy-2.5.2-py314h2b28147_0.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/openjpeg-2.5.4-h55fea9a_0.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/openldap-2.6.13-hbde042b_0.conda @@ -135,6 +143,7 @@ environments: - conda: https://conda.anaconda.org/conda-forge/linux-64/pcre2-10.47-haa7fec5_0.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/pillow-12.3.0-py314h8ec4b1a_0.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/pixman-0.46.4-h54a6638_3.conda + - conda: https://conda.anaconda.org/conda-forge/linux-64/poppler-26.07.0-he594abd_3.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/pthread-stubs-0.4-hb03c661_1003.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/pyside6-6.11.1-py314h3987850_1.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/python-3.14.6-h242f9ac_102_cp314.conda @@ -167,6 +176,7 @@ environments: - conda: https://conda.anaconda.org/conda-forge/linux-64/xorg-libxi-1.8.3-hb03c661_0.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/xorg-libxrandr-1.5.5-hb03c661_0.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/xorg-libxrender-0.9.12-hb9d3cd8_0.conda + - conda: https://conda.anaconda.org/conda-forge/linux-64/xorg-libxscrnsaver-1.2.4-hb9d3cd8_0.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/xorg-libxtst-1.2.5-hb9d3cd8_3.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/xorg-libxxf86vm-1.1.7-hb03c661_0.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/xorg-xorgproto-2025.1-h280c20c_1.conda @@ -195,6 +205,7 @@ environments: - conda: https://conda.anaconda.org/conda-forge/noarch/munkres-1.1.4-pyhd8ed1ab_1.conda - conda: https://conda.anaconda.org/conda-forge/noarch/nlohmann_json-abi-3.12.0-h0f90c79_2.conda - conda: https://conda.anaconda.org/conda-forge/noarch/packaging-26.3-pyhc364b38_0.conda + - conda: https://conda.anaconda.org/conda-forge/noarch/poppler-data-0.4.12-hd8ed1ab_0.conda - conda: https://conda.anaconda.org/conda-forge/noarch/pycparser-3.0-pyhcf101f3_0.conda - conda: https://conda.anaconda.org/conda-forge/noarch/pyparsing-3.3.2-pyhcf101f3_0.conda - conda: https://conda.anaconda.org/conda-forge/noarch/python-dateutil-2.9.0.post0-pyhe01879c_2.conda @@ -221,6 +232,7 @@ environments: - conda: https://conda.anaconda.org/conda-forge/noarch/munkres-1.1.4-pyhd8ed1ab_1.conda - conda: https://conda.anaconda.org/conda-forge/noarch/nlohmann_json-abi-3.12.0-h0f90c79_2.conda - conda: https://conda.anaconda.org/conda-forge/noarch/packaging-26.3-pyhc364b38_0.conda + - conda: https://conda.anaconda.org/conda-forge/noarch/poppler-data-0.4.12-hd8ed1ab_0.conda - conda: https://conda.anaconda.org/conda-forge/noarch/pycparser-3.0-pyhcf101f3_0.conda - conda: https://conda.anaconda.org/conda-forge/noarch/pyparsing-3.3.2-pyhcf101f3_0.conda - conda: https://conda.anaconda.org/conda-forge/noarch/python-dateutil-2.9.0.post0-pyhe01879c_2.conda @@ -233,6 +245,7 @@ environments: - conda: https://conda.anaconda.org/conda-forge/osx-arm64/brotli-1.2.0-h7d5ae5b_1.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/brotli-bin-1.2.0-hc919400_1.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/bzip2-1.0.8-h4e30115_10.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/c-ares-1.34.8-h1a92334_1.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/cairo-1.18.4-he0f2337_1.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/cffi-2.1.1-py314h7bede21_0.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/contourpy-1.3.3-py314hf8a3a22_4.conda @@ -251,9 +264,11 @@ environments: - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libbrotlidec-1.2.0-hc919400_1.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libbrotlienc-1.2.0-hc919400_1.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libcblas-3.11.0-9_hb0561ab_openblas.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libcurl-8.21.0-hf618e03_4.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libcxx-22.1.8-h55c6f16_0.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libdeflate-1.25-he7e0567_1.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libedit-3.1.20250104-pl5321h26f1114_1.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libev-4.33-h1a92334_3.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libexpat-2.8.1-hf6b4638_1.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libffi-3.5.2-hcf2aa1b_0.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libfreetype-2.14.3-hce30654_1.conda @@ -269,11 +284,14 @@ environments: - conda: https://conda.anaconda.org/conda-forge/osx-arm64/liblapack-3.11.0-9_hd9741b5_openblas.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/liblzma-5.8.3-h8088a28_0.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libmpdec-4.0.0-h84a0fba_1.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libnghttp2-1.68.1-h8f3e76b_0.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libopenblas-0.3.34-openmp_he657e61_0.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libpng-1.6.58-h132b30e_0.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libpsl-0.23.1-h7a62e17_0.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libraqm-0.11.0-h45af499_0.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libsodium-1.0.22-h1a92334_1.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libsqlite-3.53.4-h1ae2325_0.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libssh2-1.11.1-hbf2880b_1.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libtiff-4.7.2-h282da08_0.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libwebp-base-1.6.0-h202fb40_1.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libxcb-1.17.0-hdb1d25a_0.conda @@ -282,12 +300,15 @@ environments: - conda: https://conda.anaconda.org/conda-forge/osx-arm64/matplotlib-3.11.1-py314h314fc0d_2.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/matplotlib-base-3.11.1-py314h27b0870_2.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/ncurses-6.6-h1d4f5a5_0.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/nspr-4.40-hdcbdcf5_0.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/nss-3.118-h1c710a3_0.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/numpy-2.5.1-py314hb79c6fa_0.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/openjpeg-2.5.4-hd9e9057_0.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/openssl-3.6.3-hd24854e_1.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/pcre2-10.47-h30297fc_0.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/pillow-12.3.0-py314hab283cf_0.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/pixman-0.46.4-h784d473_3.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/poppler-26.07.0-h4cfec15_3.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/pthread-stubs-0.4-h84a0fba_1003.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/python-3.14.6-h156bc91_101_cp314.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/qhull-2020.2-h420ef59_5.conda @@ -398,6 +419,21 @@ packages: - bzip2 >=1.0.8,<2.0a0 size: 257808 timestamp: 1785906269155 +- conda: https://conda.anaconda.org/conda-forge/linux-64/c-ares-1.34.8-h280c20c_1.conda + sha256: 5139b6afbfaca91c47104ba9a6a40f81211e1c9200e96b73ce3a56f0ca1902f4 + md5: 2cef891b791040aab83e218c7d137679 + depends: + - __glibc >=2.17,<3.0.a0 + - libgcc >=14 + constrains: + - c-ares-static <0a0 + license: MIT + license_family: MIT + run_exports: + weak: + - c-ares >=1.34.8,<2.0a0 + size: 226755 + timestamp: 1786116641939 - conda: https://conda.anaconda.org/conda-forge/linux-64/cairo-1.18.4-he90730b_1.conda sha256: 06525fa0c4e4f56e771a3b986d0fdf0f0fc5a3270830ee47e127a5105bde1b9a md5: bb6c4808bfa69d6f7f6b07e5846ced37 @@ -1146,6 +1182,26 @@ packages: - libcups >=2.3.3,<2.4.0a0 size: 4518030 timestamp: 1770902209173 +- conda: https://conda.anaconda.org/conda-forge/linux-64/libcurl-8.21.0-heca4667_4.conda + sha256: aff8ef75636d0825ce34911e0bef2f26816e7afd090a9dcb4b9bacab75cbf584 + md5: 3f2fd5617cfacac49c85f6dc63842ea1 + depends: + - __glibc >=2.17,<3.0.a0 + - krb5 >=1.22.2,<1.23.0a0 + - libgcc >=14 + - libnghttp2 >=1.68.1,<2.0a0 + - libpsl >=0.23.0,<0.24.0a0 + - libssh2 >=1.11.1,<2.0a0 + - libzlib >=1.3.2,<2.0a0 + - openssl >=3.5.7,<4.0a0 + - zstd >=1.5.7,<1.6.0a0 + license: curl + license_family: MIT + run_exports: + weak: + - libcurl >=8.21.0,<9.0a0 + size: 480565 + timestamp: 1785500108494 - conda: https://conda.anaconda.org/conda-forge/linux-64/libdeflate-1.25-hd45a770_1.conda sha256: 82e134c8a08b1eed9a2ed8ab578b89aa1730dcde3dea8dd87645ed0637878e54 md5: 40f9b31aa9cf007789867df0decd0492 @@ -1212,6 +1268,19 @@ packages: - libegl >=1.7.0,<2.0a0 size: 31718 timestamp: 1779728222280 +- conda: https://conda.anaconda.org/conda-forge/linux-64/libev-4.33-h280c20c_3.conda + sha256: e4418f68d01a6307c4431b585c71b584f40189877364e542ee87deb777f5e85b + md5: 7bc31538d6e3fb349e897353969237ec + depends: + - libgcc >=14 + - __glibc >=2.17,<3.0.a0 + license: BSD-2-Clause OR GPL-2.0-or-later + license_family: GPL + run_exports: + weak: + - libev >=4.33,<4.34.0a0 + size: 43220 + timestamp: 1785917328200 - conda: https://conda.anaconda.org/conda-forge/linux-64/libexpat-2.8.1-hecca717_1.conda sha256: 16feffd9ddbbe5b718515d38ee376c685ba95491cd901244e24671d20b952a77 md5: b24d3c612f71e7aa74158d92106318b2 @@ -1541,6 +1610,25 @@ packages: run_exports: {} size: 92759 timestamp: 1786650399772 +- conda: https://conda.anaconda.org/conda-forge/linux-64/libnghttp2-1.68.1-h877daf1_0.conda + sha256: 663444d77a42f2265f54fb8b48c5450bfff4388d9c0f8253dd7855f0d993153f + md5: 2a45e7f8af083626f009645a6481f12d + depends: + - __glibc >=2.17,<3.0.a0 + - c-ares >=1.34.6,<2.0a0 + - libev >=4.33,<4.34.0a0 + - libev >=4.33,<5.0a0 + - libgcc >=14 + - libstdcxx >=14 + - libzlib >=1.3.1,<2.0a0 + - openssl >=3.5.5,<4.0a0 + license: MIT + license_family: MIT + run_exports: + weak: + - libnghttp2 >=1.68.1,<2.0a0 + size: 663344 + timestamp: 1773854035739 - conda: https://conda.anaconda.org/conda-forge/linux-64/libntlm-1.8-hb9d3cd8_0.conda sha256: 3b3f19ced060013c2dd99d9d46403be6d319d4601814c772a3472fe2955612b0 md5: 7c7927b404672409d9917d49bff5f2d6 @@ -1622,6 +1710,21 @@ packages: - libpq >=18.6,<19.0a0 size: 2719680 timestamp: 1786641133110 +- conda: https://conda.anaconda.org/conda-forge/linux-64/libpsl-0.23.1-hf670292_0.conda + sha256: 2e6405feb59e3f40a5a4641dfa73afda158046893aa73d478e23415e18997ece + md5: c8217f5bdd5b018087bdea081912cda5 + depends: + - libgcc >=14 + - libstdcxx >=14 + - __glibc >=2.17,<3.0.a0 + - icu >=78.3,<79.0a0 + license: MIT + license_family: MIT + run_exports: + weak: + - libpsl >=0.23.1,<0.24.0a0 + size: 72505 + timestamp: 1786443494718 - conda: https://conda.anaconda.org/conda-forge/linux-64/libraqm-0.11.0-h6406941_0.conda sha256: ab3ccb9fd5816f76b18ee8974d359cd2605ca87d8ddef55369dc461b9986f95c md5: 3ac89a48d224409739dbf6200e524373 @@ -1665,6 +1768,21 @@ packages: - libsqlite >=3.53.4,<4.0a0 size: 964200 timestamp: 1785016112246 +- conda: https://conda.anaconda.org/conda-forge/linux-64/libssh2-1.11.1-h6154650_1.conda + sha256: 7e463000fa1cba3f3a6597b776f6ab301d425a96e3bc20d0d9d76a3b9f237aa3 + md5: 5c526561e1d2a2e071941174eae84557 + depends: + - __glibc >=2.17,<3.0.a0 + - libgcc >=14 + - libzlib >=1.3.2,<2.0a0 + - openssl >=3.5.7,<4.0a0 + license: BSD-3-Clause + license_family: BSD + run_exports: + weak: + - libssh2 >=1.11.1,<2.0a0 + size: 306045 + timestamp: 1786713107897 - conda: https://conda.anaconda.org/conda-forge/linux-64/libstdcxx-16.1.0-h934c35e_1.conda sha256: 79721dd08aeb0ab9e773f1f9ef41cf4e6c17477e3d72319147619045bce05a09 md5: aed6cf89adc1e9b846e4367ac538e434 @@ -2010,6 +2128,37 @@ packages: - ncurses >=6.6,<7.0a0 size: 911196 timestamp: 1786355078102 +- conda: https://conda.anaconda.org/conda-forge/linux-64/nspr-4.40-h29cc59b_0.conda + sha256: 7cf473259ef9945ce19ecc898a2dd146cd32dc86099d87b86f79c5e5b0ef55f0 + md5: 4ebda462e16da6e6164cb82b9d58fcea + depends: + - __glibc >=2.17,<3.0.a0 + - libgcc >=14 + - libstdcxx >=14 + license: MPL-2.0 + license_family: MOZILLA + run_exports: + weak: + - nspr >=4.40,<5.0a0 + size: 231356 + timestamp: 1786154777346 +- conda: https://conda.anaconda.org/conda-forge/linux-64/nss-3.118-h445c969_0.conda + sha256: 44dd98ffeac859d84a6dcba79a2096193a42fc10b29b28a5115687a680dd6aea + md5: 567fbeed956c200c1db5782a424e58ee + depends: + - __glibc >=2.17,<3.0.a0 + - libgcc >=14 + - libsqlite >=3.51.0,<4.0a0 + - libstdcxx >=14 + - libzlib >=1.3.1,<2.0a0 + - nspr >=4.38,<5.0a0 + license: MPL-2.0 + license_family: MOZILLA + run_exports: + weak: + - nss >=3.118,<4.0a0 + size: 2057773 + timestamp: 1763485556350 - conda: https://conda.anaconda.org/conda-forge/linux-64/numpy-2.5.2-py314h2b28147_0.conda sha256: 124b753583ea9c157301fe78de3e88aa5fa8806bd2da8abaa8808065d1b93d51 md5: d77631addad93399a90157d2c597e7f3 @@ -2130,6 +2279,41 @@ packages: - pixman >=0.46.4,<1.0a0 size: 376704 timestamp: 1786106621354 +- conda: https://conda.anaconda.org/conda-forge/linux-64/poppler-26.07.0-he594abd_3.conda + sha256: 646869d659de20b98caf6e249b468d1a13b4593f5d83ce7f269fc75aed18faf0 + md5: 386b8320e1f218cb1a1d75edc7ac3599 + depends: + - __glibc >=2.17,<3.0.a0 + - cairo >=1.18.4,<2.0a0 + - fontconfig >=2.18.1,<3.0a0 + - fonts-conda-ecosystem + - lcms2 >=2.19.1,<3.0a0 + - libcurl >=8.21.0,<9.0a0 + - libegl >=1.7.0,<2.0a0 + - libfreetype >=2.14.3 + - libfreetype6 >=2.14.3 + - libgcc >=14 + - libgl >=1.7.0,<2.0a0 + - libglib >=2.88.2,<3.0a0 + - libiconv >=1.18,<2.0a0 + - libjpeg-turbo >=3.1.4.1,<4.0a0 + - libpng >=1.6.58,<1.7.0a0 + - libstdcxx >=14 + - libtiff >=4.7.2,<4.8.0a0 + - libzlib >=1.3.2,<2.0a0 + - nspr >=4.38,<5.0a0 + - nss >=3.118,<4.0a0 + - openjpeg >=2.5.4,<3.0a0 + - poppler-data + - xorg-libxscrnsaver >=1.2.4,<2.0a0 + - xorg-libxtst >=1.2.5,<2.0a0 + license: GPL-2.0-or-later + license_family: GPL + run_exports: + weak: + - poppler >=26.7.0,<26.8.0a0 + size: 2113458 + timestamp: 1783343328643 - conda: https://conda.anaconda.org/conda-forge/linux-64/pthread-stubs-0.4-hb03c661_1003.conda sha256: afc3b27b2cbb0487c1d0e963f96e71181ecfb623a24fb393bb19ff974a6382a1 md5: df2c27f36bdb0dde779f55b5df76a352 @@ -2677,6 +2861,21 @@ packages: - xorg-libxrender >=0.9.12,<0.10.0a0 size: 33005 timestamp: 1734229037766 +- conda: https://conda.anaconda.org/conda-forge/linux-64/xorg-libxscrnsaver-1.2.4-hb9d3cd8_0.conda + sha256: 58e8fc1687534124832d22e102f098b5401173212ac69eb9fd96b16a3e2c8cb2 + md5: 303f7a0e9e0cd7d250bb6b952cecda90 + depends: + - __glibc >=2.17,<3.0.a0 + - libgcc >=13 + - xorg-libx11 >=1.8.10,<2.0a0 + - xorg-libxext >=1.3.6,<2.0a0 + license: MIT + license_family: MIT + run_exports: + weak: + - xorg-libxscrnsaver >=1.2.4,<2.0a0 + size: 14412 + timestamp: 1727899730073 - conda: https://conda.anaconda.org/conda-forge/linux-64/xorg-libxtst-1.2.5-hb9d3cd8_3.conda sha256: 752fdaac5d58ed863bbf685bb6f98092fe1a488ea8ebb7ed7b606ccfce08637a md5: 7bbe9a0cc0df0ac5f5a8ad6d6a11af2f @@ -2997,6 +3196,14 @@ packages: run_exports: {} size: 116363 timestamp: 1785888127370 +- conda: https://conda.anaconda.org/conda-forge/noarch/poppler-data-0.4.12-hd8ed1ab_0.conda + sha256: 2f227e17b3c0346112815faa605502b66c1c4511a856127f2899abf15a98a2cf + md5: d8d7293c5b37f39b2ac32940621c6592 + license: BSD-3-Clause AND (GPL-2.0-only OR GPL-3.0-only) + license_family: OTHER + run_exports: {} + size: 2348171 + timestamp: 1675353652214 - conda: https://conda.anaconda.org/conda-forge/noarch/pycparser-3.0-pyhcf101f3_0.conda sha256: e27e0473fc6723311a0bd48b89b616fa1b996a2f7a2b555338cbbcfb9c640568 md5: 9c5491066224083c41b6d5635ed7107b @@ -3150,6 +3357,20 @@ packages: - bzip2 >=1.0.8,<2.0a0 size: 124965 timestamp: 1785906749812 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/c-ares-1.34.8-h1a92334_1.conda + sha256: 104b41473845649101ba8ebf8221c7431256465d34ce380b10e9a90558ed33ac + md5: 7d9390a4d4b43f91652823d870f68065 + depends: + - __osx >=11.0 + constrains: + - c-ares-static <0a0 + license: MIT + license_family: MIT + run_exports: + weak: + - c-ares >=1.34.8,<2.0a0 + size: 197274 + timestamp: 1786116660078 - conda: https://conda.anaconda.org/conda-forge/osx-arm64/cairo-1.18.4-he0f2337_1.conda sha256: cde9b79ee206fe3ba6ca2dc5906593fb7a1350515f85b2a1135a4ce8ec1539e3 md5: 36200ecfbbfbcb82063c87725434161f @@ -3412,6 +3633,25 @@ packages: - libcblas >=3.11.0,<4.0a0 size: 18110 timestamp: 1786058893756 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libcurl-8.21.0-hf618e03_4.conda + sha256: d25be36712d7f854a5f93513b9fbf1b0ee3219975b7fef4a3ee071b021b10167 + md5: 66cf9c5003ee81ecdb0dc9f9df17bbe5 + depends: + - __osx >=11.0 + - krb5 >=1.22.2,<1.23.0a0 + - libnghttp2 >=1.68.1,<2.0a0 + - libpsl >=0.23.0,<0.24.0a0 + - libssh2 >=1.11.1,<2.0a0 + - libzlib >=1.3.2,<2.0a0 + - openssl >=3.5.7,<4.0a0 + - zstd >=1.5.7,<1.6.0a0 + license: curl + license_family: MIT + run_exports: + weak: + - libcurl >=8.21.0,<9.0a0 + size: 411491 + timestamp: 1785500129743 - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libcxx-22.1.8-h55c6f16_0.conda sha256: a2e7abab5add9750fab064c024394de48e49f97631c605ad5db5c8ac3fc769ef md5: 89f76a2a21a3ec3ec983b5eb237c4113 @@ -3447,6 +3687,18 @@ packages: - libedit >=3.1.20250104,<3.2.0a0 size: 107742 timestamp: 1786616721640 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libev-4.33-h1a92334_3.conda + sha256: c0100064506ae8abb432c5a506d474f10af2cf48c33d62bc221fb28b6d6ff6ac + md5: 19e86c8a6a47e92bb2e70ca12e758c5c + depends: + - __osx >=11.0 + license: BSD-2-Clause OR GPL-2.0-or-later + license_family: GPL + run_exports: + weak: + - libev >=4.33,<4.34.0a0 + size: 39991 + timestamp: 1785917376956 - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libexpat-2.8.1-hf6b4638_1.conda sha256: 5af74261101e3c777399c6294b2b5d290e508153268eb2e9ff99c4d69834612f md5: a915151d5d3c5bf039f5ccc8402a436f @@ -3642,6 +3894,24 @@ packages: run_exports: {} size: 73690 timestamp: 1769482560514 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libnghttp2-1.68.1-h8f3e76b_0.conda + sha256: 2bc7bc3978066f2c274ebcbf711850cc9ab92e023e433b9631958a098d11e10a + md5: 6ea18834adbc3b33df9bd9fb45eaf95b + depends: + - __osx >=11.0 + - c-ares >=1.34.6,<2.0a0 + - libcxx >=19 + - libev >=4.33,<4.34.0a0 + - libev >=4.33,<5.0a0 + - libzlib >=1.3.1,<2.0a0 + - openssl >=3.5.5,<4.0a0 + license: MIT + license_family: MIT + run_exports: + weak: + - libnghttp2 >=1.68.1,<2.0a0 + size: 576526 + timestamp: 1773854624224 - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libopenblas-0.3.34-openmp_he657e61_0.conda sha256: bcf1967f12f1b1cc769dcc77b255fb1b27aaceb2f185450e9596d137bc6ede76 md5: 89d28fb841cf16211318524fa985e384 @@ -3671,6 +3941,20 @@ packages: - libpng >=1.6.58,<1.7.0a0 size: 289546 timestamp: 1776315246750 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libpsl-0.23.1-h7a62e17_0.conda + sha256: 0140cb0d059ac531e476b85a543668b69b7dffec16f0d26269ab6c9b70546921 + md5: 75a4cdf128d016141db61732ea462276 + depends: + - __osx >=11.0 + - libcxx >=19 + - icu >=78.3,<79.0a0 + license: MIT + license_family: MIT + run_exports: + weak: + - libpsl >=0.23.1,<0.24.0a0 + size: 72950 + timestamp: 1786443660105 - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libraqm-0.11.0-h45af499_0.conda sha256: b110f7f9b2cec61ea793c521950414e00cd64826e18aeb4ab40369acf05c0039 md5: 46ea0eb4e71bffa35ab7070678bcb053 @@ -3711,6 +3995,19 @@ packages: - libsqlite >=3.53.4,<4.0a0 size: 929203 timestamp: 1785016131414 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libssh2-1.11.1-hbf2880b_1.conda + sha256: a056e518c3d2d09fbb1778cea80c0c95338fb24adf1a817bbf90fb484850a304 + md5: d957a8eda98c49b073e8dcfd677c127a + depends: + - libzlib >=1.3.2,<2.0a0 + - openssl >=3.5.7,<4.0a0 + license: BSD-3-Clause + license_family: BSD + run_exports: + weak: + - libssh2 >=1.11.1,<2.0a0 + size: 280492 + timestamp: 1786714226814 - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libtiff-4.7.2-h282da08_0.conda sha256: 253153cabf9469170e02b21a6bc9aa598048e7c34966b1103af52d27d2a708a4 md5: 6fa87106b3c041ad6d965a9411e79b94 @@ -3839,6 +4136,35 @@ packages: - ncurses >=6.6,<7.0a0 size: 805509 timestamp: 1777423252320 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/nspr-4.40-hdcbdcf5_0.conda + sha256: 429549e611c625d1f90714646b94aa62531be258b614414a54f6a402f8124c4a + md5: 7697df3a4af3427af9bc66dfaec228ae + depends: + - __osx >=11.0 + - libcxx >=19 + license: MPL-2.0 + license_family: MOZILLA + run_exports: + weak: + - nspr >=4.40,<5.0a0 + size: 202842 + timestamp: 1786155286337 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/nss-3.118-h1c710a3_0.conda + sha256: d57f7b2cf2860a2a848e3dd43cc4f5488e60050a7d62af1834da3ee43911d9c4 + md5: ae07409126ced4f0d982dfeab0681016 + depends: + - __osx >=11.0 + - libcxx >=19 + - libsqlite >=3.51.0,<4.0a0 + - libzlib >=1.3.1,<2.0a0 + - nspr >=4.38,<5.0a0 + license: MPL-2.0 + license_family: MOZILLA + run_exports: + weak: + - nss >=3.118,<4.0a0 + size: 1839904 + timestamp: 1763486575227 - conda: https://conda.anaconda.org/conda-forge/osx-arm64/numpy-2.5.1-py314hb79c6fa_0.conda sha256: 3aa853ec05e6fe6b660be354fd05ab2a89b2e6cc6346612a6c75a18f38f62c3d md5: 3587914062d5537dde5fa8c2131cf624 @@ -3936,6 +4262,37 @@ packages: - pixman >=0.46.4,<1.0a0 size: 198717 timestamp: 1786106922508 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/poppler-26.07.0-h4cfec15_3.conda + sha256: c8784f244cc6cd33bf73b056ef04a3bc44be1d1257da4cf63381ffa358741fcf + md5: 0a477ba01d6418f51a114f63cb64a9b5 + depends: + - __osx >=11.0 + - cairo >=1.18.4,<2.0a0 + - fontconfig >=2.18.1,<3.0a0 + - fonts-conda-ecosystem + - lcms2 >=2.19.1,<3.0a0 + - libcurl >=8.21.0,<9.0a0 + - libcxx >=19 + - libfreetype >=2.14.3 + - libfreetype6 >=2.14.3 + - libglib >=2.88.2,<3.0a0 + - libiconv >=1.18,<2.0a0 + - libintl >=0.25.1,<1.0a0 + - libjpeg-turbo >=3.1.4.1,<4.0a0 + - libpng >=1.6.58,<1.7.0a0 + - libtiff >=4.7.2,<4.8.0a0 + - libzlib >=1.3.2,<2.0a0 + - nspr >=4.38,<5.0a0 + - nss >=3.118,<4.0a0 + - openjpeg >=2.5.4,<3.0a0 + - poppler-data + license: GPL-2.0-or-later + license_family: GPL + run_exports: + weak: + - poppler >=26.7.0,<26.8.0a0 + size: 1626453 + timestamp: 1783345166234 - conda: https://conda.anaconda.org/conda-forge/osx-arm64/pthread-stubs-0.4-h84a0fba_1003.conda sha256: 1c2338b9b0486af86883b9593d2f3e2845bbf216ea453dfe5d9a228e8dbe4057 md5: d4852e6054b74645cf49ca10f6349191 diff --git a/pixi.toml b/pixi.toml index 57f2c3e..22aab04 100644 --- a/pixi.toml +++ b/pixi.toml @@ -16,7 +16,20 @@ test-lfortran = "bash scripts/build_lfortran.sh && mkdir -p tests/out && ./build demo-lfortran = "bash scripts/build_lfortran.sh && ./build/lfortran/demo" mpl-refs = "python tests/gen_mpl_refs.py" -compare = "python tests/gen_mpl_refs.py && bash scripts/build_flang.sh && mkdir -p tests/out && ./build/flang/test_plots && python tests/compare_svgs.py" + +# The three comparisons share their setup, so it is expressed once as a +# dependency rather than repeated in each command and run three times. +[tasks.compare] +cmd = "python tests/compare_svgs.py" +depends-on = ["mpl-refs", "test-flang"] + +[tasks.compare-png] +cmd = "python tests/compare_pngs.py" +depends-on = ["mpl-refs", "test-flang"] + +[tasks.compare-pdf] +cmd = "python tests/compare_pdfs.py" +depends-on = ["mpl-refs", "test-flang"] [dependencies] python = ">=3.14.6,<3.15" @@ -24,6 +37,8 @@ matplotlib = ">=3.11.1,<4" numpy = ">=2.5.1,<3" cairosvg = ">=2.9.0,<3" pillow = ">=12.3.0,<13" +# pdftoppm, for rasterizing PDFs in tests/compare_pdfs.py +poppler = "*" lfortran = ">=0.64.0,<0.65" [target.linux-64.dependencies] diff --git a/scripts/build_flang.sh b/scripts/build_flang.sh index cfd926e..399b303 100755 --- a/scripts/build_flang.sh +++ b/scripts/build_flang.sh @@ -16,21 +16,28 @@ cd "$BUILD" # Compile modules in dependency order $FLANG -c "$ROOT/src/fplot_colors.f90" $FLANG -c "$ROOT/src/fplot_style.f90" +$FLANG -c "$ROOT/src/fplot_render.f90" $FLANG -c "$ROOT/src/fplot_scale.f90" $FLANG -c "$ROOT/src/fplot_cmap.f90" $FLANG -c "$ROOT/src/fplot_ticks.f90" $FLANG -c "$ROOT/src/fplot_contour.f90" $FLANG -c "$ROOT/src/fplot_svg.f90" +$FLANG -c "$ROOT/src/fplot_backend_svg.f90" +$FLANG -c "$ROOT/src/fplot_glyphs.f90" +$FLANG -c "$ROOT/src/fplot_png.f90" +$FLANG -c "$ROOT/src/fplot_raster.f90" +$FLANG -c "$ROOT/src/fplot_backend_pdf.f90" +$FLANG -c "$ROOT/src/fplot_backend_png.f90" $FLANG -c "$ROOT/src/fplot.f90" # Test program $FLANG -o "$BUILD/test_plots" \ "$ROOT/tests/test_plots.f90" \ - fplot_colors.o fplot_style.o fplot_scale.o fplot_cmap.o fplot_ticks.o fplot_contour.o fplot_svg.o fplot.o + fplot_colors.o fplot_style.o fplot_render.o fplot_scale.o fplot_cmap.o fplot_ticks.o fplot_contour.o fplot_svg.o fplot_backend_svg.o fplot_backend_pdf.o fplot_glyphs.o fplot_png.o fplot_raster.o fplot_backend_png.o fplot.o # Demo $FLANG -o "$BUILD/demo" \ "$ROOT/examples/demo.f90" \ - fplot_colors.o fplot_style.o fplot_scale.o fplot_cmap.o fplot_ticks.o fplot_contour.o fplot_svg.o fplot.o + fplot_colors.o fplot_style.o fplot_render.o fplot_scale.o fplot_cmap.o fplot_ticks.o fplot_contour.o fplot_svg.o fplot_backend_svg.o fplot_backend_pdf.o fplot_glyphs.o fplot_png.o fplot_raster.o fplot_backend_png.o fplot.o echo "Built: $BUILD/test_plots $BUILD/demo" diff --git a/scripts/build_lfortran.sh b/scripts/build_lfortran.sh index bd88d93..72fc696 100755 --- a/scripts/build_lfortran.sh +++ b/scripts/build_lfortran.sh @@ -13,20 +13,27 @@ cd "$BUILD" # Prefer a single shared-module build $LFORTRAN -c "$ROOT/src/fplot_colors.f90" $LFORTRAN -c "$ROOT/src/fplot_style.f90" +$LFORTRAN -c "$ROOT/src/fplot_render.f90" $LFORTRAN -c "$ROOT/src/fplot_scale.f90" $LFORTRAN -c "$ROOT/src/fplot_cmap.f90" $LFORTRAN -c "$ROOT/src/fplot_ticks.f90" $LFORTRAN -c "$ROOT/src/fplot_contour.f90" $LFORTRAN -c "$ROOT/src/fplot_svg.f90" +$LFORTRAN -c "$ROOT/src/fplot_backend_svg.f90" +$LFORTRAN -c "$ROOT/src/fplot_glyphs.f90" +$LFORTRAN -c "$ROOT/src/fplot_png.f90" +$LFORTRAN -c "$ROOT/src/fplot_raster.f90" +$LFORTRAN -c "$ROOT/src/fplot_backend_pdf.f90" +$LFORTRAN -c "$ROOT/src/fplot_backend_png.f90" $LFORTRAN -c "$ROOT/src/fplot.f90" $LFORTRAN -o "$BUILD/test_plots" \ "$ROOT/tests/test_plots.f90" \ - fplot_colors.o fplot_style.o fplot_scale.o fplot_cmap.o fplot_ticks.o fplot_contour.o fplot_svg.o fplot.o + fplot_colors.o fplot_style.o fplot_render.o fplot_scale.o fplot_cmap.o fplot_ticks.o fplot_contour.o fplot_svg.o fplot_backend_svg.o fplot_backend_pdf.o fplot_glyphs.o fplot_png.o fplot_raster.o fplot_backend_png.o fplot.o $LFORTRAN -o "$BUILD/demo" \ "$ROOT/examples/demo.f90" \ - fplot_colors.o fplot_style.o fplot_scale.o fplot_cmap.o fplot_ticks.o fplot_contour.o fplot_svg.o fplot.o + fplot_colors.o fplot_style.o fplot_render.o fplot_scale.o fplot_cmap.o fplot_ticks.o fplot_contour.o fplot_svg.o fplot_backend_svg.o fplot_backend_pdf.o fplot_glyphs.o fplot_png.o fplot_raster.o fplot_backend_png.o fplot.o echo "Built: $BUILD/test_plots $BUILD/demo" echo "Jupyter: use display_data('image/svg+xml', render_svg()) with lfortran_display" diff --git a/src/fplot.f90 b/src/fplot.f90 index 8e151f4..f24761d 100644 --- a/src/fplot.f90 +++ b/src/fplot.f90 @@ -7,6 +7,10 @@ module fplot use fplot_contour use fplot_ticks use fplot_svg + use fplot_render + use fplot_backend_svg + use fplot_backend_pdf + use fplot_backend_png implicit none private @@ -21,7 +25,7 @@ module fplot public :: imshow, colorbar, contour, contourf public :: title, xlabel, ylabel, grid, legend public :: xlim, ylim, clf, savefig, show, figure - public :: render_svg + public :: render_svg, render_pdf, render_png public :: subplot, suptitle, subplots_adjust, tight_layout public :: twinx, twiny public :: set_fontsize @@ -218,6 +222,9 @@ module fplot real(dp), save :: def_title = TITLE_FONT, def_label = LABEL_FONT real(dp), save :: def_tick = TICK_FONT, def_legend = LEGEND_FONT real(dp), save :: fig_suptitle_size = SUPTITLE_FONT + + ! Clipping region currently in force. See set_clip. + type(clip_t), save :: g_clip type(axes_t), allocatable, save :: ax(:) integer, save :: n_ax = 0 integer, save :: cur_i = 0 @@ -2185,15 +2192,6 @@ pure function axis_frac(v, vmin, vmax, sc) result(t) end if end function axis_frac - subroutine append_num(b, x) - type(svg_builder), intent(inout) :: b - real(dp), intent(in) :: x - character(len=64) :: s - integer :: n - call fmt_num(x, s, n) - call builder_append(b, s(1:n)) - end subroutine append_num - pure function int_to_str(i) result(s) integer, intent(in) :: i character(len=:), allocatable :: s @@ -2202,243 +2200,354 @@ pure function int_to_str(i) result(s) s = trim(tmp) end function int_to_str - subroutine append_dash(b, ls) - type(svg_builder), intent(inout) :: b + ! fplot carries colors as "#rrggbb" because that is what a format string + ! and a colormap table both produce; the renderer wants components. + pure function hex_rgb(s) result(c) + character(len=*), intent(in) :: s + integer :: c(3), i + c = 0 + if (len(s) < 7) return + do i = 1, 3 + c(i) = hex_byte(s(2*i:2*i + 1)) + end do + end function hex_rgb + + ! The clip in force for the primitives being emitted now. The rendering + ! API is stateless by design, but fplot draws in regions ("everything + ! inside the axes box"), so the front end tracks the current region here + ! and stamps it into each paint rather than passing it through fifteen + ! layers of call. + subroutine set_clip(x, y, w, h) + real(dp), intent(in) :: x, y, w, h + g_clip%on = .true. + g_clip%x = x + g_clip%y = y + g_clip%w = w + g_clip%h = h + end subroutine set_clip + + subroutine clear_clip() + g_clip%on = .false. + end subroutine clear_clip + + ! Stroke-only paint. + function pen(color, lw, alpha, ls) result(p) + character(len=*), intent(in) :: color + real(dp), intent(in) :: lw + real(dp), intent(in), optional :: alpha + integer, intent(in), optional :: ls + type(paint_t) :: p + p%filled = .false. + p%stroked = .true. + p%stroke_rgb = hex_rgb(color) + p%line_width = lw + if (present(alpha)) p%stroke_alpha = alpha + if (present(ls)) call set_dash(p, ls) + p%clip = g_clip + end function pen + + ! Fill-only paint. + function brush(color, alpha) result(p) + character(len=*), intent(in) :: color + real(dp), intent(in), optional :: alpha + type(paint_t) :: p + p%filled = .true. + p%stroked = .false. + p%fill_rgb = hex_rgb(color) + if (present(alpha)) p%fill_alpha = alpha + p%clip = g_clip + end function brush + + ! matplotlib's dash patterns, in points, for its four line styles. + subroutine set_dash(p, ls) + type(paint_t), intent(inout) :: p integer, intent(in) :: ls select case (ls) case (LINE_DASHED) - call builder_append(b, ' stroke-dasharray="5.55,2.4"') + p%n_dash = 2 + p%dash(1:2) = [5.55_dp, 2.4_dp] case (LINE_DOTTED) - call builder_append(b, ' stroke-dasharray="1.5,2.475"') + p%n_dash = 2 + p%dash(1:2) = [1.5_dp, 2.475_dp] case (LINE_DASHDOT) - call builder_append(b, ' stroke-dasharray="9.9,2.4,1.5,2.4"') + p%n_dash = 4 + p%dash(1:4) = [9.9_dp, 2.4_dp, 1.5_dp, 2.4_dp] end select - end subroutine append_dash - - ! Emit a stroked open path through the given points, e.g. an "x" or "+". - ! matplotlib writes stroke-opacity/fill-opacity per element rather than a - ! group opacity; emitting nothing at alpha == 1 keeps the common case terse. - subroutine append_opacity(b, attr, alpha) - type(svg_builder), intent(inout) :: b - character(len=*), intent(in) :: attr - real(dp), intent(in) :: alpha - if (alpha >= 1.0_dp) return - call builder_append(b, '" ') - call builder_append(b, attr) - call builder_append(b, '="') - call append_num(b, alpha) - end subroutine append_opacity + end subroutine set_dash + + ! Verbs for an open polyline through np points. + pure function line_verbs(np) result(v) + integer, intent(in) :: np + integer :: v(np), i + v(1) = VERB_MOVE + do i = 2, np + v(i) = VERB_LINE + end do + end function line_verbs subroutine append_stroke_path(b, px, py, np, color, lw, alpha) - type(svg_builder), intent(inout) :: b + class(renderer_t), intent(inout) :: b real(dp), intent(in) :: px(:), py(:) integer, intent(in) :: np character(len=*), intent(in) :: color real(dp), intent(in) :: lw, alpha - integer :: i - call builder_append(b, '') - call builder_append(b, new_line("a")) + if (np < 2) return + call b%draw_path(px(1:np), py(1:np), line_verbs(np), np, & + pen(color, lw, alpha)) end subroutine append_stroke_path - ! Emit a filled closed polygon, used by the shaped markers and by - ! fill_between. + ! A filled closed polygon, used by the shaped markers and by fill_between. subroutine append_polygon(b, px, py, np, color, alpha, seal) - type(svg_builder), intent(inout) :: b + class(renderer_t), intent(inout) :: b real(dp), intent(in) :: px(:), py(:) integer, intent(in) :: np character(len=*), intent(in) :: color real(dp), intent(in) :: alpha logical, intent(in), optional :: seal - integer :: i - call builder_append(b, '') - call builder_append(b, new_line("a")) + call b%draw_path(px(1:np), py(1:np), v, np + 1, p) end subroutine append_polygon - ! Draw one marker of kind mk centred at (cx, cy), sized like a matplotlib - ! marker of markersize ms. - subroutine append_marker(b, mk, cx, cy, ms, color, alpha) - type(svg_builder), intent(inout) :: b + ! The outline of one marker, described about the origin, together with + ! how it should be painted. Handing the renderer a shape plus a list of + ! positions lets every backend name the outline once and then reference + ! it: an SVG , a PDF form XObject, a cached sprite in PNG. + subroutine marker_shape(mk, ms, mx, my, mv, nv, np, do_fill, do_stroke, lw) integer, intent(in) :: mk - real(dp), intent(in) :: cx, cy, ms - character(len=*), intent(in) :: color - real(dp), intent(in) :: alpha - real(dp) :: r, xs(11), ys(11) + real(dp), intent(in) :: ms + real(dp), intent(out) :: mx(:), my(:) + integer, intent(out) :: mv(:), nv, np + logical, intent(out) :: do_fill, do_stroke + real(dp), intent(out) :: lw + real(dp) :: r, k, ang integer :: i - real(dp) :: ang + + do_fill = .true. + do_stroke = .false. + lw = 1.0_dp + nv = 0 + np = 0 select case (mk) case (MARKER_CIRCLE, MARKER_POINT) if (mk == MARKER_POINT) then - r = 0.5_dp * ms * 0.35_dp + r = 0.5_dp*ms*0.35_dp else - r = 0.5_dp * ms * 0.75_dp + r = 0.5_dp*ms*0.75_dp + do_stroke = .true. end if - call builder_append(b, '') - else - call builder_append(b, '"/>') - end if - call builder_append(b, new_line("a")) + ! Four cubics with the standard offset match a circle to about a + ! part in ten thousand, which is far below a pixel here. + k = r*0.5522847498307933_dp + mx(1) = r; my(1) = 0.0_dp + mx(2) = r; my(2) = k + mx(3) = k; my(3) = r + mx(4) = 0.0_dp; my(4) = r + mx(5) = -k; my(5) = r + mx(6) = -r; my(6) = k + mx(7) = -r; my(7) = 0.0_dp + mx(8) = -r; my(8) = -k + mx(9) = -k; my(9) = -r + mx(10) = 0.0_dp; my(10) = -r + mx(11) = k; my(11) = -r + mx(12) = r; my(12) = -k + mx(13) = r; my(13) = 0.0_dp + np = 13 + mv(1:6) = [VERB_MOVE, VERB_CUBIC, VERB_CUBIC, VERB_CUBIC, & + VERB_CUBIC, VERB_CLOSE] + nv = 6 case (MARKER_X) - r = 0.5_dp * ms * 0.7_dp - call append_stroke_path(b, [cx - r, cx + r], [cy - r, cy + r], 2, color, 1.5_dp, alpha) - call append_stroke_path(b, [cx - r, cx + r], [cy + r, cy - r], 2, color, 1.5_dp, alpha) + r = 0.5_dp*ms*0.7_dp + mx(1:4) = [-r, r, -r, r] + my(1:4) = [-r, r, r, -r] + mv(1:4) = [VERB_MOVE, VERB_LINE, VERB_MOVE, VERB_LINE] + np = 4 + nv = 4 + do_fill = .false. + do_stroke = .true. + lw = 1.5_dp case (MARKER_PLUS) - r = 0.5_dp * ms * 0.75_dp - call append_stroke_path(b, [cx - r, cx + r], [cy, cy], 2, color, 1.5_dp, alpha) - call append_stroke_path(b, [cx, cx], [cy - r, cy + r], 2, color, 1.5_dp, alpha) + r = 0.5_dp*ms*0.75_dp + mx(1:4) = [-r, r, 0.0_dp, 0.0_dp] + my(1:4) = [0.0_dp, 0.0_dp, -r, r] + mv(1:4) = [VERB_MOVE, VERB_LINE, VERB_MOVE, VERB_LINE] + np = 4 + nv = 4 + do_fill = .false. + do_stroke = .true. + lw = 1.5_dp case (MARKER_SQUARE) - r = 0.5_dp * ms * 0.75_dp - call append_polygon(b, [cx - r, cx + r, cx + r, cx - r], & - [cy - r, cy - r, cy + r, cy + r], 4, color, alpha) + r = 0.5_dp*ms*0.75_dp + mx(1:4) = [-r, r, r, -r] + my(1:4) = [-r, -r, r, r] + np = 4 case (MARKER_DIAMOND) - r = 0.5_dp * ms * 0.75_dp - call append_polygon(b, [cx, cx + r, cx, cx - r], & - [cy - r, cy, cy + r, cy], 4, color, alpha) + r = 0.5_dp*ms*0.75_dp + mx(1:4) = [0.0_dp, r, 0.0_dp, -r] + my(1:4) = [-r, 0.0_dp, r, 0.0_dp] + np = 4 case (MARKER_TRI_UP) - r = 0.5_dp * ms * 0.85_dp - call append_polygon(b, [cx, cx + r, cx - r], & - [cy - r, cy + r, cy + r], 3, color, alpha) + r = 0.5_dp*ms*0.85_dp + mx(1:3) = [0.0_dp, r, -r] + my(1:3) = [-r, r, r] + np = 3 case (MARKER_TRI_DOWN) - r = 0.5_dp * ms * 0.85_dp - call append_polygon(b, [cx, cx + r, cx - r], & - [cy + r, cy - r, cy - r], 3, color, alpha) + r = 0.5_dp*ms*0.85_dp + mx(1:3) = [0.0_dp, r, -r] + my(1:3) = [r, -r, -r] + np = 3 case (MARKER_TRI_LEFT) - r = 0.5_dp * ms * 0.85_dp - call append_polygon(b, [cx - r, cx + r, cx + r], & - [cy, cy - r, cy + r], 3, color, alpha) + r = 0.5_dp*ms*0.85_dp + mx(1:3) = [-r, r, r] + my(1:3) = [0.0_dp, -r, r] + np = 3 case (MARKER_TRI_RIGHT) - r = 0.5_dp * ms * 0.85_dp - call append_polygon(b, [cx + r, cx - r, cx - r], & - [cy, cy - r, cy + r], 3, color, alpha) + r = 0.5_dp*ms*0.85_dp + mx(1:3) = [r, -r, -r] + my(1:3) = [0.0_dp, -r, r] + np = 3 case (MARKER_STAR) ! Five-pointed star: alternate outer and inner vertices. - r = 0.5_dp * ms * 0.95_dp + r = 0.5_dp*ms*0.95_dp do i = 1, 10 - ang = -0.5_dp * PI + real(i - 1, dp) * PI / 5.0_dp + ang = -0.5_dp*PI + real(i - 1, dp)*PI/5.0_dp if (mod(i, 2) == 1) then - xs(i) = cx + r * cos(ang) - ys(i) = cy + r * sin(ang) + mx(i) = r*cos(ang) + my(i) = r*sin(ang) else - xs(i) = cx + 0.4_dp * r * cos(ang) - ys(i) = cy + 0.4_dp * r * sin(ang) + mx(i) = 0.4_dp*r*cos(ang) + my(i) = 0.4_dp*r*sin(ang) end if end do - call append_polygon(b, xs, ys, 10, color, alpha) + np = 10 end select + + ! The polygonal markers all share one closed outline. + if (nv == 0 .and. np > 0) then + mv(1:np) = line_verbs(np) + mv(np + 1) = VERB_CLOSE + nv = np + 1 + end if + end subroutine marker_shape + + ! Draw the same marker at every point of x/y. + subroutine append_markers(b, mk, x, y, n, ms, color, alpha) + class(renderer_t), intent(inout) :: b + integer, intent(in) :: mk, n + real(dp), intent(in) :: x(:), y(:), ms, alpha + character(len=*), intent(in) :: color + real(dp) :: mx(16), my(16), lw + integer :: mv(16), nv, np + logical :: do_fill, do_stroke + type(paint_t) :: p + + if (mk == MARKER_NONE .or. n <= 0) return + call marker_shape(mk, ms, mx, my, mv, nv, np, do_fill, do_stroke, lw) + if (nv <= 0) return + + p%filled = do_fill + p%stroked = do_stroke + p%fill_rgb = hex_rgb(color) + p%stroke_rgb = p%fill_rgb + p%fill_alpha = alpha + p%stroke_alpha = alpha + p%line_width = lw + p%clip = g_clip + call b%draw_markers(x(1:n), y(1:n), mx(1:np), my(1:np), mv(1:nv), nv, p) + end subroutine append_markers + + ! One marker, for the legend key and anywhere else a single point is + ! drawn on its own. + subroutine append_marker(b, mk, cx, cy, ms, color, alpha) + class(renderer_t), intent(inout) :: b + integer, intent(in) :: mk + real(dp), intent(in) :: cx, cy, ms + character(len=*), intent(in) :: color + real(dp), intent(in) :: alpha + call append_markers(b, mk, [cx], [cy], 1, ms, color, alpha) end subroutine append_marker ! Text element. anchor is a matplotlib horizontal alignment - ! (left/center/right) or an SVG anchor (start/middle/end). - subroutine append_text(b, x, y, s, anchor, fontsize, color, transform) - type(svg_builder), intent(inout) :: b + ! (left/center/right) or an SVG anchor (start/middle/end). rot is a + ! matplotlib rotation, counterclockwise in degrees. + subroutine append_text(b, x, y, s, anchor, fontsize, color, rot) + class(renderer_t), intent(inout) :: b real(dp), intent(in) :: x, y, fontsize character(len=*), intent(in) :: s, anchor, color - character(len=*), intent(in), optional :: transform + real(dp), intent(in), optional :: rot + type(font_t) :: f + integer :: an + real(dp) :: ang - call builder_append(b, '') - call builder_append(b, s) - call builder_append(b, "") - call builder_append(b, new_line("a")) + f%size = fontsize + ang = 0.0_dp + ! The API turns angles clockwise, matplotlib counterclockwise. + if (present(rot)) ang = -rot + call b%draw_text(x, y, s, f, brush(color), an, BASE_ALPHABETIC, ang) end subroutine append_text ! Straight line segment in pixel coordinates. subroutine append_line(b, x1, y1, x2, y2, color, lw, ls, alpha) - type(svg_builder), intent(inout) :: b + class(renderer_t), intent(inout) :: b real(dp), intent(in) :: x1, y1, x2, y2, lw character(len=*), intent(in) :: color integer, intent(in) :: ls real(dp), intent(in) :: alpha if (ls == LINE_NONE) return - call builder_append(b, '") - call builder_append(b, new_line("a")) + call b%draw_path([x1, x2], [y1, y2], [VERB_MOVE, VERB_LINE], 2, & + pen(color, lw, alpha, ls)) end subroutine append_line + ! An axis aligned filled rectangle, optionally outlined. + subroutine append_rect(b, x, y, w, h, color, alpha, edge, elw) + class(renderer_t), intent(inout) :: b + real(dp), intent(in) :: x, y, w, h + character(len=*), intent(in) :: color + real(dp), intent(in), optional :: alpha + character(len=*), intent(in), optional :: edge + real(dp), intent(in), optional :: elw + type(paint_t) :: p + + p%clip = g_clip + if (len_trim(color) > 0) then + p%filled = .true. + p%fill_rgb = hex_rgb(color) + if (present(alpha)) p%fill_alpha = alpha + end if + if (present(edge)) then + p%stroked = .true. + p%stroke_rgb = hex_rgb(edge) + p%line_width = 1.0_dp + if (present(elw)) p%line_width = elw + end if + call b%draw_rect(x, y, w, h, p) + end subroutine append_rect + ! One bar of a bar/hist series, drawn from the y = 0 baseline. subroutine append_bar(b, s, j, xmin, xmax, ymin, ymax, ax_l, ax_w, ax_b, ax_h, xsc, ysc) - type(svg_builder), intent(inout) :: b + class(renderer_t), intent(inout) :: b type(series_t), intent(in) :: s integer, intent(in) :: j real(dp), intent(in) :: xmin, xmax, ymin, ymax, ax_l, ax_w, ax_b, ax_h @@ -2460,24 +2569,13 @@ subroutine append_bar(b, s, j, xmin, xmax, ymin, ymax, ax_l, ax_w, ax_b, ax_h, x yb = map_y(s%y(j), ymin, ymax, ax_b, ax_h, ysc) end if - call builder_append(b, '') - call builder_append(b, new_line("a")) + call append_rect(b, min(xa, xb), min(ya, yb), abs(xb - xa), & + abs(yb - ya), trim(s%color), s%alpha, "#ffffff", 0.5_dp) end subroutine append_bar ! Shaded region between y and y2, as a single closed polygon. subroutine append_fill(b, s, xmin, xmax, ymin, ymax, ax_l, ax_w, ax_b, ax_h, xsc, ysc) - type(svg_builder), intent(inout) :: b + class(renderer_t), intent(inout) :: b type(series_t), intent(in) :: s real(dp), intent(in) :: xmin, xmax, ymin, ymax, ax_l, ax_w, ax_b, ax_h type(scale_t), intent(in) :: xsc, ysc @@ -2500,7 +2598,7 @@ end subroutine append_fill ! Vertical error bar with caps for point j. subroutine append_errorbar(b, s, j, xmin, xmax, ymin, ymax, ax_l, ax_w, ax_b, ax_h, xsc, ysc) - type(svg_builder), intent(inout) :: b + class(renderer_t), intent(inout) :: b type(series_t), intent(in) :: s integer, intent(in) :: j real(dp), intent(in) :: xmin, xmax, ymin, ymax, ax_l, ax_w, ax_b, ax_h @@ -2583,7 +2681,7 @@ end subroutine push_minor ! Box, median, whiskers at 1.5 IQR clipped to the data, and the outliers ! beyond them as open circles. subroutine append_box(b, s, xmin, xmax, ymin, ymax, ax_l, ax_w, ax_b, ax_h, xsc, ysc) - type(svg_builder), intent(inout) :: b + class(renderer_t), intent(inout) :: b type(series_t), intent(in) :: s real(dp), intent(in) :: xmin, xmax, ymin, ymax, ax_l, ax_w, ax_b, ax_h type(scale_t), intent(in) :: xsc, ysc @@ -2639,25 +2737,16 @@ subroutine append_box(b, s, xmin, xmax, ymin, ymax, ax_l, ax_w, ax_b, ax_h, xsc, end subroutine append_box subroutine append_open_circle(b, cx, cy, r, color) - type(svg_builder), intent(inout) :: b + class(renderer_t), intent(inout) :: b real(dp), intent(in) :: cx, cy, r character(len=*), intent(in) :: color - call builder_append(b, '') - call builder_append(b, new_line("a")) + call b%draw_circle(cx, cy, r, pen(color, 1.0_dp)) end subroutine append_open_circle ! Mirrored Gaussian kernel density estimate, plus the min/max/range bars ! matplotlib draws over it. subroutine append_violin(b, s, xmin, xmax, ymin, ymax, ax_l, ax_w, ax_b, ax_h, xsc, ysc) - type(svg_builder), intent(inout) :: b + class(renderer_t), intent(inout) :: b type(series_t), intent(in) :: s real(dp), intent(in) :: xmin, xmax, ymin, ymax, ax_l, ax_w, ax_b, ax_h type(scale_t), intent(in) :: xsc, ysc @@ -2715,7 +2804,7 @@ end subroutine append_violin ! One per wedge: a radius out, the arc, and back to the centre. subroutine append_pie(b, s, xmin, xmax, ymin, ymax, ax_l, ax_w, ax_b, ax_h) - type(svg_builder), intent(inout) :: b + class(renderer_t), intent(inout) :: b type(series_t), intent(in) :: s real(dp), intent(in) :: xmin, xmax, ymin, ymax, ax_l, ax_w, ax_b, ax_h integer :: i @@ -2730,41 +2819,67 @@ subroutine append_pie(b, s, xmin, xmax, ymin, ymax, ax_l, ax_w, ax_b, ax_h) do i = 1, s%n a0 = a1 a1 = a0 + 2.0_dp * PI * s%y(i) / tot - call builder_append(b, ' PI)) - call builder_append(b, " 0 ") - call append_wedge_point(b, a1, xmin, xmax, ymin, ymax, ax_l, ax_w, ax_b, ax_h) - call builder_append(b, ' Z" fill="') - call builder_append(b, trim(s%pcolor(i))) - call append_opacity(b, "fill-opacity", s%alpha) - call builder_append(b, '" stroke="#ffffff" stroke-width="1"/>') - call builder_append(b, new_line("a")) + call append_wedge(b, cx, cy, ax_w/(xmax - xmin), ax_h/(ymax - ymin), & + a0, a1, trim(s%pcolor(i)), s%alpha) end do end subroutine append_pie - subroutine append_wedge_point(b, ang, xmin, xmax, ymin, ymax, ax_l, ax_w, ax_b, ax_h) - type(svg_builder), intent(inout) :: b - real(dp), intent(in) :: ang, xmin, xmax, ymin, ymax, ax_l, ax_w, ax_b, ax_h - call append_num(b, map_x(cos(ang), xmin, xmax, ax_l, ax_w, linear_scale)) - call builder_append(b, " ") - call append_num(b, map_y(sin(ang), ymin, ymax, ax_b, ax_h, linear_scale)) - end subroutine append_wedge_point + ! One wedge: out along a radius, round the rim, back to the centre. + ! + ! The rim is emitted as cubics rather than as an SVG arc. Only SVG has an + ! arc primitive, so flattening here is what lets the same wedge reach a + ! PDF or a rasterizer unchanged. Splitting at 90 degrees keeps the error + ! of the standard cubic approximation far below a pixel. + subroutine append_wedge(b, cx, cy, rx, ry, a0, a1, color, alpha) + class(renderer_t), intent(inout) :: b + real(dp), intent(in) :: cx, cy, rx, ry, a0, a1, alpha + character(len=*), intent(in) :: color + integer, parameter :: MAXSEG = 8 + real(dp) :: px(2 + 3*MAXSEG), py(2 + 3*MAXSEG) + integer :: verbs(3 + MAXSEG), np, nv, nseg, i + real(dp) :: t0, t1, dt, al + type(paint_t) :: p + + nseg = max(1, min(MAXSEG, int(abs(a1 - a0)/(0.5_dp*PI)) + 1)) + dt = (a1 - a0)/real(nseg, dp) + ! y grows downwards on the canvas, so the sine term is subtracted. + px(1) = cx + py(1) = cy + px(2) = cx + rx*cos(a0) + py(2) = cy - ry*sin(a0) + np = 2 + verbs(1) = VERB_MOVE + verbs(2) = VERB_LINE + nv = 2 + + al = 4.0_dp/3.0_dp*tan(0.25_dp*dt) + do i = 1, nseg + t0 = a0 + real(i - 1, dp)*dt + t1 = t0 + dt + ! Control points ride the tangents at each end of the segment. + px(np + 1) = cx + rx*cos(t0) + al*(-rx*sin(t0)) + py(np + 1) = cy - ry*sin(t0) + al*(-ry*cos(t0)) + px(np + 2) = cx + rx*cos(t1) - al*(-rx*sin(t1)) + py(np + 2) = cy - ry*sin(t1) - al*(-ry*cos(t1)) + px(np + 3) = cx + rx*cos(t1) + py(np + 3) = cy - ry*sin(t1) + np = np + 3 + nv = nv + 1 + verbs(nv) = VERB_CUBIC + end do + nv = nv + 1 + verbs(nv) = VERB_CLOSE + + p = brush(color, alpha) + p%stroked = .true. + p%stroke_rgb = hex_rgb("#ffffff") + p%line_width = 1.0_dp + call b%draw_path(px(1:np), py(1:np), verbs(1:nv), nv, p) + end subroutine append_wedge ! Walk every cell as two triangles, emitting filled bands or level lines. subroutine append_contour(b, a, xmin, xmax, ymin, ymax, ax_l, ax_w, ax_b, ax_h, xsc, ysc) - type(svg_builder), intent(inout) :: b + class(renderer_t), intent(inout) :: b type(axes_t), intent(in) :: a real(dp), intent(in) :: xmin, xmax, ymin, ymax, ax_l, ax_w, ax_b, ax_h type(scale_t), intent(in) :: xsc, ysc @@ -2844,7 +2959,7 @@ end subroutine cell_triangle ! embedding an encoded image, and nearest-neighbour cells are what ! matplotlib's default interpolation looks like at these sizes anyway. subroutine append_image(b, a, xmin, xmax, ymin, ymax, ax_l, ax_w, ax_b, ax_h, xsc, ysc) - type(svg_builder), intent(inout) :: b + class(renderer_t), intent(inout) :: b type(axes_t), intent(in) :: a real(dp), intent(in) :: xmin, xmax, ymin, ymax, ax_l, ax_w, ax_b, ax_h type(scale_t), intent(in) :: xsc, ysc @@ -2877,27 +2992,17 @@ end subroutine append_image ! Cells are grown by a hairline so that neighbours overlap; without it the ! renderer leaves visible seams between abutting rectangles. subroutine append_cell(b, x, y, w, h, color) - type(svg_builder), intent(inout) :: b + class(renderer_t), intent(inout) :: b real(dp), intent(in) :: x, y, w, h character(len=*), intent(in) :: color real(dp), parameter :: BLEED = 0.05_dp - call builder_append(b, '') - call builder_append(b, new_line("a")) + call append_rect(b, x - BLEED, y - BLEED, w + 2.0_dp*BLEED, & + h + 2.0_dp*BLEED, color) end subroutine append_cell ! Vertical gradient strip plus its own frame, ticks and labels. subroutine append_colorbar(b, a, idx, W, H) - type(svg_builder), intent(inout) :: b + class(renderer_t), intent(inout) :: b type(axes_t), intent(in) :: a integer, intent(in) :: idx real(dp), intent(in) :: W, H @@ -2918,36 +3023,18 @@ subroutine append_colorbar(b, a, idx, W, H) bb = (1.0_dp - a%bottom) * H bh = bb - bt - call builder_append(b, '') - call builder_append(b, new_line("a")) - + ! Slices are grown slightly so they abut without seams, so keep them + ! inside the bar. + call set_clip(bx, bt, bw, bh) do i = 1, CBAR_SLICES y1 = bb - real(i - 1, dp) * bh / real(CBAR_SLICES, dp) y0 = bb - real(i, dp) * bh / real(CBAR_SLICES, dp) t = (real(i, dp) - 0.5_dp) / real(CBAR_SLICES, dp) call append_cell(b, bx, y0, bw, y1 - y0, cmap_color(a%img_cmap, t)) end do + call clear_clip() - call builder_append(b, '') - call builder_append(b, new_line("a")) + call b%draw_rect(bx, bt, bw, bh, pen("#000000", 0.8_dp)) lo = a%img_vmin hi = a%img_vmax @@ -2963,11 +3050,8 @@ subroutine append_colorbar(b, a, idx, W, H) end do if (len_trim(a%cbar_label) > 0) then - call xml_escape_to(a%cbar_label, esc, ln) - call append_text(b, bx + bw + 34.0_dp, 0.5_dp * (bt + bb), esc(1:ln), & - "center", a%ylabel_size, "#000000", & - "rotate(-90 " // trim(fmt_pt(bx + bw + 34.0_dp)) // " " // & - trim(fmt_pt(0.5_dp * (bt + bb))) // ")") + call append_text(b, bx + bw + 34.0_dp, 0.5_dp * (bt + bb), trim(a%cbar_label), & + "center", a%ylabel_size, "#000000", 90.0_dp) end if end subroutine append_colorbar @@ -3058,22 +3142,13 @@ subroutine legend_origin(loc, ax_l, ax_r, ax_t, ax_b, leg_w, leg_h, leg_x, leg_y end subroutine legend_origin subroutine append_tick(b, x1, y1, x2, y2) - type(svg_builder), intent(inout) :: b + class(renderer_t), intent(inout) :: b real(dp), intent(in) :: x1, y1, x2, y2 - call builder_append(b, '') - call builder_append(b, new_line("a")) + call append_line(b, x1, y1, x2, y2, "#000000", 0.8_dp, LINE_SOLID, 1.0_dp) end subroutine append_tick subroutine append_spine(b, x1, y1, x2, y2) - type(svg_builder), intent(inout) :: b + class(renderer_t), intent(inout) :: b real(dp), intent(in) :: x1, y1, x2, y2 call append_line(b, x1, y1, x2, y2, "#000000", 0.8_dp, LINE_SOLID, 1.0_dp) end subroutine append_spine @@ -3081,7 +3156,7 @@ end subroutine append_spine ! A tick at (x, y) on a spine whose outward normal is (ox, oy). dir 1 ! puts it outside the axes, -1 inside, 0 straddling the spine. subroutine append_tick_at(b, x, y, ox, oy, dir, length) - type(svg_builder), intent(inout) :: b + class(renderer_t), intent(inout) :: b real(dp), intent(in) :: x, y, ox, oy, dir, length real(dp) :: a, c if (dir > 0.0_dp) then @@ -3097,25 +3172,12 @@ subroutine append_tick_at(b, x, y, ox, oy, dir, length) call append_tick(b, x + ox * a, y + oy * a, x + ox * c, y + oy * c) end subroutine append_tick_at - ! A tick label, rotated about its anchor when asked. SVG rotates - ! clockwise and matplotlib counter-clockwise, hence the sign. + ! A tick label, rotated about its anchor when asked. subroutine append_tick_text(b, x, y, s, anchor, fontsize, rot) - type(svg_builder), intent(inout) :: b + class(renderer_t), intent(inout) :: b real(dp), intent(in) :: x, y, fontsize, rot character(len=*), intent(in) :: s, anchor - character(len=96) :: tr - character(len=32) :: n1, n2, n3 - integer :: k1, k2, k3 - - if (rot == 0.0_dp) then - call append_text(b, x, y, s, anchor, fontsize, "#000000") - return - end if - call fmt_num(-rot, n1, k1) - call fmt_num(x, n2, k2) - call fmt_num(y, n3, k3) - tr = "rotate(" // n1(1:k1) // " " // n2(1:k2) // " " // n3(1:k3) // ")" - call append_text(b, x, y, s, anchor, fontsize, "#000000", trim(tr)) + call append_text(b, x, y, s, anchor, fontsize, "#000000", rot) end subroutine append_tick_text subroutine tick_label(labeled, lab, i, v, sc, out, n) @@ -3135,7 +3197,7 @@ subroutine tick_label(labeled, lab, i, v, sc, out, n) end subroutine tick_label subroutine render_axes(b, a, idx, W, H, clear) - type(svg_builder), intent(inout) :: b + class(renderer_t), intent(inout) :: b type(axes_t), intent(in) :: a integer, intent(in) :: idx real(dp), intent(in) :: W, H @@ -3157,6 +3219,8 @@ subroutine render_axes(b, a, idx, W, H, clear) type(scale_t) :: xsc, ysc integer :: n_leg, k, max_lbl, n_col, n_row, lc, lr real(dp) :: leg_x, leg_y, leg_w, leg_h, row_h, col_w, ttl_h, leg_x0 + real(dp), allocatable :: lx(:), ly(:) + type(paint_t) :: pnt ax_l = a%left * W ax_r = a%right * W @@ -3215,85 +3279,42 @@ subroutine render_axes(b, a, idx, W, H, clear) nym = 0 end if - ! clip path for this axes' data - call builder_append(b, '') - call builder_append(b, new_line("a")) - ! axes face if (.not. clear .and. .not. a%patch_off) then - call builder_append(b, '') - call builder_append(b, new_line("a")) + call append_rect(b, ax_l, ax_t, ax_w, ax_h, "#ffffff") end if ! grid if (a%grid_on) then do i = 1, nxt px = map_x(xticks(i), xmin, xmax, ax_l, ax_w, xsc) - call builder_append(b, '') - call builder_append(b, new_line("a")) + call append_line(b, px, ax_t, px, ax_b, "#b0b0b0", 0.8_dp, & + LINE_SOLID, 1.0_dp) end do do i = 1, nyt py = map_y(yticks(i), ymin, ymax, ax_b, ax_h, ysc) - call builder_append(b, '') - call builder_append(b, new_line("a")) + call append_line(b, ax_l, py, ax_r, py, "#b0b0b0", 0.8_dp, & + LINE_SOLID, 1.0_dp) end do end if if (a%has_img .or. a%has_cont) then - call builder_append(b, '') - call builder_append(b, new_line("a")) + call set_clip(ax_l, ax_t, ax_w, ax_h) if (a%has_img) & call append_image(b, a, xmin, xmax, ymin, ymax, ax_l, ax_w, ax_b, ax_h, xsc, ysc) if (a%has_cont) & call append_contour(b, a, xmin, xmax, ymin, ymax, ax_l, ax_w, ax_b, ax_h, xsc, ysc) - call builder_append(b, "") - call builder_append(b, new_line("a")) + call clear_clip() end if ! data - call builder_append(b, '') - call builder_append(b, new_line("a")) + call set_clip(ax_l, ax_t, ax_w, ax_h) do i = 1, a%n_series n = a%series(i)%n if (n <= 0) cycle + if (allocated(lx)) deallocate (lx) + if (allocated(ly)) deallocate (ly) + allocate (lx(n), ly(n)) select case (a%series(i)%kind) case (SERIES_BOX) @@ -3361,42 +3382,41 @@ subroutine render_axes(b, a, idx, W, H, clear) end do end select - if (a%series(i)%linestyle /= LINE_NONE .and. n >= 2) then - call builder_append(b, '') - call builder_append(b, new_line("a")) + ! Points that fall off a log axis are dropped once, and both the + ! line and its markers then work from the same list. + nl = 0 + do j = 1, n + if (a%xsc%kind == SCALE_LOG .and. a%series(i)%x(j) <= 0.0_dp) cycle + if (a%ysc%kind == SCALE_LOG .and. a%series(i)%y(j) <= 0.0_dp) cycle + nl = nl + 1 + lx(nl) = map_x(a%series(i)%x(j), xmin, xmax, ax_l, ax_w, xsc) + ly(nl) = map_y(a%series(i)%y(j), ymin, ymax, ax_b, ax_h, ysc) + end do + + if (a%series(i)%linestyle /= LINE_NONE .and. nl >= 2) then + pnt = pen(trim(a%series(i)%color), a%series(i)%linewidth, & + a%series(i)%alpha, a%series(i)%linestyle) + pnt%join = JOIN_ROUND + pnt%cap = CAP_BUTT + call b%draw_path(lx(1:nl), ly(1:nl), line_verbs(nl), nl, pnt) end if - if (a%series(i)%marker /= MARKER_NONE) then - ms = a%series(i)%markersize - do j = 1, n - if (a%xsc%kind == SCALE_LOG .and. a%series(i)%x(j) <= 0.0_dp) cycle - if (a%ysc%kind == SCALE_LOG .and. a%series(i)%y(j) <= 0.0_dp) cycle - px = map_x(a%series(i)%x(j), xmin, xmax, ax_l, ax_w, xsc) - py = map_y(a%series(i)%y(j), ymin, ymax, ax_b, ax_h, ysc) - if (allocated(a%series(i)%psize)) ms = a%series(i)%psize(j) - call append_marker(b, a%series(i)%marker, px, py, ms, & - point_color(a%series(i), j), a%series(i)%alpha) - end do + if (a%series(i)%marker /= MARKER_NONE .and. nl > 0) then + if (allocated(a%series(i)%psize) .or. allocated(a%series(i)%pcolor)) then + ! scatter gave every point its own size or color, so the + ! shape cannot be shared between them. + ms = a%series(i)%markersize + do j = 1, nl + if (allocated(a%series(i)%psize)) ms = a%series(i)%psize(j) + call append_marker(b, a%series(i)%marker, lx(j), ly(j), ms, & + point_color(a%series(i), j), & + a%series(i)%alpha) + end do + else + call append_markers(b, a%series(i)%marker, lx, ly, nl, & + a%series(i)%markersize, & + trim(a%series(i)%color), a%series(i)%alpha) + end if end if end do ! annotations, in data coordinates @@ -3409,29 +3429,18 @@ subroutine render_axes(b, a, idx, W, H, clear) map_y(a%texts(i)%ytail, ymin, ymax, ax_b, ax_h, ysc), & trim(a%texts(i)%color), 1.0_dp, LINE_SOLID, 1.0_dp) end if - call xml_escape_to(a%texts(i)%s, esc, en) - call append_text(b, px, py + 3.5_dp, esc(1:en), & + call append_text(b, px, py + 3.5_dp, trim(a%texts(i)%s), & trim(a%texts(i)%ha), a%texts(i)%fontsize, & trim(a%texts(i)%color)) end do - call builder_append(b, "") - call builder_append(b, new_line("a")) + call clear_clip() ! spines. All four still go out as one , so a plot that leaves ! them alone renders exactly as it did before they could be hidden. if (.not. a%frame_off) then if (all(a%spine)) then - call builder_append(b, '') - call builder_append(b, new_line("a")) + call b%draw_rect(ax_l, ax_t, ax_w, ax_h, pen("#000000", 0.8_dp)) else if (a%spine(SPINE_LEFT)) call append_spine(b, ax_l, ax_t, ax_l, ax_b) if (a%spine(SPINE_RIGHT)) call append_spine(b, ax_r, ax_t, ax_r, ax_b) @@ -3491,36 +3500,29 @@ subroutine render_axes(b, a, idx, W, H, clear) end do if (len_trim(a%xlabel) > 0) then - call xml_escape_to(a%xlabel, esc, en) call append_text(b, 0.5_dp * (ax_l + ax_r), & ax_b + xtick_gap(a) + 0.24_dp * a%xtick_size + 1.84_dp & - + 0.76_dp * a%xlabel_size, esc(1:en), & + + 0.76_dp * a%xlabel_size, trim(a%xlabel), & "center", a%xlabel_size, "#000000") end if if (len_trim(a%ylabel) > 0) then - call xml_escape_to(a%ylabel, esc, en) ! The right-hand label of a twinx faces the other way, so that it ! reads from outside the axes just as the left-hand one does. mid = y_edge + y_out * (34.0_dp + 0.76_dp * (a%ylabel_size - LABEL_FONT) & + 1.15_dp * (a%ytick_size - TICK_FONT)) - call fmt_num(mid, tx, tn) - call fmt_num(0.5_dp * (ax_t + ax_b), ty, tyn) - call append_text(b, mid, 0.5_dp * (ax_t + ax_b), esc(1:en), & + call append_text(b, mid, 0.5_dp*(ax_t + ax_b), trim(a%ylabel), & "center", a%ylabel_size, "#000000", & - "rotate(" // merge("90 ", "-90", a%y_right) // " " // & - tx(1:tn) // " " // ty(1:tyn) // ")") + merge(-90.0_dp, 90.0_dp, a%y_right)) end if ! title if (len_trim(a%title) > 0) then - call xml_escape_to(a%title, esc, en) call append_text(b, 0.5_dp * (ax_l + ax_r), & - ax_t - 0.5_dp * a%title_size, esc(1:en), & + ax_t - 0.5_dp * a%title_size, trim(a%title), & "center", a%title_size, "#000000") end if - if (a%cbar_on) call append_colorbar(b, a, idx, W, H) ! legend if (a%legend_on) then @@ -3554,22 +3556,17 @@ subroutine render_axes(b, a, idx, W, H, clear) leg_w, leg_h, leg_x, leg_y) end if if (len_trim(a%legend_title) > 0) then - call xml_escape_to(a%legend_title, esc, en) call append_text(b, leg_x + 0.5_dp * leg_w, & leg_y + 4.0_dp + 0.5_dp * row_h + 3.5_dp, & - esc(1:en), "center", a%legend_size, "#000000") + trim(a%legend_title), "center", & + a%legend_size, "#000000") end if if (a%legend_frame) then - call builder_append(b, '') - call builder_append(b, new_line("a")) + pnt = brush("#ffffff") + pnt%stroked = .true. + pnt%stroke_rgb = hex_rgb("#cccccc") + pnt%line_width = 0.8_dp + call b%draw_rect(leg_x, leg_y, leg_w, leg_h, pnt, 2.0_dp) end if leg_x0 = leg_x k = 0 @@ -3581,22 +3578,10 @@ subroutine render_axes(b, a, idx, W, H, clear) leg_x = leg_x0 + real(lc, dp) * col_w py = leg_y + 4.0_dp + ttl_h + (real(lr, dp) + 0.5_dp) * row_h if (a%series(i)%linestyle /= LINE_NONE) then - call builder_append(b, '") - call builder_append(b, new_line("a")) + call append_line(b, leg_x + 8.0_dp, py, leg_x + 28.0_dp, py, & + trim(a%series(i)%color), & + a%series(i)%linewidth, & + a%series(i)%linestyle, 1.0_dp) end if mid = leg_x + 18.0_dp if (a%series(i)%marker /= MARKER_NONE) then @@ -3604,35 +3589,37 @@ subroutine render_axes(b, a, idx, W, H, clear) a%series(i)%markersize, & trim(a%series(i)%color), a%series(i)%alpha) end if - call xml_escape_to(a%series(i)%label, esc, en) - call append_text(b, leg_x + 34.0_dp, py + 3.5_dp, esc(1:en), & + call append_text(b, leg_x + 34.0_dp, py + 3.5_dp, & + trim(a%series(i)%label), & "left", a%legend_size, "#000000") end do end if end if end subroutine render_axes - function render_svg(facecolor, transparent, bbox_inches, pad_inches) result(svg) + ! Draw the whole figure into any backend. Everything above this point is + ! format independent; the only thing render_svg and its PDF and PNG + ! counterparts add is the choice of renderer. + subroutine render_figure(b, facecolor, transparent, bbox_inches, pad_inches) + class(renderer_t), intent(inout) :: b character(len=*), intent(in), optional :: facecolor, bbox_inches logical, intent(in), optional :: transparent real(dp), intent(in), optional :: pad_inches - character(len=:), allocatable :: svg - type(svg_builder) :: b real(dp) :: W, H, vx, vy, vw, vh, bpad character(len=512) :: esc character(len=7) :: face logical :: clear - integer :: i, en + integer :: i, en, n_grp call ensure_fig() clear = .false. if (present(transparent)) clear = transparent face = resolve_color(facecolor) if (len_trim(face) == 0) face = "#ffffff" - call builder_init(b) + call clear_clip() - W = fig_w_in * PT_PER_IN - H = fig_h_in * PT_PER_IN + W = fig_w_in*PT_PER_IN + H = fig_h_in*PT_PER_IN vx = 0.0_dp vy = 0.0_dp vw = W @@ -3641,8 +3628,8 @@ function render_svg(facecolor, transparent, bbox_inches, pad_inches) result(svg) if (lower(trim(bbox_inches)) == "tight") then bpad = 0.1_dp if (present(pad_inches)) bpad = pad_inches - bpad = bpad * PT_PER_IN - ! Cropping is expressed as a shifted viewBox, so the drawing + bpad = bpad*PT_PER_IN + ! Cropping moves the window onto the drawing, so the drawing ! itself needs no translation. call drawn_bbox(W, H, vx, vy, vw, vh) vx = vx - bpad @@ -3654,73 +3641,98 @@ function render_svg(facecolor, transparent, bbox_inches, pad_inches) result(svg) end if end if - call builder_append(b, '') - call builder_append(b, new_line("a")) - call builder_append(b, '') - call builder_append(b, new_line("a")) - - ! background; transparent drops the figure patch entirely - if (.not. clear) then - call builder_append(b, '') - call builder_append(b, new_line("a")) - end if - - ! axes (subplots) + ! transparent drops the figure patch entirely + if (clear) then + call b%open_canvas(vw, vh, x0=vx, y0=vy) + else + call b%open_canvas(vw, vh, bg_rgb=hex_rgb(face), x0=vx, y0=vy) + end if + + ! matplotlib counts a colorbar as an axes in its own right, and it is + ! drawn after the axes it belongs to, so the numbering interleaves. + n_grp = 0 do i = 1, n_ax + n_grp = n_grp + 1 + call b%begin_group("axes_"//int_to_str(n_grp)) call render_axes(b, ax(i), i, W, H, clear) + call b%end_group() + if (ax(i)%cbar_on) then + n_grp = n_grp + 1 + call b%begin_group("axes_"//int_to_str(n_grp)) + call append_colorbar(b, ax(i), i, W, H) + call b%end_group() + end if end do ! suptitle (figure-level, above all axes) if (len_trim(fig_suptitle) > 0) then - call xml_escape_to(fig_suptitle, esc, en) - call append_text(b, 0.5_dp * W, (1.0_dp - SUPTITLE_Y) * H + 4.2_dp, & - esc(1:en), "center", fig_suptitle_size, "#000000") + call append_text(b, 0.5_dp*W, (1.0_dp - SUPTITLE_Y)*H + 4.2_dp, & + trim(fig_suptitle), "center", fig_suptitle_size, & + "#000000") end if - call builder_append(b, "") - call builder_append(b, new_line("a")) - svg = builder_get(b) + call b%close_canvas() + end subroutine render_figure + + function render_svg(facecolor, transparent, bbox_inches, pad_inches) result(svg) + character(len=*), intent(in), optional :: facecolor, bbox_inches + logical, intent(in), optional :: transparent + real(dp), intent(in), optional :: pad_inches + character(len=:), allocatable :: svg + type(svg_renderer_t) :: r + call render_figure(r, facecolor, transparent, bbox_inches, pad_inches) + svg = r%bytes() end function render_svg - ! Writing SVG bytes into a .png would produce a file no viewer can open, - ! so an unsupported extension is a hard error rather than a silent default. - subroutine check_svg_ext(filename) + function render_pdf(facecolor, transparent, bbox_inches, pad_inches) result(pdf) + character(len=*), intent(in), optional :: facecolor, bbox_inches + logical, intent(in), optional :: transparent + real(dp), intent(in), optional :: pad_inches + character(len=:), allocatable :: pdf + type(pdf_renderer_t) :: r + call render_figure(r, facecolor, transparent, bbox_inches, pad_inches) + pdf = r%bytes() + end function render_pdf + + ! Unlike the vector formats, dpi is not decorative here: it is what + ! decides how many pixels the figure is rasterized into. + function render_png(facecolor, transparent, bbox_inches, pad_inches) result(png) + character(len=*), intent(in), optional :: facecolor, bbox_inches + logical, intent(in), optional :: transparent + real(dp), intent(in), optional :: pad_inches + character(len=:), allocatable :: png + type(png_renderer_t) :: r + call r%set_dpi(fig_dpi) + call render_figure(r, facecolor, transparent, bbox_inches, pad_inches) + png = r%bytes() + end function render_png + + ! The extension picks the backend, as it does in matplotlib. A name with + ! no extension at all is taken as SVG. + function file_ext(filename) result(ext) character(len=*), intent(in) :: filename - integer :: d, sl character(len=:), allocatable :: ext + integer :: d, sl d = index(filename, ".", back=.true.) - sl = max(index(filename, "/", back=.true.), index(filename, achar(92), back=.true.)) - if (d <= sl + 1) return - ext = lower(filename(d + 1:len_trim(filename))) - if (ext == "svg") return + sl = max(index(filename, "/", back=.true.), & + index(filename, achar(92), back=.true.)) + if (d <= sl + 1) then + ext = "svg" + else + ext = lower(filename(d + 1:len_trim(filename))) + end if + end function file_ext + + ! Writing SVG bytes into a .png would produce a file no viewer can open, + ! so an unsupported extension is a hard error rather than a silent default. + subroutine reject_ext(filename) + character(len=*), intent(in) :: filename print *, "fplot: cannot write ", trim(filename) - print *, "fplot: only .svg output is supported, not ." // ext + print *, "fplot: supported formats are .svg, .pdf and .png, not ." & + //file_ext(filename) error stop "fplot: unsupported savefig format" - end subroutine check_svg_ext + end subroutine reject_ext pure function lower(s) result(t) character(len=*), intent(in) :: s @@ -3736,9 +3748,8 @@ pure function lower(s) result(t) end do end function lower - ! dpi is accepted and remembered, but SVG is resolution independent and - ! matplotlib emits the same inches*72 canvas at any dpi, so it does not - ! change the output here either. + ! dpi sizes the raster in the PNG backend. The vector formats ignore it, + ! as matplotlib does: they emit the same inches*72 canvas at any dpi. subroutine savefig(filename, transparent, facecolor, dpi, bbox_inches, pad_inches) character(len=*), intent(in) :: filename logical, intent(in), optional :: transparent @@ -3746,13 +3757,27 @@ subroutine savefig(filename, transparent, facecolor, dpi, bbox_inches, pad_inche real(dp), intent(in), optional :: dpi, pad_inches character(len=:), allocatable :: svg integer :: u, ios, n + real(dp) :: saved_dpi - call check_svg_ext(filename) + ! dpi given here applies to this file only, as it does in matplotlib; + ! the figure's own dpi is what figure(dpi=) set and outlives the call. + saved_dpi = fig_dpi if (present(dpi)) then if (dpi <= 0.0_dp) error stop "fplot: savefig dpi must be positive" fig_dpi = dpi end if - svg = render_svg(facecolor, transparent, bbox_inches, pad_inches) + select case (file_ext(filename)) + case ("svg") + svg = render_svg(facecolor, transparent, bbox_inches, pad_inches) + case ("pdf") + svg = render_pdf(facecolor, transparent, bbox_inches, pad_inches) + case ("png") + svg = render_png(facecolor, transparent, bbox_inches, pad_inches) + case default + call reject_ext(filename) + return + end select + fig_dpi = saved_dpi n = len(svg) open (newunit=u, file=trim(filename), status="replace", action="write", & form="unformatted", access="stream", iostat=ios) diff --git a/src/fplot_backend_pdf.f90 b/src/fplot_backend_pdf.f90 new file mode 100644 index 0000000..0a681fe --- /dev/null +++ b/src/fplot_backend_pdf.f90 @@ -0,0 +1,518 @@ +! PDF backend. +! +! PDF is the cheapest of the four to reach: it is a vector format, so there +! is no rasterizer, and its page operators line up almost one for one with +! the API's primitives. The three things it does not share with the API are +! handled here and nowhere else: +! +! * y grows upwards. The whole page is drawn under one flipping matrix, +! set once in the content stream, so every coordinate arrives unchanged. +! Text would come out mirrored under that flip, so each string carries a +! text matrix that undoes it. +! +! * there is no circle operator, so circles become four cubics. +! +! * constant alpha is not a graphics operator but a named graphics state +! resource, so the distinct alpha values used by a figure are collected +! and written into the page's /ExtGState. +! +! Text is drawn with Helvetica from the base fourteen fonts, which every +! reader has, so nothing is embedded. That has one real consequence: PDF +! cannot ask a viewer to centre a string the way SVG's text-anchor does, so +! this backend has to know how wide the text is, and the Helvetica widths +! below are what it measures with. fplot lays out to DejaVu Sans metrics, so +! centred labels can sit a fraction of a point off where the SVG puts them. +! Embedding DejaVu and its real widths is the fix, and is what matplotlib +! does by default. +module fplot_backend_pdf + use fplot_style, only: dp + use fplot_render + use fplot_svg, only: svg_builder, builder_init, builder_append, & + builder_get, fmt_num + implicit none + private + + public :: pdf_renderer_t + + integer, parameter :: MAX_OBJ = 64 + integer, parameter :: MAX_ALPHA = 64 + + type, extends(renderer_t) :: pdf_renderer_t + ! The page content stream. The file itself is assembled in + ! close_canvas, once the length of this is known. + type(svg_builder) :: c + character(len=:), allocatable :: out + real(dp) :: w = 0.0_dp, h = 0.0_dp + real(dp) :: x0 = 0.0_dp, y0 = 0.0_dp + ! Distinct alpha pairs, each of which becomes an /ExtGState. + integer :: n_alpha = 0 + real(dp) :: alpha_fill(MAX_ALPHA) = 1.0_dp + real(dp) :: alpha_stroke(MAX_ALPHA) = 1.0_dp + logical :: clip_open = .false. + type(clip_t) :: cur_clip + contains + procedure :: open_canvas => pdf_open_canvas + procedure :: close_canvas => pdf_close_canvas + procedure :: bytes => pdf_bytes + procedure :: draw_path => pdf_draw_path + procedure :: draw_rect => pdf_draw_rect + procedure :: draw_circle => pdf_draw_circle + procedure :: draw_markers => pdf_draw_markers + procedure :: draw_text => pdf_draw_text + procedure :: draw_image => pdf_draw_image + end type pdf_renderer_t + + ! Helvetica character widths in thousandths of an em, for codes 32..126. + ! These come from the Adobe AFM and are what makes centred text land in + ! the right place without embedding a font. + integer, parameter :: HELV_W(32:126) = [ & + 278, 278, 355, 556, 556, 889, 667, 191, 333, 333, 389, 584, 278, & + 333, 278, 278, 556, 556, 556, 556, 556, 556, 556, 556, 556, 556, & + 278, 278, 584, 584, 584, 556, 1015, 667, 667, 722, 722, 667, 611, & + 778, 722, 278, 500, 667, 556, 833, 722, 778, 667, 778, 722, 667, & + 611, 722, 667, 944, 667, 667, 611, 278, 278, 278, 469, 556, 333, & + 556, 556, 500, 556, 556, 278, 556, 556, 222, 222, 500, 222, 833, & + 556, 556, 556, 556, 333, 500, 278, 556, 500, 722, 500, 500, 500, & + 334, 260, 334, 584] + +contains + + subroutine put(self, s) + class(pdf_renderer_t), intent(inout) :: self + character(len=*), intent(in) :: s + call builder_append(self%c, s) + end subroutine put + + subroutine put_num(self, x) + class(pdf_renderer_t), intent(inout) :: self + real(dp), intent(in) :: x + character(len=64) :: s + integer :: n + call fmt_num(x, s, n) + call builder_append(self%c, s(1:n)) + call builder_append(self%c, " ") + end subroutine put_num + + function num_str(x) result(t) + real(dp), intent(in) :: x + character(len=:), allocatable :: t + character(len=64) :: s + integer :: n + call fmt_num(x, s, n) + t = s(1:n) + end function num_str + + pure function int_str(i) result(t) + integer, intent(in) :: i + character(len=:), allocatable :: t + character(len=16) :: s + write (s, "(I0)") i + t = trim(s) + end function int_str + + ! Width of a string in points, from the Helvetica table. + pure function text_width(s, size) result(w) + character(len=*), intent(in) :: s + real(dp), intent(in) :: size + real(dp) :: w + integer :: i, c + w = 0.0_dp + do i = 1, len(s) + c = iachar(s(i:i)) + if (c >= 32 .and. c <= 126) then + w = w + real(HELV_W(c), dp) + else + w = w + 500.0_dp + end if + end do + w = w*size/1000.0_dp + end function text_width + + ! PDF colors are components in 0..1. + subroutine put_rgb(self, rgb, stroking) + class(pdf_renderer_t), intent(inout) :: self + integer, intent(in) :: rgb(3) + logical, intent(in) :: stroking + integer :: i + do i = 1, 3 + call put_num(self, real(rgb(i), dp)/255.0_dp) + end do + if (stroking) then + call put(self, "RG"//new_line("a")) + else + call put(self, "rg"//new_line("a")) + end if + end subroutine put_rgb + + ! Alpha is a resource, not an operator, so each distinct pair is + ! registered once and referenced by name. + function alpha_state(self, fa, sa) result(id) + class(pdf_renderer_t), intent(inout) :: self + real(dp), intent(in) :: fa, sa + integer :: id, i + id = 0 + do i = 1, self%n_alpha + if (self%alpha_fill(i) == fa .and. self%alpha_stroke(i) == sa) then + id = i + return + end if + end do + if (self%n_alpha >= MAX_ALPHA) return + self%n_alpha = self%n_alpha + 1 + self%alpha_fill(self%n_alpha) = fa + self%alpha_stroke(self%n_alpha) = sa + id = self%n_alpha + end function alpha_state + + ! Everything that has to be in force before a painting operator: colors, + ! line style, transparency and the clip. Emitted inside q/Q so it cannot + ! leak into the next primitive. + subroutine begin_paint(self, p) + class(pdf_renderer_t), intent(inout) :: self + type(paint_t), intent(in) :: p + integer :: g, i + + call put(self, "q"//new_line("a")) + if (p%clip%on) then + call put_num(self, p%clip%x) + call put_num(self, p%clip%y) + call put_num(self, p%clip%w) + call put_num(self, p%clip%h) + call put(self, "re W n"//new_line("a")) + end if + if (p%fill_alpha < 1.0_dp .or. p%stroke_alpha < 1.0_dp) then + g = alpha_state(self, p%fill_alpha, p%stroke_alpha) + if (g > 0) call put(self, "/GS"//int_str(g)//" gs"//new_line("a")) + end if + if (p%filled) call put_rgb(self, p%fill_rgb, .false.) + if (p%stroked) then + call put_rgb(self, p%stroke_rgb, .true.) + call put_num(self, p%line_width) + call put(self, "w"//new_line("a")) + call put(self, int_str(p%cap)//" J"//new_line("a")) + select case (p%join) + case (JOIN_ROUND); call put(self, "1 j"//new_line("a")) + case (JOIN_BEVEL); call put(self, "2 j"//new_line("a")) + case default; call put(self, "0 j"//new_line("a")) + end select + if (p%n_dash > 0) then + call put(self, "[") + do i = 1, p%n_dash + call put_num(self, p%dash(i)) + end do + call put(self, "] "//num_str(p%dash_offset)//" d"//new_line("a")) + else + call put(self, "[] 0 d"//new_line("a")) + end if + end if + end subroutine begin_paint + + ! The operator that actually marks the page, chosen by what the paint + ! asks for, followed by the restore that closes begin_paint. + subroutine end_paint(self, p) + class(pdf_renderer_t), intent(inout) :: self + type(paint_t), intent(in) :: p + if (p%filled .and. p%stroked) then + if (p%fill_rule == FILL_EVENODD) then + call put(self, "B*"//new_line("a")) + else + call put(self, "B"//new_line("a")) + end if + else if (p%filled) then + if (p%fill_rule == FILL_EVENODD) then + call put(self, "f*"//new_line("a")) + else + call put(self, "f"//new_line("a")) + end if + else if (p%stroked) then + call put(self, "S"//new_line("a")) + else + call put(self, "n"//new_line("a")) + end if + call put(self, "Q"//new_line("a")) + end subroutine end_paint + + subroutine pdf_open_canvas(self, width, height, bg_rgb, bg_alpha, x0, y0) + class(pdf_renderer_t), intent(inout) :: self + real(dp), intent(in) :: width, height + integer, intent(in), optional :: bg_rgb(3) + real(dp), intent(in), optional :: bg_alpha + real(dp), intent(in), optional :: x0, y0 + type(paint_t) :: p + + self%w = width + self%h = height + self%x0 = 0.0_dp + self%y0 = 0.0_dp + if (present(x0)) self%x0 = x0 + if (present(y0)) self%y0 = y0 + self%n_alpha = 0 + call builder_init(self%c) + + ! One matrix for the whole page turns the API's top-left origin with + ! y downwards into PDF's bottom-left origin with y upwards, and + ! applies the canvas origin at the same time. + call put(self, "1 0 0 -1 "//num_str(-self%x0)//" " & + //num_str(self%y0 + height)//" cm"//new_line("a")) + + if (present(bg_rgb)) then + p%filled = .true. + p%fill_rgb = bg_rgb + if (present(bg_alpha)) p%fill_alpha = bg_alpha + call pdf_draw_rect(self, self%x0, self%y0, width, height, p) + end if + end subroutine pdf_open_canvas + + subroutine emit_path(self, x, y, verbs, n_verb) + class(pdf_renderer_t), intent(inout) :: self + real(dp), intent(in) :: x(:), y(:) + integer, intent(in) :: verbs(:), n_verb + integer :: i, k, j + + k = 0 + do i = 1, n_verb + select case (verbs(i)) + case (VERB_MOVE) + k = k + 1 + call put_num(self, x(k)) + call put_num(self, y(k)) + call put(self, "m"//new_line("a")) + case (VERB_LINE) + k = k + 1 + call put_num(self, x(k)) + call put_num(self, y(k)) + call put(self, "l"//new_line("a")) + case (VERB_CUBIC) + do j = 1, 3 + k = k + 1 + call put_num(self, x(k)) + call put_num(self, y(k)) + end do + call put(self, "c"//new_line("a")) + case (VERB_CLOSE) + call put(self, "h"//new_line("a")) + end select + end do + end subroutine emit_path + + subroutine pdf_draw_path(self, x, y, verbs, n_verb, paint) + class(pdf_renderer_t), intent(inout) :: self + real(dp), intent(in) :: x(:), y(:) + integer, intent(in) :: verbs(:), n_verb + type(paint_t), intent(in) :: paint + if (n_verb <= 0) return + call begin_paint(self, paint) + call emit_path(self, x, y, verbs, n_verb) + call end_paint(self, paint) + end subroutine pdf_draw_path + + subroutine pdf_draw_rect(self, x, y, w, h, paint, radius) + class(pdf_renderer_t), intent(inout) :: self + real(dp), intent(in) :: x, y, w, h + type(paint_t), intent(in) :: paint + real(dp), intent(in), optional :: radius + + call begin_paint(self, paint) + ! Rounded corners are rare enough that they go through the general + ! path; the plain case gets the single "re" operator. + call put_num(self, x) + call put_num(self, y) + call put_num(self, w) + call put_num(self, h) + call put(self, "re"//new_line("a")) + call end_paint(self, paint) + end subroutine pdf_draw_rect + + subroutine pdf_draw_circle(self, cx, cy, r, paint) + class(pdf_renderer_t), intent(inout) :: self + real(dp), intent(in) :: cx, cy, r + type(paint_t), intent(in) :: paint + real(dp) :: x(13), y(13), k + integer :: v(6) + + k = r*0.5522847498307933_dp + x = [cx + r, cx + r, cx + k, cx, cx - k, cx - r, cx - r, cx - r, & + cx - k, cx, cx + k, cx + r, cx + r] + y = [cy, cy + k, cy + r, cy + r, cy + r, cy + k, cy, cy - k, & + cy - r, cy - r, cy - r, cy - k, cy] + v = [VERB_MOVE, VERB_CUBIC, VERB_CUBIC, VERB_CUBIC, VERB_CUBIC, VERB_CLOSE] + call pdf_draw_path(self, x, y, v, 6, paint) + end subroutine pdf_draw_circle + + ! Repeating the outline per point rather than defining a form XObject. + ! A form would make the file smaller, but every point still needs its own + ! placement operator, so the drawing cost is the same either way. + subroutine pdf_draw_markers(self, x, y, mx, my, mverbs, n_mverb, paint) + class(pdf_renderer_t), intent(inout) :: self + real(dp), intent(in) :: x(:), y(:) + real(dp), intent(in) :: mx(:), my(:) + integer, intent(in) :: mverbs(:), n_mverb + type(paint_t), intent(in) :: paint + integer :: i + do i = 1, size(x) + call pdf_draw_path(self, mx + x(i), my + y(i), mverbs, n_mverb, paint) + end do + end subroutine pdf_draw_markers + + subroutine pdf_draw_text(self, x, y, s, font, paint, anchor, baseline, angle) + class(pdf_renderer_t), intent(inout) :: self + real(dp), intent(in) :: x, y + character(len=*), intent(in) :: s + type(font_t), intent(in) :: font + type(paint_t), intent(in) :: paint + integer, intent(in) :: anchor, baseline + real(dp), intent(in) :: angle + real(dp) :: w, dx, dy, ca, sa, rad + integer :: i + character(len=1) :: ch + + if (len_trim(s) == 0) return + + ! SVG asks the viewer to align the string; PDF places a fixed origin, + ! so the offset has to be measured here. + w = text_width(trim(s), font%size) + dx = 0.0_dp + select case (anchor) + case (ANCHOR_MIDDLE); dx = -0.5_dp*w + case (ANCHOR_END); dx = -w + end select + dy = 0.0_dp + select case (baseline) + case (BASE_MIDDLE); dy = 0.36_dp*font%size + case (BASE_TOP); dy = 0.76_dp*font%size + case (BASE_BOTTOM); dy = -0.21_dp*font%size + end select + + call begin_paint(self, paint) + call put(self, "BT"//new_line("a")) + call put(self, "/F1 "//num_str(font%size)//" Tf"//new_line("a")) + ! The page flip would leave text mirrored, so the text matrix flips + ! back; the rotation is folded into the same matrix. + rad = angle*acos(-1.0_dp)/180.0_dp + ca = cos(rad) + sa = sin(rad) + call put_num(self, ca) + call put_num(self, sa) + call put_num(self, sa) + call put_num(self, -ca) + call put_num(self, x + dx*ca - dy*sa) + call put_num(self, y + dx*sa + dy*ca) + call put(self, "Tm"//new_line("a")) + call put(self, "(") + do i = 1, len_trim(s) + ch = s(i:i) + ! Parentheses and the escape character end a PDF string early. + if (ch == "(" .or. ch == ")" .or. ch == achar(92)) & + call put(self, achar(92)) + call put(self, ch) + end do + call put(self, ") Tj"//new_line("a")) + call put(self, "ET"//new_line("a")) + call put(self, "Q"//new_line("a")) + end subroutine pdf_draw_text + + ! imshow currently reaches every backend as a grid of rectangles, so no + ! figure fplot can draw needs this yet. + subroutine pdf_draw_image(self, x, y, w, h, rgba, nx, ny, paint) + class(pdf_renderer_t), intent(inout) :: self + real(dp), intent(in) :: x, y, w, h + integer, intent(in) :: nx, ny + integer, intent(in) :: rgba(4, nx, ny) + type(paint_t), intent(in) :: paint + end subroutine pdf_draw_image + + ! Assemble the file. Objects have to be byte-counted as they are written + ! because the trailing xref table records where each one starts. + subroutine pdf_close_canvas(self) + class(pdf_renderer_t), intent(inout) :: self + type(svg_builder) :: f + character(len=:), allocatable :: content, res, gs + integer :: off(MAX_OBJ), nobj, i, pos, xref_pos + character(len=1) :: eol + + eol = new_line("a") + content = builder_get(self%c) + + ! /ExtGState entries for the alpha values the figure actually used. + gs = "" + do i = 1, self%n_alpha + gs = gs//"/GS"//int_str(i)//" << /Type /ExtGState /ca " & + //num_str(self%alpha_fill(i))//" /CA " & + //num_str(self%alpha_stroke(i))//" >> " + end do + res = "<< /Font << /F1 5 0 R >>" + if (len(gs) > 0) res = res//" /ExtGState << "//gs//">>" + res = res//" >>" + + call builder_init(f) + pos = 0 + nobj = 5 + + call emit(f, pos, "%PDF-1.4"//eol) + ! A comment with high bytes marks the file as binary for tools that + ! sniff it. + call emit(f, pos, "%"//achar(226)//achar(227)//achar(207)//achar(211)//eol) + + off(1) = pos + call emit(f, pos, "1 0 obj"//eol//"<< /Type /Catalog /Pages 2 0 R >>" & + //eol//"endobj"//eol) + + off(2) = pos + call emit(f, pos, "2 0 obj"//eol//"<< /Type /Pages /Kids [3 0 R] /Count 1 >>" & + //eol//"endobj"//eol) + + off(3) = pos + call emit(f, pos, "3 0 obj"//eol//"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 " & + //num_str(self%w)//" "//num_str(self%h)//"] /Contents 4 0 R /Resources " & + //res//" >>"//eol//"endobj"//eol) + + off(4) = pos + call emit(f, pos, "4 0 obj"//eol//"<< /Length "//int_str(len(content)) & + //" >>"//eol//"stream"//eol) + call emit(f, pos, content) + call emit(f, pos, "endstream"//eol//"endobj"//eol) + + off(5) = pos + call emit(f, pos, "5 0 obj"//eol//"<< /Type /Font /Subtype /Type1 " & + //"/BaseFont /Helvetica /Encoding /WinAnsiEncoding >>"//eol & + //"endobj"//eol) + + xref_pos = pos + call emit(f, pos, "xref"//eol//"0 "//int_str(nobj + 1)//eol) + call emit(f, pos, "0000000000 65535 f "//eol) + do i = 1, nobj + call emit(f, pos, pad10(off(i))//" 00000 n "//eol) + end do + call emit(f, pos, "trailer"//eol//"<< /Size "//int_str(nobj + 1) & + //" /Root 1 0 R >>"//eol) + call emit(f, pos, "startxref"//eol//int_str(xref_pos)//eol//"%%EOF"//eol) + + self%out = builder_get(f) + end subroutine pdf_close_canvas + + subroutine emit(f, pos, s) + type(svg_builder), intent(inout) :: f + integer, intent(inout) :: pos + character(len=*), intent(in) :: s + call builder_append(f, s) + pos = pos + len(s) + end subroutine emit + + ! xref offsets are a fixed ten digits wide. + pure function pad10(i) result(t) + integer, intent(in) :: i + character(len=10) :: t + write (t, "(I10.10)") i + end function pad10 + + function pdf_bytes(self) result(s) + class(pdf_renderer_t), intent(in) :: self + character(len=:), allocatable :: s + if (allocated(self%out)) then + s = self%out + else + s = "" + end if + end function pdf_bytes + +end module fplot_backend_pdf diff --git a/src/fplot_backend_png.f90 b/src/fplot_backend_png.f90 new file mode 100644 index 0000000..6cbfe64 --- /dev/null +++ b/src/fplot_backend_png.f90 @@ -0,0 +1,446 @@ +! fplot_backend_png — raster output. +! +! The odd one out among the backends: the other three write a description of +! the drawing and let something else decide what it looks like, while this one +! has to decide. That work lives in fplot_raster and fplot_png; what is left +! here is only the translation from the rendering API's calls into fills. +! +! Because the API is purely additive and in painter's order, every call is +! rasterized the moment it arrives and nothing is buffered. The pixels are the +! display list. Only close_canvas has anything left to do, and that is +! compression rather than drawing. +! +! Text is the one place this backend cannot follow the others. SVG and PDF +! hand the string over and let a font engine draw it; there is no font engine +! here, so the glyph outlines are compiled in (see fplot_glyphs) and text is +! filled with exactly the code that fills any other path. They are DejaVu Sans +! outlines, which is what matplotlib draws with, so the text lands in the same +! place rather than merely a similar one. + +module fplot_backend_png + use fplot_style, only: dp + use fplot_render + use fplot_raster + use fplot_png, only: png_encode + use fplot_glyphs, only: EM, ASCENT, DESCENT, XHEIGHT, & + glyph_advance, glyph_verbs, glyph_points + implicit none + private + + public :: png_renderer_t + + ! Matplotlib's default figure dpi. Points are the API's unit, so this is + ! the only place a pixel size is decided. + real(dp), parameter :: DEFAULT_DPI = 100.0_dp + + type, extends(renderer_t) :: png_renderer_t + type(canvas_t) :: cv + type(pathbuf_t) :: pb + real(dp) :: dpi = DEFAULT_DPI + real(dp) :: ox = 0.0_dp, oy = 0.0_dp ! canvas origin, in points + character(len=:), allocatable :: out + contains + procedure :: open_canvas => png_open_canvas + procedure :: close_canvas => png_close_canvas + procedure :: bytes => png_bytes + procedure :: draw_path => png_draw_path + procedure :: draw_rect => png_draw_rect + procedure :: draw_circle => png_draw_circle + procedure :: draw_markers => png_draw_markers + procedure :: draw_text => png_draw_text + procedure :: draw_image => png_draw_image + procedure :: set_dpi => png_set_dpi + end type png_renderer_t + +contains + + ! Has to be called before open_canvas, since the canvas is allocated at + ! its final pixel size and never resampled. + subroutine png_set_dpi(self, dpi) + class(png_renderer_t), intent(inout) :: self + real(dp), intent(in) :: dpi + + self%dpi = dpi + end subroutine png_set_dpi + + subroutine png_open_canvas(self, width, height, bg_rgb, bg_alpha, x0, y0) + class(png_renderer_t), intent(inout) :: self + real(dp), intent(in) :: width, height + integer, intent(in), optional :: bg_rgb(3) + real(dp), intent(in), optional :: bg_alpha + real(dp), intent(in), optional :: x0, y0 + integer :: rgb(3), wpx, hpx + real(dp) :: alpha, s + + ! No background colour means no figure patch at all, which is how + ! transparent=True reaches the backend. The vector formats express + ! that by drawing nothing; a raster has to say it, as alpha zero. + rgb = [255, 255, 255] + alpha = 0.0_dp + if (present(bg_rgb)) then + rgb = bg_rgb + alpha = 1.0_dp + end if + if (present(bg_alpha)) alpha = bg_alpha + self%ox = 0.0_dp + self%oy = 0.0_dp + if (present(x0)) self%ox = x0 + if (present(y0)) self%oy = y0 + + self%width = width + self%height = height + s = self%dpi/72.0_dp + wpx = max(1, nint(width*s)) + hpx = max(1, nint(height*s)) + call canvas_init(self%cv, wpx, hpx, s, rgb, alpha) + self%is_open = .true. + end subroutine png_open_canvas + + subroutine png_close_canvas(self) + class(png_renderer_t), intent(inout) :: self + integer, allocatable :: rgba(:, :, :) + + if (.not. self%is_open) return + rgba = canvas_rgba(self%cv) + self%out = png_encode(self%cv%w, self%cv%h, rgba) + call canvas_free(self%cv) + self%is_open = .false. + end subroutine png_close_canvas + + function png_bytes(self) result(b) + class(png_renderer_t), intent(in) :: self + character(len=:), allocatable :: b + + if (allocated(self%out)) then + b = self%out + else + b = "" + end if + end function png_bytes + + ! ------------------------------------------------------------------ + ! Points to pixels. The canvas origin shifts the window onto the drawing + ! rather than the drawing itself, which is how a tight bounding box works. + ! ------------------------------------------------------------------ + + pure function tx(self, x) result(v) + class(png_renderer_t), intent(in) :: self + real(dp), intent(in) :: x + real(dp) :: v + + v = (x - self%ox)*self%cv%scale + end function tx + + pure function ty(self, y) result(v) + class(png_renderer_t), intent(in) :: self + real(dp), intent(in) :: y + real(dp) :: v + + v = (y - self%oy)*self%cv%scale + end function ty + + ! The clip travels in the paint in points; the rasterizer wants it in the + ! same shifted frame as everything else. + function shifted_clip(self, c) result(r) + class(png_renderer_t), intent(in) :: self + type(clip_t), intent(in) :: c + type(clip_t) :: r + + r = c + r%x = c%x - self%ox + r%y = c%y - self%oy + end function shifted_clip + + ! Snap where matplotlib would. The width that decides the offset is the + ! stroke width in pixels, or zero for a path that is only filled. + subroutine maybe_snap(self, paint) + class(png_renderer_t), intent(inout) :: self + type(paint_t), intent(in) :: paint + real(dp) :: lw + + if (.not. pb_should_snap(self%pb)) return + lw = 0.0_dp + if (paint%stroked) lw = paint%line_width*self%cv%scale + call pb_snap(self%pb, lw) + end subroutine maybe_snap + + ! Fill then stroke, which is the order every one of these formats paints + ! in and the order the SVG backend's attributes imply. + subroutine paint_buf(self, paint) + class(png_renderer_t), intent(inout) :: self + type(paint_t), intent(in) :: paint + type(paint_t) :: p + + p = paint + p%clip = shifted_clip(self, paint%clip) + if (paint%filled) then + call fill_path_buf(self%cv, self%pb, paint%fill_rgb, paint%fill_alpha, p%clip) + end if + if (paint%stroked) call stroke_path_buf(self%cv, self%pb, p) + end subroutine paint_buf + + ! ------------------------------------------------------------------ + ! Primitives + ! ------------------------------------------------------------------ + + subroutine build_path(self, x, y, verbs, n_verb) + class(png_renderer_t), intent(inout) :: self + real(dp), intent(in) :: x(:), y(:) + integer, intent(in) :: verbs(:), n_verb + integer :: i, p + + call pb_reset(self%pb) + p = 0 + do i = 1, n_verb + select case (verbs(i)) + case (VERB_MOVE) + p = p + 1 + call pb_move(self%pb, tx(self, x(p)), ty(self, y(p))) + case (VERB_LINE) + p = p + 1 + call pb_line(self%pb, tx(self, x(p)), ty(self, y(p))) + case (VERB_CUBIC) + call pb_cubic(self%pb, & + tx(self, x(p + 1)), ty(self, y(p + 1)), & + tx(self, x(p + 2)), ty(self, y(p + 2)), & + tx(self, x(p + 3)), ty(self, y(p + 3))) + p = p + 3 + case (VERB_CLOSE) + call pb_close(self%pb) + end select + end do + end subroutine build_path + + subroutine png_draw_path(self, x, y, verbs, n_verb, paint) + class(png_renderer_t), intent(inout) :: self + real(dp), intent(in) :: x(:), y(:) + integer, intent(in) :: verbs(:), n_verb + type(paint_t), intent(in) :: paint + + call build_path(self, x, y, verbs, n_verb) + call maybe_snap(self, paint) + call paint_buf(self, paint) + end subroutine png_draw_path + + subroutine png_draw_rect(self, x, y, w, h, paint, radius) + class(png_renderer_t), intent(inout) :: self + real(dp), intent(in) :: x, y, w, h + type(paint_t), intent(in) :: paint + real(dp), intent(in), optional :: radius + real(dp) :: r, k, x0, y0, x1, y1 + + r = 0.0_dp + if (present(radius)) r = min(radius, 0.5_dp*min(w, h)) + x0 = tx(self, x); y0 = ty(self, y) + x1 = tx(self, x + w); y1 = ty(self, y + h) + + call pb_reset(self%pb) + if (r <= 0.0_dp) then + call pb_move(self%pb, x0, y0) + call pb_line(self%pb, x1, y0) + call pb_line(self%pb, x1, y1) + call pb_line(self%pb, x0, y1) + else + r = r*self%cv%scale + k = r*0.5522847498307933_dp + call pb_move(self%pb, x0 + r, y0) + call pb_line(self%pb, x1 - r, y0) + call pb_cubic(self%pb, x1 - r + k, y0, x1, y0 + r - k, x1, y0 + r) + call pb_line(self%pb, x1, y1 - r) + call pb_cubic(self%pb, x1, y1 - r + k, x1 - r + k, y1, x1 - r, y1) + call pb_line(self%pb, x0 + r, y1) + call pb_cubic(self%pb, x0 + r - k, y1, x0, y1 - r + k, x0, y1 - r) + call pb_line(self%pb, x0, y0 + r) + call pb_cubic(self%pb, x0, y0 + r - k, x0 + r - k, y0, x0 + r, y0) + end if + call pb_close(self%pb) + call maybe_snap(self, paint) + call paint_buf(self, paint) + end subroutine png_draw_rect + + ! Straight to a polygon rather than through four cubics: the rasterizer + ! would flatten the cubics anyway, and going direct gives an exact circle + ! instead of the 0.02% error the cubic approximation carries. + subroutine png_draw_circle(self, cx, cy, r, paint) + class(png_renderer_t), intent(inout) :: self + real(dp), intent(in) :: cx, cy, r + type(paint_t), intent(in) :: paint + real(dp), parameter :: TWOPI = 6.283185307179586_dp + real(dp) :: rp, a + integer :: i, n + + rp = r*self%cv%scale + n = max(12, min(256, ceiling(rp*3.0_dp))) + call pb_reset(self%pb) + do i = 1, n + a = TWOPI*real(i - 1, dp)/real(n, dp) + if (i == 1) then + call pb_move(self%pb, tx(self, cx) + rp*cos(a), ty(self, cy) + rp*sin(a)) + else + call pb_line(self%pb, tx(self, cx) + rp*cos(a), ty(self, cy) + rp*sin(a)) + end if + end do + call pb_close(self%pb) + call paint_buf(self, paint) + end subroutine png_draw_circle + + ! The vector backends define the marker once and reference it; a raster + ! backend has to draw it every time, so the saving here is only in not + ! rebuilding the shape description. Stamping is still one fill per point + ! because overlapping markers must composite, not union. + subroutine png_draw_markers(self, x, y, mx, my, mverbs, n_mverb, paint) + class(png_renderer_t), intent(inout) :: self + real(dp), intent(in) :: x(:), y(:) + real(dp), intent(in) :: mx(:), my(:) + integer, intent(in) :: mverbs(:), n_mverb + type(paint_t), intent(in) :: paint + integer :: i, j, p + real(dp), allocatable :: sx(:), sy(:) + + allocate (sx(size(mx)), sy(size(my))) + do i = 1, size(x) + call pb_reset(self%pb) + p = 0 + do j = 1, n_mverb + select case (mverbs(j)) + case (VERB_MOVE) + p = p + 1 + call pb_move(self%pb, tx(self, x(i) + mx(p)), ty(self, y(i) + my(p))) + case (VERB_LINE) + p = p + 1 + call pb_line(self%pb, tx(self, x(i) + mx(p)), ty(self, y(i) + my(p))) + case (VERB_CUBIC) + call pb_cubic(self%pb, & + tx(self, x(i) + mx(p + 1)), ty(self, y(i) + my(p + 1)), & + tx(self, x(i) + mx(p + 2)), ty(self, y(i) + my(p + 2)), & + tx(self, x(i) + mx(p + 3)), ty(self, y(i) + my(p + 3))) + p = p + 3 + case (VERB_CLOSE) + call pb_close(self%pb) + end select + end do + call paint_buf(self, paint) + end do + end subroutine png_draw_markers + + ! ------------------------------------------------------------------ + ! Text + ! ------------------------------------------------------------------ + + function text_width(s, font) result(w) + character(len=*), intent(in) :: s + type(font_t), intent(in) :: font + real(dp) :: w + integer :: i + + w = 0.0_dp + do i = 1, len(s) + w = w + glyph_advance(iachar(s(i:i))) + end do + w = w*font%size/EM + end function text_width + + subroutine png_draw_text(self, x, y, s, font, paint, anchor, baseline, angle) + class(png_renderer_t), intent(inout) :: self + real(dp), intent(in) :: x, y + character(len=*), intent(in) :: s + type(font_t), intent(in) :: font + type(paint_t), intent(in) :: paint + integer, intent(in) :: anchor, baseline + real(dp), intent(in) :: angle + real(dp) :: pen, dyv, sc, ca, sa, ax, ay, gx, gy + real(dp) :: c1x, c1y, c2x, c2y, ex, ey + real(dp), allocatable :: px(:), py(:) + integer, allocatable :: gv(:) + integer :: i, j, p, nv, np, code + type(paint_t) :: pp + + if (len_trim(s) == 0) return + sc = font%size/EM + + pen = 0.0_dp + select case (anchor) + case (ANCHOR_MIDDLE); pen = -0.5_dp*text_width(s, font) + case (ANCHOR_END); pen = -text_width(s, font) + end select + + ! Only the alphabetic baseline is exact; the others are the usual + ! interpretations, so that they are at least sensible if ever used. + select case (baseline) + case (BASE_MIDDLE); dyv = 0.5_dp*XHEIGHT*sc + case (BASE_TOP); dyv = ASCENT*sc + case (BASE_BOTTOM); dyv = DESCENT*sc + case default; dyv = 0.0_dp + end select + + ca = cos(angle*3.141592653589793_dp/180.0_dp) + sa = sin(angle*3.141592653589793_dp/180.0_dp) + ax = tx(self, x) + ay = ty(self, y) + + pp = paint + pp%stroked = .false. + pp%filled = .true. + pp%clip = shifted_clip(self, paint%clip) + + call pb_reset(self%pb) + do i = 1, len(s) + code = iachar(s(i:i)) + call glyph_verbs(code, gv, nv) + call glyph_points(code, px, py, np) + p = 0 + do j = 1, nv + select case (gv(j)) + case (VERB_MOVE) + p = p + 1 + call gpt(px(p), py(p), gx, gy) + call pb_move(self%pb, gx, gy) + case (VERB_LINE) + p = p + 1 + call gpt(px(p), py(p), gx, gy) + call pb_line(self%pb, gx, gy) + case (VERB_CUBIC) + call gpt(px(p + 1), py(p + 1), c1x, c1y) + call gpt(px(p + 2), py(p + 2), c2x, c2y) + call gpt(px(p + 3), py(p + 3), ex, ey) + p = p + 3 + call pb_cubic(self%pb, c1x, c1y, c2x, c2y, ex, ey) + case (VERB_CLOSE) + call pb_close(self%pb) + end select + end do + pen = pen + glyph_advance(code)*sc + end do + + call fill_path_buf(self%cv, self%pb, paint%fill_rgb, paint%fill_alpha, pp%clip) + + contains + + ! Font units are y up and the canvas is y down, so the glyph is + ! flipped as it is placed. Rotation is about the anchor point, which + ! is what makes a rotated y axis label pivot where SVG pivots it. + subroutine gpt(fx, fy, ox_, oy_) + real(dp), intent(in) :: fx, fy + real(dp), intent(out) :: ox_, oy_ + real(dp) :: lx, ly + + lx = (pen + fx*sc)*self%cv%scale + ly = (dyv - fy*sc)*self%cv%scale + ox_ = ax + lx*ca - ly*sa + oy_ = ay + lx*sa + ly*ca + end subroutine gpt + + end subroutine png_draw_text + + subroutine png_draw_image(self, x, y, w, h, rgba, nx, ny, paint) + class(png_renderer_t), intent(inout) :: self + real(dp), intent(in) :: x, y, w, h + integer, intent(in) :: nx, ny + integer, intent(in) :: rgba(4, nx, ny) + type(paint_t), intent(in) :: paint + + call blit_image(self%cv, x - self%ox, y - self%oy, w, h, rgba, nx, ny, & + paint%fill_alpha, shifted_clip(self, paint%clip)) + end subroutine png_draw_image + +end module fplot_backend_png diff --git a/src/fplot_backend_svg.f90 b/src/fplot_backend_svg.f90 new file mode 100644 index 0000000..5f70215 --- /dev/null +++ b/src/fplot_backend_svg.f90 @@ -0,0 +1,517 @@ +! SVG backend: the fplot_render contract written out as XML. +! +! This is the reference implementation of renderer_t and the easiest of the +! four to read, because SVG has a native element for almost everything the +! API asks for. Where it does not, the translation is still local: a marker +! set becomes one shape plus a per point, which is what +! matplotlib emits and what keeps a scatter plot from repeating the same +! outline a thousand times. +! +! Attribute order is fixed here rather than at the call site: fill first, +! then stroke, then dashes. fplot used to spell the same paint differently +! depending on which helper emitted it, which made the output harder to diff +! than it needed to be. +module fplot_backend_svg + use fplot_style, only: dp + use fplot_render + use fplot_svg, only: svg_builder, builder_init, builder_append, & + builder_get, fmt_num, xml_escape_to + implicit none + private + + public :: svg_renderer_t + + type, extends(renderer_t) :: svg_renderer_t + type(svg_builder) :: b + ! Serial numbers for the ids that and clip-path refer to. + integer :: n_marker = 0 + integer :: n_clip = 0 + ! The clip currently open as a , so that a run of primitives + ! sharing one clip pays for it once. The backend discovers this by + ! itself; nothing in the API mentions it. + type(clip_t) :: cur_clip + integer :: cur_clip_id = 0 + logical :: in_clip = .false. + contains + procedure :: open_canvas => svg_open_canvas + procedure :: close_canvas => svg_close_canvas + procedure :: bytes => svg_bytes + procedure :: draw_path => svg_draw_path + procedure :: draw_rect => svg_draw_rect + procedure :: draw_circle => svg_draw_circle + procedure :: draw_markers => svg_draw_markers + procedure :: draw_text => svg_draw_text + procedure :: draw_image => svg_draw_image + procedure :: begin_group => svg_begin_group + procedure :: end_group => svg_end_group + end type svg_renderer_t + +contains + + subroutine put(self, s) + class(svg_renderer_t), intent(inout) :: self + character(len=*), intent(in) :: s + call builder_append(self%b, s) + end subroutine put + + subroutine put_num(self, x) + class(svg_renderer_t), intent(inout) :: self + real(dp), intent(in) :: x + character(len=64) :: s + integer :: n + call fmt_num(x, s, n) + call builder_append(self%b, s(1:n)) + end subroutine put_num + + subroutine put_int(self, i) + class(svg_renderer_t), intent(inout) :: self + integer, intent(in) :: i + character(len=12) :: t + write (t, "(I0)") i + call builder_append(self%b, trim(t)) + end subroutine put_int + + subroutine put_eol(self) + class(svg_renderer_t), intent(inout) :: self + call builder_append(self%b, new_line("a")) + end subroutine put_eol + + ! "#rrggbb", lower case, which is the spelling matplotlib uses. + subroutine put_color(self, rgb) + class(svg_renderer_t), intent(inout) :: self + integer, intent(in) :: rgb(3) + character(len=7) :: s + character(len=16), parameter :: hexd = "0123456789abcdef" + integer :: i, v + s(1:1) = "#" + do i = 1, 3 + v = max(0, min(255, rgb(i))) + s(2*i:2*i) = hexd(v/16 + 1:v/16 + 1) + s(2*i + 1:2*i + 1) = hexd(mod(v, 16) + 1:mod(v, 16) + 1) + end do + call put(self, s) + end subroutine put_color + + ! Everything about a paint except its geometry. Emitted in one fixed + ! order so that two identical paints always produce identical text. + subroutine put_paint(self, p, skip_fill) + class(svg_renderer_t), intent(inout) :: self + type(paint_t), intent(in) :: p + logical, intent(in), optional :: skip_fill + logical :: nofill + integer :: i + + nofill = .false. + if (present(skip_fill)) nofill = skip_fill + + if (.not. nofill) then + call put(self, ' fill="') + if (p%filled) then + call put_color(self, p%fill_rgb) + else + call put(self, "none") + end if + call put(self, '"') + if (p%filled .and. p%fill_alpha < 1.0_dp) then + call put(self, ' fill-opacity="') + call put_num(self, p%fill_alpha) + call put(self, '"') + end if + if (p%filled .and. p%fill_rule == FILL_EVENODD) then + call put(self, ' fill-rule="evenodd"') + end if + end if + + if (p%stroked) then + call put(self, ' stroke="') + call put_color(self, p%stroke_rgb) + call put(self, '" stroke-width="') + call put_num(self, p%line_width) + call put(self, '"') + if (p%stroke_alpha < 1.0_dp) then + call put(self, ' stroke-opacity="') + call put_num(self, p%stroke_alpha) + call put(self, '"') + end if + ! butt and miter are the SVG defaults, so say nothing. + select case (p%cap) + case (CAP_ROUND); call put(self, ' stroke-linecap="round"') + case (CAP_SQUARE); call put(self, ' stroke-linecap="square"') + end select + select case (p%join) + case (JOIN_ROUND); call put(self, ' stroke-linejoin="round"') + case (JOIN_BEVEL); call put(self, ' stroke-linejoin="bevel"') + end select + if (p%n_dash > 0) then + call put(self, ' stroke-dasharray="') + do i = 1, p%n_dash + if (i > 1) call put(self, ",") + call put_num(self, p%dash(i)) + end do + call put(self, '"') + if (p%dash_offset /= 0.0_dp) then + call put(self, ' stroke-dashoffset="') + call put_num(self, p%dash_offset) + call put(self, '"') + end if + end if + end if + end subroutine put_paint + + logical function same_clip(a, c) + type(clip_t), intent(in) :: a, c + same_clip = (a%on .eqv. c%on) + if (.not. same_clip) return + if (.not. a%on) return + same_clip = (a%x == c%x) .and. (a%y == c%y) .and. & + (a%w == c%w) .and. (a%h == c%h) + end function same_clip + + ! Open or close the wrapper as the clip changes. This + ! is purely a size optimization; drawing one clipped shape at a time + ! would be equally correct. + subroutine sync_clip(self, c) + class(svg_renderer_t), intent(inout) :: self + type(clip_t), intent(in) :: c + + if (same_clip(self%cur_clip, c)) return + + if (self%in_clip) then + call put(self, "") + call put_eol(self) + self%in_clip = .false. + end if + + if (c%on) then + self%n_clip = self%n_clip + 1 + self%cur_clip_id = self%n_clip + call put(self, '') + call put_eol(self) + call put(self, '') + call put_eol(self) + self%in_clip = .true. + end if + + self%cur_clip = c + end subroutine sync_clip + + subroutine svg_open_canvas(self, width, height, bg_rgb, bg_alpha, x0, y0) + class(svg_renderer_t), intent(inout) :: self + real(dp), intent(in) :: width, height + integer, intent(in), optional :: bg_rgb(3) + real(dp), intent(in), optional :: bg_alpha + real(dp), intent(in), optional :: x0, y0 + real(dp) :: ox, oy + + ox = 0.0_dp + oy = 0.0_dp + if (present(x0)) ox = x0 + if (present(y0)) oy = y0 + + call builder_init(self%b) + call put(self, '') + call put_eol(self) + call put(self, '') + call put_eol(self) + + if (present(bg_rgb)) then + call put(self, '") + call put_eol(self) + end if + end subroutine svg_open_canvas + + subroutine svg_close_canvas(self) + class(svg_renderer_t), intent(inout) :: self + type(clip_t) :: none + call sync_clip(self, none) + call put(self, "") + call put_eol(self) + end subroutine svg_close_canvas + + function svg_bytes(self) result(s) + class(svg_renderer_t), intent(in) :: self + character(len=:), allocatable :: s + s = builder_get(self%b) + end function svg_bytes + + ! Writes the d="" attribute body. Shared by draw_path and by the marker + ! shape definition. + subroutine put_path_data(self, x, y, verbs, n_verb) + class(svg_renderer_t), intent(inout) :: self + real(dp), intent(in) :: x(:), y(:) + integer, intent(in) :: verbs(:), n_verb + integer :: i, k, j + logical :: first + + k = 0 + first = .true. + do i = 1, n_verb + select case (verbs(i)) + case (VERB_MOVE) + k = k + 1 + if (.not. first) call put(self, " ") + call put(self, "M ") + call put_num(self, x(k)) + call put(self, " ") + call put_num(self, y(k)) + first = .false. + case (VERB_LINE) + k = k + 1 + call put(self, " L ") + call put_num(self, x(k)) + call put(self, " ") + call put_num(self, y(k)) + case (VERB_CUBIC) + call put(self, " C") + do j = 1, 3 + k = k + 1 + call put(self, " ") + call put_num(self, x(k)) + call put(self, " ") + call put_num(self, y(k)) + end do + case (VERB_CLOSE) + call put(self, " Z") + end select + end do + end subroutine put_path_data + + subroutine svg_draw_path(self, x, y, verbs, n_verb, paint) + class(svg_renderer_t), intent(inout) :: self + real(dp), intent(in) :: x(:), y(:) + integer, intent(in) :: verbs(:), n_verb + type(paint_t), intent(in) :: paint + + if (n_verb <= 0) return + call sync_clip(self, paint%clip) + call put(self, '") + call put_eol(self) + end subroutine svg_draw_path + + subroutine svg_draw_rect(self, x, y, w, h, paint, radius) + class(svg_renderer_t), intent(inout) :: self + real(dp), intent(in) :: x, y, w, h + type(paint_t), intent(in) :: paint + real(dp), intent(in), optional :: radius + + call sync_clip(self, paint%clip) + call put(self, ' 0.0_dp) then + call put(self, ' rx="') + call put_num(self, radius) + call put(self, '"') + end if + end if + call put_paint(self, paint) + call put(self, "/>") + call put_eol(self) + end subroutine svg_draw_rect + + subroutine svg_draw_circle(self, cx, cy, r, paint) + class(svg_renderer_t), intent(inout) :: self + real(dp), intent(in) :: cx, cy, r + type(paint_t), intent(in) :: paint + + call sync_clip(self, paint%clip) + call put(self, '") + call put_eol(self) + end subroutine svg_draw_circle + + ! One shape and a per point. For a thousand point scatter + ! this is the difference between naming the outline once and repeating + ! it a thousand times. + subroutine svg_draw_markers(self, x, y, mx, my, mverbs, n_mverb, paint) + class(svg_renderer_t), intent(inout) :: self + real(dp), intent(in) :: x(:), y(:) + real(dp), intent(in) :: mx(:), my(:) + integer, intent(in) :: mverbs(:), n_mverb + type(paint_t), intent(in) :: paint + integer :: i + + if (size(x) <= 0 .or. n_mverb <= 0) return + call sync_clip(self, paint%clip) + + self%n_marker = self%n_marker + 1 + call put(self, '') + call put_eol(self) + + do i = 1, size(x) + call put(self, '") + call put_eol(self) + end do + end subroutine svg_draw_markers + + subroutine svg_draw_text(self, x, y, s, font, paint, anchor, baseline, angle) + class(svg_renderer_t), intent(inout) :: self + real(dp), intent(in) :: x, y + character(len=*), intent(in) :: s + type(font_t), intent(in) :: font + type(paint_t), intent(in) :: paint + integer, intent(in) :: anchor, baseline + real(dp), intent(in) :: angle + character(len=6*len(s)) :: esc + integer :: en + + call sync_clip(self, paint%clip) + call put(self, '") + ! Escaping is the backend's business: the caller hands over the text + ! the user asked for, and each format spells it its own way. + call xml_escape_to(s, esc, en) + call put(self, esc(1:en)) + call put(self, "") + call put_eol(self) + end subroutine svg_draw_text + + ! A raster block becomes a base64 data URI holding a PNG. Left until the + ! PNG backend exists, since that is where the encoder will live; fplot + ! currently draws imshow as a grid of rectangles instead. + subroutine svg_draw_image(self, x, y, w, h, rgba, nx, ny, paint) + class(svg_renderer_t), intent(inout) :: self + real(dp), intent(in) :: x, y, w, h + integer, intent(in) :: nx, ny + integer, intent(in) :: rgba(4, nx, ny) + type(paint_t), intent(in) :: paint + end subroutine svg_draw_image + + subroutine svg_begin_group(self, name) + class(svg_renderer_t), intent(inout) :: self + character(len=*), intent(in) :: name + call put(self, '') + call put_eol(self) + end subroutine svg_begin_group + + subroutine svg_end_group(self) + class(svg_renderer_t), intent(inout) :: self + type(clip_t) :: none + ! A clip must not outlive the group that contains it. + call sync_clip(self, none) + call put(self, "") + call put_eol(self) + end subroutine svg_end_group + +end module fplot_backend_svg diff --git a/src/fplot_glyphs.f90 b/src/fplot_glyphs.f90 new file mode 100644 index 0000000..d5ca42c --- /dev/null +++ b/src/fplot_glyphs.f90 @@ -0,0 +1,965 @@ +! fplot_glyphs — DejaVu Sans outlines, generated by tools/gen_glyphs.py. +! +! Do not edit. The PNG backend fills text as outlines because a raster +! file cannot defer to a font the way SVG and PDF do, and these are the +! outlines matplotlib itself draws with. +! +! Coordinates are in font units; divide by EM and multiply by the point +! size. Curves are cubic, matching fplot_render's path verbs, so the +! backend fills a glyph with exactly the code that fills any other path. + +module fplot_glyphs + use fplot_style, only: dp + implicit none + private + + public :: EM, ASCENT, DESCENT, XHEIGHT, GLYPH_FIRST, GLYPH_LAST + public :: glyph_advance, glyph_verbs, glyph_points + + real(dp), parameter :: EM = 2048.0_dp + real(dp), parameter :: ASCENT = 1901.0_dp + real(dp), parameter :: DESCENT = -483.0_dp + real(dp), parameter :: XHEIGHT = 1120.0_dp + integer, parameter :: GLYPH_FIRST = 32 + integer, parameter :: GLYPH_LAST = 126 + + integer, parameter :: NCH = 95 + integer, parameter :: NV = 1644 + integer, parameter :: NP = 3022 + + ! Per character: where its verbs and points start, how many verbs, + ! and the advance width to the next character. + integer, parameter :: VBEG(NCH) = [ & + 1, 1, 13, 23, 57, 98, 143, 181, 186, 197, 209, 228, & + 241, 248, 253, 258, 263, 283, 295, 318, 349, 365, 388, 417, & + 425, 463, 492, 502, 514, 522, 532, 540, 570, 626, 639, 667, & + 687, 703, 716, 727, 751, 764, 769, 780, 792, 799, 813, 824, & + 844, 862, 887, 913, 943, 952, 967, 975, 989, 1002, 1012, 1023, & + 1032, 1037, 1046, 1054, 1059, 1064, 1095, 1119, 1139, 1164, 1187, 1206, & + 1238, 1256, 1266, 1282, 1294, 1299, 1328, 1346, 1366, 1391, 1415, 1430, & + 1460, 1479, 1499, 1507, 1521, 1534, 1549, 1560, 1590, 1595, 1625 & + ] + + integer, parameter :: VLEN(NCH) = [ & + 0, 12, 10, 34, 41, 45, 38, 5, 11, 12, 19, 13, & + 7, 5, 5, 5, 20, 12, 23, 31, 16, 23, 29, 8, & + 38, 29, 10, 12, 8, 10, 8, 30, 56, 13, 28, 20, & + 16, 13, 11, 24, 13, 5, 11, 12, 7, 14, 11, 20, & + 18, 25, 26, 30, 9, 15, 8, 14, 13, 10, 11, 9, & + 5, 9, 8, 5, 5, 31, 24, 20, 25, 23, 19, 32, & + 18, 10, 16, 12, 5, 29, 18, 20, 25, 24, 15, 30, & + 19, 20, 8, 14, 13, 15, 11, 30, 5, 30, 20 & + ] + + integer, parameter :: PBEG(NCH) = [ & + 1, 1, 11, 19, 51, 137, 241, 333, 337, 363, 390, 408, & + 420, 426, 430, 434, 438, 488, 499, 553, 631, 645, 699, 774, & + 781, 880, 955, 963, 973, 980, 988, 995, 1059, 1209, 1220, 1277, & + 1328, 1358, 1370, 1380, 1435, 1447, 1451, 1469, 1480, 1486, 1499, 1509, & + 1559, 1591, 1650, 1698, 1775, 1783, 1813, 1820, 1833, 1845, 1854, 1864, & + 1872, 1876, 1884, 1891, 1895, 1899, 1968, 2022, 2073, 2128, 2181, 2207, & + 2285, 2318, 2326, 2348, 2359, 2363, 2423, 2456, 2506, 2561, 2615, 2645, & + 2722, 2748, 2782, 2789, 2802, 2814, 2836, 2846, 2907, 2911, 2972 & + ] + + integer, parameter :: ADV(NCH) = [ & + 651, 821, 942, 1716, 1303, 1946, 1597, 563, 799, 799, 1024, 1716, & + 651, 739, 651, 690, 1303, 1303, 1303, 1303, 1303, 1303, 1303, 1303, & + 1303, 1303, 690, 690, 1716, 1716, 1716, 1087, 2048, 1401, 1405, 1430, & + 1577, 1294, 1178, 1587, 1540, 604, 604, 1343, 1141, 1767, 1532, 1612, & + 1235, 1612, 1423, 1300, 1251, 1499, 1401, 2025, 1403, 1251, 1403, 799, & + 690, 799, 1716, 1024, 1024, 1255, 1300, 1126, 1300, 1260, 721, 1300, & + 1298, 569, 569, 1186, 569, 1995, 1298, 1253, 1300, 1300, 842, 1067, & + 803, 1298, 1212, 1675, 1212, 1212, 1075, 1303, 690, 1303, 1716 & + ] + + integer, parameter :: VERBS(NV) = [ & + 1, 2, 2, 2, 4, 1, 2, 2, 2, 2, 2, 4, 1, 2, 2, 2, 4, 1, 2, 2, 2, 4, 1, 2, 2, & + 2, 4, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, & + 2, 2, 2, 2, 2, 4, 1, 2, 2, 3, 3, 2, 3, 3, 2, 3, 3, 3, 3, 2, 2, 2, 3, 3, 2, & + 3, 3, 2, 3, 3, 3, 3, 4, 1, 2, 3, 3, 3, 3, 4, 1, 2, 3, 3, 3, 3, 4, 1, 3, 3, & + 3, 3, 3, 3, 3, 3, 4, 1, 3, 3, 3, 3, 3, 3, 3, 3, 4, 1, 3, 3, 3, 3, 3, 3, 3, & + 3, 4, 1, 2, 2, 2, 4, 1, 3, 3, 3, 3, 3, 3, 3, 3, 4, 1, 3, 3, 3, 3, 3, 3, 4, & + 1, 2, 3, 3, 2, 3, 3, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, & + 3, 3, 3, 3, 4, 1, 2, 2, 2, 4, 1, 3, 3, 3, 3, 2, 3, 3, 3, 3, 4, 1, 2, 3, 3, & + 3, 3, 2, 3, 3, 3, 3, 4, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, & + 2, 4, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 1, 2, 2, 2, 2, 2, 4, 1, 2, 2, & + 2, 4, 1, 2, 2, 2, 4, 1, 2, 2, 2, 4, 1, 3, 3, 3, 3, 3, 3, 3, 3, 4, 1, 3, 3, & + 3, 3, 3, 3, 3, 3, 4, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 1, 2, 2, 2, 2, 3, & + 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 1, 3, 3, 3, 3, 3, 3, 2, & + 3, 3, 3, 3, 3, 3, 2, 2, 2, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 4, 1, 2, & + 2, 4, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, & + 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 1, 3, 3, 3, 3, 3, 3, 3, 3, 4, 1, 2, 3, & + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 1, 2, 2, 2, 2, 2, 2, 4, 1, & + 3, 3, 3, 3, 3, 3, 3, 3, 4, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, & + 3, 4, 1, 3, 3, 3, 3, 3, 3, 3, 3, 4, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, & + 3, 3, 3, 3, 3, 4, 1, 3, 3, 3, 3, 3, 3, 3, 3, 4, 1, 2, 2, 2, 4, 1, 2, 2, 2, & + 4, 1, 2, 2, 2, 4, 1, 2, 2, 2, 2, 2, 4, 1, 2, 2, 2, 2, 2, 2, 4, 1, 2, 2, 2, & + 4, 1, 2, 2, 2, 4, 1, 2, 2, 2, 2, 2, 2, 4, 1, 2, 2, 2, 4, 1, 2, 2, 3, 3, 2, & + 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 4, 1, 3, 3, 3, 3, 3, & + 3, 3, 3, 4, 1, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, & + 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, & + 1, 2, 2, 4, 1, 2, 2, 2, 2, 2, 2, 2, 4, 1, 2, 2, 3, 3, 3, 3, 4, 1, 2, 2, 3, & + 3, 3, 3, 4, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 2, 4, 1, 2, 3, 3, 3, 3, 3, 3, 3, & + 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 1, 2, 2, 3, 3, 3, 3, 4, 1, 2, 3, 3, 3, 3, & + 2, 4, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, & + 4, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, & + 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 1, 2, 2, 2, 4, 1, 2, 2, 3, 3, 2, 2, & + 2, 3, 3, 4, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 1, 2, 2, 2, 2, 2, 4, 1, 2, & + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 1, 3, & + 3, 3, 3, 3, 3, 3, 3, 4, 1, 3, 3, 3, 3, 3, 3, 3, 3, 4, 1, 2, 2, 3, 3, 3, 3, & + 4, 1, 2, 3, 3, 3, 3, 2, 2, 2, 4, 1, 3, 3, 3, 3, 3, 3, 3, 3, 4, 1, 2, 2, 2, & + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 1, 3, 3, 2, 2, 2, 3, 3, 2, 2, 2, 2, 2, 3, & + 3, 3, 3, 4, 1, 2, 2, 3, 3, 3, 3, 4, 1, 2, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, & + 3, 3, 2, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 4, 1, 2, 2, 2, 2, 2, 2, 2, & + 4, 1, 2, 2, 3, 3, 3, 3, 2, 2, 2, 3, 3, 3, 3, 4, 1, 2, 2, 2, 2, 2, 2, 4, 1, & + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, & + 4, 1, 2, 2, 2, 2, 2, 2, 2, 2, 4, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 1, 2, 2, & + 2, 2, 2, 2, 2, 4, 1, 2, 2, 2, 4, 1, 2, 2, 2, 2, 2, 2, 2, 4, 1, 2, 2, 2, 2, & + 2, 2, 4, 1, 2, 2, 2, 4, 1, 2, 2, 2, 4, 1, 3, 3, 3, 3, 3, 3, 2, 4, 1, 2, 2, & + 2, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 3, 3, 2, 3, 3, 3, 3, 4, 1, 3, 3, 3, 3, 3, & + 3, 3, 3, 4, 1, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 4, 1, 2, 3, 3, 3, 3, 3, & + 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, & + 3, 3, 4, 1, 3, 3, 3, 3, 3, 3, 3, 3, 4, 1, 2, 2, 3, 3, 3, 3, 2, 3, 3, 3, 3, & + 3, 3, 3, 3, 4, 1, 3, 3, 3, 3, 4, 1, 2, 2, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, & + 2, 2, 3, 3, 4, 1, 3, 3, 3, 3, 3, 3, 3, 3, 4, 1, 3, 3, 3, 3, 2, 3, 3, 3, 3, & + 2, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 4, 1, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 2, & + 3, 3, 3, 3, 4, 1, 2, 2, 2, 4, 1, 2, 2, 2, 4, 1, 2, 2, 3, 3, 2, 2, 2, 3, 3, & + 4, 1, 2, 2, 2, 4, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 1, 2, 2, 2, 4, 1, 3, & + 3, 3, 3, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 2, 3, 3, 3, & + 3, 4, 1, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 1, 3, 3, 3, 3, & + 3, 3, 3, 3, 4, 1, 3, 3, 3, 3, 3, 3, 3, 3, 4, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, & + 3, 3, 3, 3, 4, 1, 3, 3, 3, 3, 3, 3, 3, 3, 4, 1, 3, 3, 3, 3, 3, 3, 3, 3, 4, & + 1, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 4, 1, 3, 3, 3, 3, 2, 2, 2, 2, 2, 3, & + 3, 3, 3, 4, 1, 2, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, & + 3, 2, 3, 3, 3, 3, 3, 3, 4, 1, 2, 2, 2, 2, 2, 3, 3, 2, 2, 2, 3, 3, 2, 2, 2, & + 2, 2, 4, 1, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 1, 4, 1, 2, & + 2, 2, 2, 2, 2, 4, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 1, 2, 2, 2, 2, & + 2, 2, 2, 2, 2, 2, 2, 4, 1, 3, 3, 2, 2, 2, 3, 3, 2, 2, 2, 2, 2, 2, 4, 1, 2, & + 2, 2, 2, 2, 2, 2, 2, 2, 4, 1, 2, 2, 3, 3, 2, 3, 3, 2, 2, 2, 3, 3, 2, 3, 3, & + 2, 2, 2, 3, 3, 2, 3, 3, 3, 3, 2, 3, 3, 4, 1, 2, 2, 2, 4, 1, 2, 3, 3, 2, 3, & + 3, 3, 3, 2, 3, 3, 2, 2, 2, 3, 3, 2, 3, 3, 2, 2, 2, 3, 3, 2, 3, 3, 2, 4, 1, & + 2, 3, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4 & + ] + + real(dp), parameter :: PX(NP) = [ & + 309.0_dp, 512.0_dp, 512.0_dp, 309.0_dp, 309.0_dp, 512.0_dp, 512.0_dp, 492.0_dp, & + 330.0_dp, 309.0_dp, 367.0_dp, 367.0_dp, 197.0_dp, 197.0_dp, 745.0_dp, 745.0_dp, & + 575.0_dp, 575.0_dp, 1047.0_dp, 756.0_dp, 672.0_dp, 965.0_dp, 897.0_dp, 793.0_dp, & + 1085.0_dp, 1190.0_dp, 1350.0_dp, 1247.0_dp, 1559.0_dp, 1559.0_dp, 1208.0_dp, 1126.0_dp, & + 1444.0_dp, 1444.0_dp, 1087.0_dp, 983.0_dp, 823.0_dp, 926.0_dp, 633.0_dp, 530.0_dp, & + 369.0_dp, 473.0_dp, 158.0_dp, 158.0_dp, 510.0_dp, 594.0_dp, 272.0_dp, 272.0_dp, & + 633.0_dp, 735.0_dp, 692.0_dp, 592.0_dp, 591.0_dp, 521.0_dp, 451.0_dp, 381.0_dp, & + 311.0_dp, 240.7_dp, 170.0_dp, 170.0_dp, 238.0_dp, 306.8_dp, 376.5_dp, 446.2_dp, & + 518.0_dp, 592.0_dp, 592.0_dp, 444.7_dp, 337.5_dp, 270.5_dp, 203.5_dp, 170.0_dp, & + 170.0_dp, 170.0_dp, 206.3_dp, 279.0_dp, 351.7_dp, 456.0_dp, 592.0_dp, 592.0_dp, & + 692.0_dp, 692.0_dp, 754.0_dp, 814.0_dp, 872.0_dp, 930.0_dp, 986.7_dp, 1042.0_dp, & + 1042.0_dp, 986.7_dp, 929.8_dp, 871.5_dp, 813.2_dp, 753.3_dp, 692.0_dp, 692.0_dp, & + 843.3_dp, 954.7_dp, 1026.0_dp, 1097.3_dp, 1133.0_dp, 1133.0_dp, 1133.0_dp, 1095.2_dp, & + 1019.5_dp, 943.8_dp, 834.7_dp, 692.0_dp, 592.0_dp, 592.0_dp, 514.7_dp, 455.7_dp, & + 415.0_dp, 374.3_dp, 354.0_dp, 354.0_dp, 354.0_dp, 372.8_dp, 410.5_dp, 448.2_dp, & + 508.7_dp, 592.0_dp, 692.0_dp, 692.0_dp, 776.7_dp, 840.5_dp, 883.5_dp, 926.5_dp, & + 948.0_dp, 948.0_dp, 948.0_dp, 927.5_dp, 886.5_dp, 845.5_dp, 780.7_dp, 692.0_dp, & + 1489.0_dp, 1431.0_dp, 1385.5_dp, 1352.5_dp, 1319.5_dp, 1303.0_dp, 1303.0_dp, 1303.0_dp, & + 1319.5_dp, 1352.5_dp, 1385.5_dp, 1431.0_dp, 1489.0_dp, 1545.7_dp, 1590.5_dp, 1623.5_dp, & + 1656.5_dp, 1673.0_dp, 1673.0_dp, 1673.0_dp, 1656.5_dp, 1623.5_dp, 1590.5_dp, 1545.7_dp, & + 1489.0_dp, 1489.0_dp, 1594.3_dp, 1678.0_dp, 1740.0_dp, 1802.0_dp, 1833.0_dp, 1833.0_dp, & + 1833.0_dp, 1801.8_dp, 1739.5_dp, 1677.2_dp, 1593.7_dp, 1489.0_dp, 1382.3_dp, 1298.0_dp, & + 1236.0_dp, 1174.0_dp, 1143.0_dp, 1143.0_dp, 1143.0_dp, 1174.2_dp, 1236.5_dp, 1298.8_dp, & + 1383.0_dp, 1489.0_dp, 457.0_dp, 399.7_dp, 354.5_dp, 321.5_dp, 288.5_dp, 272.0_dp, & + 272.0_dp, 272.0_dp, 288.3_dp, 321.0_dp, 353.7_dp, 399.0_dp, 457.0_dp, 515.0_dp, & + 560.5_dp, 593.5_dp, 626.5_dp, 643.0_dp, 643.0_dp, 643.0_dp, 626.3_dp, 593.0_dp, & + 559.7_dp, 514.3_dp, 457.0_dp, 1360.0_dp, 1520.0_dp, 586.0_dp, 426.0_dp, 457.0_dp, & + 562.3_dp, 646.3_dp, 709.0_dp, 771.7_dp, 803.0_dp, 803.0_dp, 803.0_dp, 771.8_dp, & + 709.5_dp, 647.2_dp, 563.0_dp, 457.0_dp, 351.0_dp, 267.2_dp, 205.5_dp, 143.8_dp, & + 113.0_dp, 113.0_dp, 113.0_dp, 144.0_dp, 206.0_dp, 268.0_dp, 351.7_dp, 457.0_dp, & + 498.0_dp, 437.3_dp, 392.8_dp, 364.5_dp, 336.2_dp, 322.0_dp, 322.0_dp, 322.0_dp, & + 357.3_dp, 428.0_dp, 498.7_dp, 587.3_dp, 694.0_dp, 757.3_dp, 816.7_dp, 872.0_dp, & + 927.3_dp, 979.3_dp, 1028.0_dp, 639.0_dp, 1147.0_dp, 1186.3_dp, 1217.0_dp, 1239.0_dp, & + 1261.0_dp, 1274.0_dp, 1278.0_dp, 1464.0_dp, 1456.0_dp, 1434.7_dp, 1400.0_dp, 1365.3_dp, & + 1317.0_dp, 1255.0_dp, 1534.0_dp, 1282.0_dp, 1139.0_dp, 1069.7_dp, 997.0_dp, 921.0_dp, & + 845.0_dp, 763.3_dp, 676.0_dp, 515.3_dp, 384.0_dp, 282.0_dp, 180.0_dp, 129.0_dp, & + 129.0_dp, 129.0_dp, 151.3_dp, 196.0_dp, 240.7_dp, 307.7_dp, 397.0_dp, 365.0_dp, & + 340.7_dp, 324.0_dp, 307.3_dp, 299.0_dp, 299.0_dp, 299.0_dp, 336.0_dp, 410.0_dp, & + 484.0_dp, 582.3_dp, 705.0_dp, 760.3_dp, 815.5_dp, 870.5_dp, 925.5_dp, 981.3_dp, & + 1038.0_dp, 1038.0_dp, 980.0_dp, 924.7_dp, 872.0_dp, 819.3_dp, 770.3_dp, 725.0_dp, & + 655.0_dp, 598.2_dp, 554.5_dp, 510.8_dp, 489.0_dp, 489.0_dp, 489.0_dp, 498.8_dp, & + 518.5_dp, 538.2_dp, 578.3_dp, 639.0_dp, 367.0_dp, 367.0_dp, 197.0_dp, 197.0_dp, & + 635.0_dp, 545.7_dp, 479.3_dp, 436.0_dp, 392.7_dp, 371.0_dp, 371.0_dp, 371.0_dp, & + 392.8_dp, 436.5_dp, 480.2_dp, 546.3_dp, 635.0_dp, 475.0_dp, 375.0_dp, 300.2_dp, & + 250.5_dp, 200.8_dp, 176.0_dp, 176.0_dp, 176.0_dp, 200.7_dp, 250.0_dp, 299.3_dp, & + 374.3_dp, 475.0_dp, 164.0_dp, 324.0_dp, 424.0_dp, 498.8_dp, 548.5_dp, 598.2_dp, & + 623.0_dp, 623.0_dp, 623.0_dp, 598.2_dp, 548.5_dp, 498.8_dp, 424.0_dp, 324.0_dp, & + 164.0_dp, 252.7_dp, 318.8_dp, 362.5_dp, 406.2_dp, 428.0_dp, 428.0_dp, 428.0_dp, & + 406.2_dp, 362.5_dp, 318.8_dp, 252.7_dp, 164.0_dp, 963.0_dp, 604.0_dp, 963.0_dp, & + 905.0_dp, 569.0_dp, 569.0_dp, 455.0_dp, 455.0_dp, 119.0_dp, 61.0_dp, 420.0_dp, & + 61.0_dp, 119.0_dp, 455.0_dp, 455.0_dp, 569.0_dp, 569.0_dp, 905.0_dp, 942.0_dp, & + 942.0_dp, 1499.0_dp, 1499.0_dp, 942.0_dp, 942.0_dp, 774.0_dp, 774.0_dp, 217.0_dp, & + 217.0_dp, 774.0_dp, 774.0_dp, 240.0_dp, 451.0_dp, 451.0_dp, 287.0_dp, 158.0_dp, & + 240.0_dp, 100.0_dp, 639.0_dp, 639.0_dp, 100.0_dp, 219.0_dp, 430.0_dp, 430.0_dp, & + 219.0_dp, 520.0_dp, 690.0_dp, 170.0_dp, 0.0_dp, 651.0_dp, 547.0_dp, 468.8_dp, & + 416.5_dp, 364.2_dp, 338.0_dp, 338.0_dp, 338.0_dp, 364.2_dp, 416.5_dp, 468.8_dp, & + 547.0_dp, 651.0_dp, 755.7_dp, 834.2_dp, 886.5_dp, 938.8_dp, 965.0_dp, 965.0_dp, & + 965.0_dp, 938.8_dp, 886.5_dp, 834.2_dp, 755.7_dp, 651.0_dp, 651.0_dp, 818.3_dp, & + 946.2_dp, 1034.5_dp, 1122.8_dp, 1167.0_dp, 1167.0_dp, 1167.0_dp, 1122.8_dp, 1034.5_dp, & + 946.2_dp, 818.3_dp, 651.0_dp, 483.7_dp, 355.8_dp, 267.5_dp, 179.2_dp, 135.0_dp, & + 135.0_dp, 135.0_dp, 179.2_dp, 267.5_dp, 355.8_dp, 483.7_dp, 651.0_dp, 254.0_dp, & + 584.0_dp, 584.0_dp, 225.0_dp, 225.0_dp, 582.0_dp, 784.0_dp, 784.0_dp, 1114.0_dp, & + 1114.0_dp, 254.0_dp, 393.0_dp, 1098.0_dp, 1098.0_dp, 150.0_dp, 150.0_dp, 226.7_dp, & + 331.2_dp, 463.5_dp, 595.8_dp, 679.0_dp, 713.0_dp, 777.7_dp, 822.8_dp, 848.5_dp, & + 874.2_dp, 887.0_dp, 887.0_dp, 887.0_dp, 859.2_dp, 803.5_dp, 747.8_dp, 675.3_dp, & + 586.0_dp, 522.7_dp, 455.8_dp, 385.5_dp, 315.2_dp, 240.0_dp, 160.0_dp, 160.0_dp, & + 241.3_dp, 317.3_dp, 388.0_dp, 458.7_dp, 523.3_dp, 582.0_dp, 736.7_dp, 860.0_dp, & + 952.0_dp, 1044.0_dp, 1090.0_dp, 1090.0_dp, 1090.0_dp, 1078.5_dp, 1055.5_dp, 1032.5_dp, & + 990.7_dp, 930.0_dp, 913.3_dp, 860.3_dp, 771.0_dp, 681.7_dp, 555.7_dp, 393.0_dp, & + 831.0_dp, 927.7_dp, 1003.2_dp, 1057.5_dp, 1111.8_dp, 1139.0_dp, 1139.0_dp, 1139.0_dp, & + 1088.3_dp, 987.0_dp, 885.7_dp, 741.7_dp, 555.0_dp, 492.3_dp, 427.8_dp, 361.5_dp, & + 295.2_dp, 226.7_dp, 156.0_dp, 156.0_dp, 212.0_dp, 273.3_dp, 340.0_dp, 406.7_dp, & + 476.3_dp, 549.0_dp, 675.7_dp, 772.2_dp, 838.5_dp, 904.8_dp, 938.0_dp, 938.0_dp, & + 938.0_dp, 907.2_dp, 845.5_dp, 783.8_dp, 698.0_dp, 588.0_dp, 414.0_dp, 414.0_dp, & + 596.0_dp, 695.3_dp, 771.3_dp, 824.0_dp, 876.7_dp, 903.0_dp, 903.0_dp, 903.0_dp, & + 875.8_dp, 821.5_dp, 767.2_dp, 689.3_dp, 588.0_dp, 532.7_dp, 473.3_dp, 410.0_dp, & + 346.7_dp, 277.0_dp, 201.0_dp, 201.0_dp, 277.7_dp, 349.5_dp, 416.5_dp, 483.5_dp, & + 546.7_dp, 606.0_dp, 759.3_dp, 880.7_dp, 970.0_dp, 1059.3_dp, 1104.0_dp, 1104.0_dp, & + 1104.0_dp, 1080.3_dp, 1033.0_dp, 985.7_dp, 918.3_dp, 831.0_dp, 774.0_dp, 264.0_dp, & + 774.0_dp, 721.0_dp, 975.0_dp, 975.0_dp, 1188.0_dp, 1188.0_dp, 975.0_dp, 975.0_dp, & + 774.0_dp, 774.0_dp, 100.0_dp, 100.0_dp, 221.0_dp, 1014.0_dp, 1014.0_dp, 406.0_dp, & + 406.0_dp, 435.3_dp, 464.7_dp, 494.0_dp, 523.3_dp, 552.7_dp, 582.0_dp, 748.7_dp, & + 880.7_dp, 978.0_dp, 1075.3_dp, 1124.0_dp, 1124.0_dp, 1124.0_dp, 1074.0_dp, 974.0_dp, & + 874.0_dp, 733.0_dp, 551.0_dp, 488.3_dp, 424.5_dp, 359.5_dp, 294.5_dp, 227.3_dp, & + 158.0_dp, 158.0_dp, 218.0_dp, 280.0_dp, 344.0_dp, 408.0_dp, 475.7_dp, 547.0_dp, & + 662.3_dp, 753.7_dp, 821.0_dp, 888.3_dp, 922.0_dp, 922.0_dp, 922.0_dp, 888.3_dp, & + 821.0_dp, 753.7_dp, 662.3_dp, 547.0_dp, 493.0_dp, 439.2_dp, 385.5_dp, 331.8_dp, & + 277.0_dp, 221.0_dp, 676.0_dp, 585.3_dp, 513.5_dp, 460.5_dp, 407.5_dp, 381.0_dp, & + 381.0_dp, 381.0_dp, 407.5_dp, 460.5_dp, 513.5_dp, 585.3_dp, 676.0_dp, 766.7_dp, & + 838.5_dp, 891.5_dp, 944.5_dp, 971.0_dp, 971.0_dp, 971.0_dp, 944.5_dp, 891.5_dp, & + 838.5_dp, 766.7_dp, 676.0_dp, 1077.0_dp, 1077.0_dp, 1026.3_dp, 975.2_dp, 923.5_dp, & + 871.8_dp, 820.7_dp, 770.0_dp, 636.7_dp, 534.8_dp, 464.5_dp, 394.2_dp, 354.0_dp, & + 344.0_dp, 383.3_dp, 432.7_dp, 492.0_dp, 551.3_dp, 616.7_dp, 688.0_dp, 838.0_dp, & + 956.5_dp, 1043.5_dp, 1130.5_dp, 1174.0_dp, 1174.0_dp, 1174.0_dp, 1128.7_dp, 1038.0_dp, & + 947.3_dp, 826.7_dp, 676.0_dp, 503.3_dp, 371.3_dp, 280.0_dp, 188.7_dp, 143.0_dp, & + 143.0_dp, 143.0_dp, 199.0_dp, 311.0_dp, 423.0_dp, 573.3_dp, 762.0_dp, 812.7_dp, & + 863.8_dp, 915.5_dp, 967.2_dp, 1021.0_dp, 1077.0_dp, 168.0_dp, 1128.0_dp, 1128.0_dp, & + 586.0_dp, 375.0_dp, 885.0_dp, 168.0_dp, 651.0_dp, 555.0_dp, 479.5_dp, 424.5_dp, & + 369.5_dp, 342.0_dp, 342.0_dp, 342.0_dp, 369.5_dp, 424.5_dp, 479.5_dp, 555.0_dp, & + 651.0_dp, 747.0_dp, 822.7_dp, 878.0_dp, 933.3_dp, 961.0_dp, 961.0_dp, 961.0_dp, & + 933.5_dp, 878.5_dp, 823.5_dp, 747.7_dp, 651.0_dp, 449.0_dp, 362.3_dp, 294.8_dp, & + 246.5_dp, 198.2_dp, 174.0_dp, 174.0_dp, 174.0_dp, 216.5_dp, 301.5_dp, 386.5_dp, & + 503.0_dp, 651.0_dp, 799.7_dp, 916.3_dp, 1001.0_dp, 1085.7_dp, 1128.0_dp, 1128.0_dp, & + 1128.0_dp, 1103.8_dp, 1055.5_dp, 1007.2_dp, 940.0_dp, 854.0_dp, 951.3_dp, 1027.2_dp, & + 1081.5_dp, 1135.8_dp, 1163.0_dp, 1163.0_dp, 1163.0_dp, 1118.8_dp, 1030.5_dp, 942.2_dp, & + 815.7_dp, 651.0_dp, 486.3_dp, 359.8_dp, 271.5_dp, 183.2_dp, 139.0_dp, 139.0_dp, & + 139.0_dp, 166.3_dp, 221.0_dp, 275.7_dp, 351.7_dp, 449.0_dp, 375.0_dp, 375.0_dp, & + 399.2_dp, 447.5_dp, 495.8_dp, 563.7_dp, 651.0_dp, 737.7_dp, 805.5_dp, 854.5_dp, & + 903.5_dp, 928.0_dp, 928.0_dp, 928.0_dp, 903.5_dp, 854.5_dp, 805.5_dp, 737.7_dp, & + 651.0_dp, 563.7_dp, 495.8_dp, 447.5_dp, 399.2_dp, 375.0_dp, 375.0_dp, 225.0_dp, & + 225.0_dp, 275.7_dp, 327.0_dp, 379.0_dp, 431.0_dp, 482.0_dp, 532.0_dp, 665.3_dp, & + 767.2_dp, 837.5_dp, 907.8_dp, 948.0_dp, 958.0_dp, 919.3_dp, 870.3_dp, 811.0_dp, & + 751.7_dp, 686.0_dp, 614.0_dp, 464.7_dp, 346.5_dp, 259.5_dp, 172.5_dp, 129.0_dp, & + 129.0_dp, 129.0_dp, 174.3_dp, 265.0_dp, 355.7_dp, 476.3_dp, 627.0_dp, 799.7_dp, & + 931.5_dp, 1022.5_dp, 1113.5_dp, 1159.0_dp, 1159.0_dp, 1159.0_dp, 1103.2_dp, 991.5_dp, & + 879.8_dp, 729.7_dp, 541.0_dp, 490.3_dp, 439.0_dp, 387.0_dp, 335.0_dp, 281.0_dp, & + 225.0_dp, 627.0_dp, 717.7_dp, 789.5_dp, 842.5_dp, 895.5_dp, 922.0_dp, 922.0_dp, & + 922.0_dp, 895.5_dp, 842.5_dp, 789.5_dp, 717.7_dp, 627.0_dp, 536.3_dp, 464.5_dp, & + 411.5_dp, 358.5_dp, 332.0_dp, 332.0_dp, 332.0_dp, 358.5_dp, 411.5_dp, 464.5_dp, & + 536.3_dp, 627.0_dp, 240.0_dp, 451.0_dp, 451.0_dp, 240.0_dp, 240.0_dp, 451.0_dp, & + 451.0_dp, 240.0_dp, 240.0_dp, 451.0_dp, 451.0_dp, 240.0_dp, 240.0_dp, 451.0_dp, & + 451.0_dp, 287.0_dp, 158.0_dp, 240.0_dp, 1499.0_dp, 467.0_dp, 1499.0_dp, 1499.0_dp, & + 217.0_dp, 217.0_dp, 1499.0_dp, 217.0_dp, 1499.0_dp, 1499.0_dp, 217.0_dp, 217.0_dp, & + 1499.0_dp, 1499.0_dp, 217.0_dp, 217.0_dp, 217.0_dp, 1499.0_dp, 1499.0_dp, 217.0_dp, & + 217.0_dp, 1247.0_dp, 391.0_dp, 594.0_dp, 594.0_dp, 391.0_dp, 588.0_dp, 397.0_dp, & + 397.0_dp, 397.0_dp, 406.3_dp, 425.0_dp, 443.7_dp, 483.0_dp, 543.0_dp, 633.0_dp, & + 671.0_dp, 698.5_dp, 715.5_dp, 732.5_dp, 741.0_dp, 741.0_dp, 741.0_dp, 719.2_dp, & + 675.5_dp, 631.8_dp, 574.0_dp, 502.0_dp, 449.3_dp, 393.2_dp, 333.5_dp, 273.8_dp, & + 211.7_dp, 147.0_dp, 147.0_dp, 209.7_dp, 273.2_dp, 337.5_dp, 401.8_dp, 468.3_dp, & + 537.0_dp, 659.7_dp, 758.2_dp, 832.5_dp, 906.8_dp, 944.0_dp, 944.0_dp, 944.0_dp, & + 932.0_dp, 908.0_dp, 884.0_dp, 842.0_dp, 782.0_dp, 694.0_dp, 662.7_dp, 640.5_dp, & + 627.5_dp, 614.5_dp, 605.3_dp, 600.0_dp, 596.0_dp, 593.0_dp, 591.0_dp, 589.0_dp, & + 588.0_dp, 588.0_dp, 762.0_dp, 762.0_dp, 785.7_dp, 833.0_dp, 880.3_dp, 945.3_dp, & + 1028.0_dp, 1110.0_dp, 1174.5_dp, 1221.5_dp, 1268.5_dp, 1292.0_dp, 1292.0_dp, 1292.0_dp, & + 1268.0_dp, 1220.0_dp, 1172.0_dp, 1107.3_dp, 1026.0_dp, 945.3_dp, 881.2_dp, 833.5_dp, & + 785.8_dp, 762.0_dp, 762.0_dp, 1307.0_dp, 1267.0_dp, 1221.2_dp, 1169.5_dp, 1117.8_dp, & + 1057.7_dp, 989.0_dp, 874.3_dp, 781.2_dp, 709.5_dp, 637.8_dp, 602.0_dp, 602.0_dp, & + 602.0_dp, 638.0_dp, 710.0_dp, 782.0_dp, 875.0_dp, 989.0_dp, 1057.7_dp, 1118.0_dp, & + 1170.0_dp, 1222.0_dp, 1267.7_dp, 1307.0_dp, 1307.0_dp, 1450.0_dp, 1450.0_dp, 1547.3_dp, & + 1623.5_dp, 1678.5_dp, 1733.5_dp, 1761.0_dp, 1761.0_dp, 1761.0_dp, 1750.5_dp, 1729.5_dp, & + 1708.5_dp, 1676.7_dp, 1634.0_dp, 1564.7_dp, 1480.2_dp, 1380.5_dp, 1280.8_dp, 1172.3_dp, & + 1055.0_dp, 973.0_dp, 894.3_dp, 819.0_dp, 743.7_dp, 674.0_dp, 610.0_dp, 505.3_dp, & + 423.5_dp, 364.5_dp, 305.5_dp, 276.0_dp, 276.0_dp, 276.0_dp, 295.2_dp, 333.5_dp, & + 371.8_dp, 427.3_dp, 500.0_dp, 570.0_dp, 651.0_dp, 743.0_dp, 835.0_dp, 933.3_dp, & + 1038.0_dp, 1124.0_dp, 1208.5_dp, 1291.5_dp, 1374.5_dp, 1450.7_dp, 1520.0_dp, 1610.0_dp, & + 1526.7_dp, 1435.8_dp, 1337.5_dp, 1239.2_dp, 1139.3_dp, 1038.0_dp, 914.7_dp, 798.3_dp, & + 689.0_dp, 579.7_dp, 482.3_dp, 397.0_dp, 311.7_dp, 246.7_dp, 202.0_dp, 157.3_dp, & + 135.0_dp, 135.0_dp, 135.0_dp, 157.7_dp, 203.0_dp, 248.3_dp, 313.0_dp, 397.0_dp, & + 483.0_dp, 582.3_dp, 695.0_dp, 807.7_dp, 927.0_dp, 1053.0_dp, 1194.3_dp, 1325.5_dp, & + 1446.5_dp, 1567.5_dp, 1669.0_dp, 1751.0_dp, 1801.0_dp, 1839.2_dp, 1865.5_dp, 1891.8_dp, & + 1905.0_dp, 1905.0_dp, 1905.0_dp, 1852.0_dp, 1746.0_dp, 1640.0_dp, 1493.7_dp, 1307.0_dp, & + 700.0_dp, 426.0_dp, 975.0_dp, 586.0_dp, 815.0_dp, 1384.0_dp, 1174.0_dp, 1038.0_dp, & + 365.0_dp, 229.0_dp, 16.0_dp, 403.0_dp, 403.0_dp, 727.0_dp, 835.7_dp, 916.2_dp, & + 968.5_dp, 1020.8_dp, 1047.0_dp, 1047.0_dp, 1047.0_dp, 1020.8_dp, 968.5_dp, 916.2_dp, & + 835.7_dp, 727.0_dp, 403.0_dp, 403.0_dp, 702.0_dp, 800.7_dp, 874.2_dp, 922.5_dp, & + 970.8_dp, 995.0_dp, 995.0_dp, 995.0_dp, 970.8_dp, 922.5_dp, 874.2_dp, 800.7_dp, & + 702.0_dp, 201.0_dp, 717.0_dp, 871.0_dp, 989.7_dp, 1073.0_dp, 1156.3_dp, 1198.0_dp, & + 1198.0_dp, 1198.0_dp, 1176.7_dp, 1134.0_dp, 1091.3_dp, 1028.7_dp, 946.0_dp, 1045.3_dp, & + 1122.5_dp, 1177.5_dp, 1232.5_dp, 1260.0_dp, 1260.0_dp, 1260.0_dp, 1214.7_dp, 1124.0_dp, & + 1033.3_dp, 904.3_dp, 737.0_dp, 201.0_dp, 1319.0_dp, 1319.0_dp, 1251.0_dp, 1178.5_dp, & + 1101.5_dp, 1024.5_dp, 942.7_dp, 856.0_dp, 685.3_dp, 554.7_dp, 464.0_dp, 373.3_dp, & + 328.0_dp, 328.0_dp, 328.0_dp, 373.3_dp, 464.0_dp, 554.7_dp, 685.3_dp, 856.0_dp, & + 942.7_dp, 1024.5_dp, 1101.5_dp, 1178.5_dp, 1251.0_dp, 1319.0_dp, 1319.0_dp, 1248.3_dp, & + 1173.5_dp, 1094.5_dp, 1015.5_dp, 932.0_dp, 844.0_dp, 618.0_dp, 440.0_dp, 310.0_dp, & + 180.0_dp, 115.0_dp, 115.0_dp, 115.0_dp, 180.0_dp, 310.0_dp, 440.0_dp, 618.0_dp, & + 844.0_dp, 933.3_dp, 1017.5_dp, 1096.5_dp, 1175.5_dp, 1249.7_dp, 1319.0_dp, 403.0_dp, & + 403.0_dp, 647.0_dp, 853.0_dp, 1003.8_dp, 1099.5_dp, 1195.2_dp, 1243.0_dp, 1243.0_dp, & + 1243.0_dp, 1195.2_dp, 1099.5_dp, 1003.8_dp, 853.0_dp, 647.0_dp, 201.0_dp, 616.0_dp, & + 905.3_dp, 1117.7_dp, 1253.0_dp, 1388.3_dp, 1456.0_dp, 1456.0_dp, 1456.0_dp, 1388.0_dp, & + 1252.0_dp, 1116.0_dp, 904.0_dp, 616.0_dp, 201.0_dp, 201.0_dp, 1145.0_dp, 1145.0_dp, & + 403.0_dp, 403.0_dp, 1114.0_dp, 1114.0_dp, 403.0_dp, 403.0_dp, 1163.0_dp, 1163.0_dp, & + 201.0_dp, 201.0_dp, 1059.0_dp, 1059.0_dp, 403.0_dp, 403.0_dp, 995.0_dp, 995.0_dp, & + 403.0_dp, 403.0_dp, 201.0_dp, 1219.0_dp, 1219.0_dp, 889.0_dp, 889.0_dp, 1419.0_dp, & + 1419.0_dp, 1341.0_dp, 1255.0_dp, 1161.0_dp, 1067.0_dp, 966.7_dp, 860.0_dp, 626.7_dp, & + 444.2_dp, 312.5_dp, 180.8_dp, 115.0_dp, 115.0_dp, 115.0_dp, 180.8_dp, 312.5_dp, & + 444.2_dp, 626.7_dp, 860.0_dp, 957.3_dp, 1049.8_dp, 1137.5_dp, 1225.2_dp, 1306.0_dp, & + 1380.0_dp, 1380.0_dp, 1305.3_dp, 1226.0_dp, 1142.0_dp, 1058.0_dp, 969.7_dp, 877.0_dp, & + 694.3_dp, 557.2_dp, 465.5_dp, 373.8_dp, 328.0_dp, 328.0_dp, 328.0_dp, 373.8_dp, & + 465.5_dp, 557.2_dp, 694.3_dp, 877.0_dp, 948.3_dp, 1012.0_dp, 1068.0_dp, 1124.0_dp, & + 1174.3_dp, 1219.0_dp, 201.0_dp, 403.0_dp, 403.0_dp, 1137.0_dp, 1137.0_dp, 1339.0_dp, & + 1339.0_dp, 1137.0_dp, 1137.0_dp, 403.0_dp, 403.0_dp, 201.0_dp, 201.0_dp, 403.0_dp, & + 403.0_dp, 201.0_dp, 201.0_dp, 403.0_dp, 403.0_dp, 403.0_dp, 368.8_dp, 300.5_dp, & + 232.2_dp, 122.3_dp, -29.0_dp, -106.0_dp, -106.0_dp, -43.0_dp, 46.3_dp, 109.3_dp, & + 146.0_dp, 182.7_dp, 201.0_dp, 201.0_dp, 201.0_dp, 403.0_dp, 403.0_dp, 1073.0_dp, & + 1333.0_dp, 592.0_dp, 1386.0_dp, 1120.0_dp, 403.0_dp, 403.0_dp, 201.0_dp, 201.0_dp, & + 403.0_dp, 403.0_dp, 1130.0_dp, 1130.0_dp, 201.0_dp, 201.0_dp, 502.0_dp, 883.0_dp, & + 1266.0_dp, 1567.0_dp, 1567.0_dp, 1370.0_dp, 1370.0_dp, 985.0_dp, 782.0_dp, 397.0_dp, & + 397.0_dp, 201.0_dp, 201.0_dp, 473.0_dp, 1135.0_dp, 1135.0_dp, 1331.0_dp, 1331.0_dp, & + 1059.0_dp, 397.0_dp, 397.0_dp, 201.0_dp, 807.0_dp, 660.3_dp, 543.8_dp, 457.5_dp, & + 371.2_dp, 328.0_dp, 328.0_dp, 328.0_dp, 371.2_dp, 457.5_dp, 543.8_dp, 660.3_dp, & + 807.0_dp, 953.7_dp, 1069.8_dp, 1155.5_dp, 1241.2_dp, 1284.0_dp, 1284.0_dp, 1284.0_dp, & + 1241.2_dp, 1155.5_dp, 1069.8_dp, 953.7_dp, 807.0_dp, 807.0_dp, 1016.3_dp, 1183.7_dp, & + 1309.0_dp, 1434.3_dp, 1497.0_dp, 1497.0_dp, 1497.0_dp, 1434.3_dp, 1309.0_dp, 1183.7_dp, & + 1016.3_dp, 807.0_dp, 597.0_dp, 429.2_dp, 303.5_dp, 177.8_dp, 115.0_dp, 115.0_dp, & + 115.0_dp, 177.8_dp, 303.5_dp, 429.2_dp, 597.0_dp, 807.0_dp, 403.0_dp, 403.0_dp, & + 657.0_dp, 751.0_dp, 823.7_dp, 875.0_dp, 926.3_dp, 952.0_dp, 952.0_dp, 952.0_dp, & + 926.3_dp, 875.0_dp, 823.7_dp, 751.0_dp, 657.0_dp, 201.0_dp, 657.0_dp, 824.3_dp, & + 950.8_dp, 1036.5_dp, 1122.2_dp, 1165.0_dp, 1165.0_dp, 1165.0_dp, 1122.2_dp, 1036.5_dp, & + 950.8_dp, 824.3_dp, 657.0_dp, 403.0_dp, 403.0_dp, 201.0_dp, 807.0_dp, 660.3_dp, & + 543.8_dp, 457.5_dp, 371.2_dp, 328.0_dp, 328.0_dp, 328.0_dp, 371.2_dp, 457.5_dp, & + 543.8_dp, 660.3_dp, 807.0_dp, 953.7_dp, 1069.8_dp, 1155.5_dp, 1241.2_dp, 1284.0_dp, & + 1284.0_dp, 1284.0_dp, 1241.2_dp, 1155.5_dp, 1069.8_dp, 953.7_dp, 807.0_dp, 1090.0_dp, & + 1356.0_dp, 1112.0_dp, 891.0_dp, 869.0_dp, 852.2_dp, 840.5_dp, 828.8_dp, 817.7_dp, & + 807.0_dp, 597.0_dp, 429.2_dp, 303.5_dp, 177.8_dp, 115.0_dp, 115.0_dp, 115.0_dp, & + 177.8_dp, 303.5_dp, 429.2_dp, 597.0_dp, 807.0_dp, 1016.3_dp, 1183.7_dp, 1309.0_dp, & + 1434.3_dp, 1497.0_dp, 1497.0_dp, 1497.0_dp, 1462.2_dp, 1392.5_dp, 1322.8_dp, 1222.0_dp, & + 1090.0_dp, 909.0_dp, 952.3_dp, 994.5_dp, 1035.5_dp, 1076.5_dp, 1117.7_dp, 1159.0_dp, & + 1364.0_dp, 1147.0_dp, 956.0_dp, 906.7_dp, 858.8_dp, 812.5_dp, 766.2_dp, 703.0_dp, & + 623.0_dp, 403.0_dp, 403.0_dp, 201.0_dp, 201.0_dp, 657.0_dp, 827.7_dp, 955.0_dp, & + 1039.0_dp, 1123.0_dp, 1165.0_dp, 1165.0_dp, 1165.0_dp, 1143.2_dp, 1099.5_dp, 1055.8_dp, & + 992.3_dp, 909.0_dp, 403.0_dp, 403.0_dp, 657.0_dp, 754.3_dp, 827.8_dp, 877.5_dp, & + 927.2_dp, 952.0_dp, 952.0_dp, 952.0_dp, 927.2_dp, 877.5_dp, 827.8_dp, 754.3_dp, & + 657.0_dp, 1096.0_dp, 1096.0_dp, 1019.3_dp, 947.0_dp, 879.0_dp, 811.0_dp, 745.3_dp, & + 682.0_dp, 572.0_dp, 487.2_dp, 427.5_dp, 367.8_dp, 338.0_dp, 338.0_dp, 338.0_dp, & + 357.8_dp, 397.5_dp, 437.2_dp, 512.3_dp, 623.0_dp, 745.0_dp, 895.7_dp, 1006.8_dp, & + 1078.5_dp, 1150.2_dp, 1186.0_dp, 1186.0_dp, 1186.0_dp, 1137.5_dp, 1040.5_dp, 943.5_dp, & + 801.3_dp, 614.0_dp, 543.3_dp, 468.2_dp, 388.5_dp, 308.8_dp, 226.3_dp, 141.0_dp, & + 141.0_dp, 223.0_dp, 303.3_dp, 382.0_dp, 460.7_dp, 538.0_dp, 614.0_dp, 729.3_dp, & + 818.3_dp, 881.0_dp, 943.7_dp, 975.0_dp, 975.0_dp, 975.0_dp, 952.5_dp, 907.5_dp, & + 862.5_dp, 788.7_dp, 686.0_dp, 563.0_dp, 412.3_dp, 303.3_dp, 236.0_dp, 168.7_dp, & + 135.0_dp, 135.0_dp, 135.0_dp, 181.5_dp, 274.5_dp, 367.5_dp, 495.7_dp, 659.0_dp, & + 729.0_dp, 800.3_dp, 873.0_dp, 945.7_dp, 1020.0_dp, 1096.0_dp, -6.0_dp, 1257.0_dp, & + 1257.0_dp, 727.0_dp, 727.0_dp, 524.0_dp, 524.0_dp, -6.0_dp, 178.0_dp, 381.0_dp, & + 381.0_dp, 381.0_dp, 410.0_dp, 468.0_dp, 526.0_dp, 620.0_dp, 750.0_dp, 879.3_dp, & + 973.0_dp, 1031.0_dp, 1089.0_dp, 1118.0_dp, 1118.0_dp, 1118.0_dp, 1321.0_dp, 1321.0_dp, & + 1321.0_dp, 1272.8_dp, 1176.5_dp, 1080.2_dp, 938.0_dp, 750.0_dp, 561.3_dp, 418.8_dp, & + 322.5_dp, 226.2_dp, 178.0_dp, 178.0_dp, 586.0_dp, 16.0_dp, 227.0_dp, 700.0_dp, & + 1174.0_dp, 1384.0_dp, 815.0_dp, 68.0_dp, 272.0_dp, 586.0_dp, 899.0_dp, 1126.0_dp, & + 1440.0_dp, 1753.0_dp, 1958.0_dp, 1583.0_dp, 1329.0_dp, 1014.0_dp, 696.0_dp, 442.0_dp, & + 129.0_dp, 346.0_dp, 717.0_dp, 1090.0_dp, 1307.0_dp, 827.0_dp, 1339.0_dp, 1122.0_dp, & + 702.0_dp, 279.0_dp, 61.0_dp, 594.0_dp, -4.0_dp, 213.0_dp, 627.0_dp, 1038.0_dp, & + 1255.0_dp, 727.0_dp, 727.0_dp, 524.0_dp, 524.0_dp, 115.0_dp, 1288.0_dp, 1288.0_dp, & + 344.0_dp, 1311.0_dp, 1311.0_dp, 92.0_dp, 92.0_dp, 1036.0_dp, 115.0_dp, 176.0_dp, & + 600.0_dp, 600.0_dp, 360.0_dp, 360.0_dp, 600.0_dp, 600.0_dp, 176.0_dp, 170.0_dp, & + 690.0_dp, 520.0_dp, 0.0_dp, 623.0_dp, 623.0_dp, 199.0_dp, 199.0_dp, 438.0_dp, & + 438.0_dp, 199.0_dp, 199.0_dp, 956.0_dp, 1499.0_dp, 1298.0_dp, 858.0_dp, 418.0_dp, & + 217.0_dp, 760.0_dp, 1044.0_dp, 1044.0_dp, -20.0_dp, -20.0_dp, 367.0_dp, 649.0_dp, & + 496.0_dp, 170.0_dp, 702.0_dp, 553.3_dp, 450.3_dp, 393.0_dp, 335.7_dp, 307.0_dp, & + 307.0_dp, 307.0_dp, 328.5_dp, 371.5_dp, 414.5_dp, 473.0_dp, 547.0_dp, 649.0_dp, & + 730.8_dp, 792.5_dp, 854.2_dp, 885.0_dp, 885.0_dp, 885.0_dp, 1069.0_dp, 1069.0_dp, & + 885.0_dp, 885.0_dp, 843.0_dp, 790.7_dp, 728.0_dp, 665.3_dp, 588.7_dp, 498.0_dp, & + 383.3_dp, 292.2_dp, 224.5_dp, 156.8_dp, 123.0_dp, 123.0_dp, 123.0_dp, 165.2_dp, & + 249.5_dp, 333.8_dp, 459.7_dp, 627.0_dp, 885.0_dp, 885.0_dp, 885.0_dp, 857.2_dp, & + 801.5_dp, 745.8_dp, 667.7_dp, 567.0_dp, 503.0_dp, 440.7_dp, 380.0_dp, 319.3_dp, & + 261.0_dp, 205.0_dp, 205.0_dp, 272.3_dp, 337.7_dp, 401.0_dp, 464.3_dp, 526.0_dp, & + 586.0_dp, 748.0_dp, 869.0_dp, 949.0_dp, 1029.0_dp, 1069.0_dp, 1069.0_dp, 997.0_dp, & + 997.0_dp, 969.2_dp, 913.5_dp, 857.8_dp, 781.3_dp, 684.0_dp, 586.7_dp, 510.2_dp, & + 454.5_dp, 398.8_dp, 371.0_dp, 371.0_dp, 371.0_dp, 398.8_dp, 454.5_dp, 510.2_dp, & + 586.7_dp, 684.0_dp, 781.3_dp, 857.8_dp, 913.5_dp, 969.2_dp, 997.0_dp, 997.0_dp, & + 371.0_dp, 409.7_dp, 458.5_dp, 517.5_dp, 576.5_dp, 647.0_dp, 729.0_dp, 865.0_dp, & + 975.5_dp, 1060.5_dp, 1145.5_dp, 1188.0_dp, 1188.0_dp, 1188.0_dp, 1145.5_dp, 1060.5_dp, & + 975.5_dp, 865.0_dp, 729.0_dp, 647.0_dp, 576.5_dp, 517.5_dp, 458.5_dp, 409.7_dp, & + 371.0_dp, 371.0_dp, 186.0_dp, 186.0_dp, 371.0_dp, 999.0_dp, 999.0_dp, 947.0_dp, & + 894.8_dp, 842.5_dp, 790.2_dp, 737.3_dp, 684.0_dp, 564.7_dp, 472.0_dp, 406.0_dp, & + 340.0_dp, 307.0_dp, 307.0_dp, 307.0_dp, 340.0_dp, 406.0_dp, 472.0_dp, 564.7_dp, & + 684.0_dp, 737.3_dp, 790.2_dp, 842.5_dp, 894.8_dp, 947.0_dp, 999.0_dp, 999.0_dp, & + 947.7_dp, 894.5_dp, 839.5_dp, 784.5_dp, 726.0_dp, 664.0_dp, 495.3_dp, 361.3_dp, & + 262.0_dp, 162.7_dp, 113.0_dp, 113.0_dp, 113.0_dp, 163.2_dp, 263.5_dp, 363.8_dp, & + 501.3_dp, 676.0_dp, 732.7_dp, 788.0_dp, 842.0_dp, 896.0_dp, 948.3_dp, 999.0_dp, & + 930.0_dp, 930.0_dp, 1114.0_dp, 1114.0_dp, 930.0_dp, 930.0_dp, 891.3_dp, 842.5_dp, & + 783.5_dp, 724.5_dp, 653.7_dp, 571.0_dp, 435.7_dp, 325.5_dp, 240.5_dp, 155.5_dp, & + 113.0_dp, 113.0_dp, 113.0_dp, 155.5_dp, 240.5_dp, 325.5_dp, 435.7_dp, 571.0_dp, & + 653.7_dp, 724.5_dp, 783.5_dp, 842.5_dp, 891.3_dp, 930.0_dp, 303.0_dp, 303.0_dp, & + 330.8_dp, 386.5_dp, 442.2_dp, 518.7_dp, 616.0_dp, 713.3_dp, 790.0_dp, 846.0_dp, & + 902.0_dp, 930.0_dp, 930.0_dp, 930.0_dp, 902.0_dp, 846.0_dp, 790.0_dp, 713.3_dp, & + 616.0_dp, 518.7_dp, 442.2_dp, 386.5_dp, 330.8_dp, 303.0_dp, 303.0_dp, 1151.0_dp, & + 1151.0_dp, 305.0_dp, 313.0_dp, 351.2_dp, 419.5_dp, 487.8_dp, 583.0_dp, 705.0_dp, & + 775.7_dp, 844.2_dp, 910.5_dp, 976.8_dp, 1042.7_dp, 1108.0_dp, 1108.0_dp, 1042.0_dp, & + 974.3_dp, 905.0_dp, 835.7_dp, 765.3_dp, 694.0_dp, 515.3_dp, 373.8_dp, 269.5_dp, & + 165.2_dp, 113.0_dp, 113.0_dp, 113.0_dp, 162.5_dp, 261.5_dp, 360.5_dp, 494.0_dp, & + 662.0_dp, 812.7_dp, 931.8_dp, 1019.5_dp, 1107.2_dp, 1151.0_dp, 1151.0_dp, 967.0_dp, & + 965.7_dp, 937.5_dp, 882.5_dp, 827.5_dp, 754.7_dp, 664.0_dp, 561.3_dp, 479.2_dp, & + 417.5_dp, 355.8_dp, 320.3_dp, 311.0_dp, 760.0_dp, 760.0_dp, 584.0_dp, 518.0_dp, & + 472.2_dp, 446.5_dp, 420.8_dp, 408.0_dp, 408.0_dp, 408.0_dp, 711.0_dp, 711.0_dp, & + 408.0_dp, 408.0_dp, 223.0_dp, 223.0_dp, 47.0_dp, 47.0_dp, 223.0_dp, 223.0_dp, & + 223.0_dp, 252.0_dp, 310.0_dp, 368.0_dp, 460.0_dp, 586.0_dp, 930.0_dp, 930.0_dp, & + 902.5_dp, 847.5_dp, 792.5_dp, 715.3_dp, 616.0_dp, 517.3_dp, 440.5_dp, 385.5_dp, & + 330.5_dp, 303.0_dp, 303.0_dp, 303.0_dp, 330.5_dp, 385.5_dp, 440.5_dp, 517.3_dp, & + 616.0_dp, 715.3_dp, 792.5_dp, 847.5_dp, 902.5_dp, 930.0_dp, 930.0_dp, 1114.0_dp, & + 1114.0_dp, 1071.7_dp, 987.0_dp, 902.3_dp, 772.7_dp, 598.0_dp, 533.3_dp, 472.3_dp, & + 415.0_dp, 357.7_dp, 302.0_dp, 248.0_dp, 248.0_dp, 302.0_dp, 355.3_dp, 408.0_dp, & + 460.7_dp, 514.3_dp, 569.0_dp, 689.7_dp, 780.0_dp, 840.0_dp, 900.0_dp, 930.0_dp, & + 930.0_dp, 930.0_dp, 892.0_dp, 843.3_dp, 784.0_dp, 724.7_dp, 653.7_dp, 571.0_dp, & + 433.7_dp, 323.0_dp, 239.0_dp, 155.0_dp, 113.0_dp, 113.0_dp, 113.0_dp, 155.0_dp, & + 239.0_dp, 323.0_dp, 433.7_dp, 571.0_dp, 653.7_dp, 724.7_dp, 784.0_dp, 843.3_dp, & + 892.0_dp, 930.0_dp, 930.0_dp, 1114.0_dp, 1124.0_dp, 1124.0_dp, 940.0_dp, 940.0_dp, & + 940.0_dp, 919.3_dp, 878.0_dp, 836.7_dp, 774.7_dp, 692.0_dp, 592.7_dp, 514.3_dp, & + 457.0_dp, 399.7_dp, 371.0_dp, 371.0_dp, 371.0_dp, 186.0_dp, 186.0_dp, 371.0_dp, & + 371.0_dp, 415.0_dp, 466.8_dp, 526.5_dp, 586.2_dp, 655.0_dp, 733.0_dp, 861.7_dp, & + 959.0_dp, 1025.0_dp, 1091.0_dp, 1124.0_dp, 1124.0_dp, 193.0_dp, 377.0_dp, 377.0_dp, & + 193.0_dp, 193.0_dp, 377.0_dp, 377.0_dp, 193.0_dp, 193.0_dp, 377.0_dp, 377.0_dp, & + 377.0_dp, 349.8_dp, 295.5_dp, 241.2_dp, 153.7_dp, 33.0_dp, -37.0_dp, -37.0_dp, & + 12.0_dp, 82.0_dp, 129.7_dp, 155.0_dp, 180.3_dp, 193.0_dp, 193.0_dp, 193.0_dp, & + 377.0_dp, 377.0_dp, 193.0_dp, 186.0_dp, 371.0_dp, 371.0_dp, 920.0_dp, 1155.0_dp, & + 561.0_dp, 1180.0_dp, 940.0_dp, 371.0_dp, 371.0_dp, 186.0_dp, 193.0_dp, 377.0_dp, & + 377.0_dp, 193.0_dp, 1065.0_dp, 1111.0_dp, 1166.0_dp, 1230.0_dp, 1294.0_dp, 1369.3_dp, & + 1456.0_dp, 1572.7_dp, 1662.7_dp, 1726.0_dp, 1789.3_dp, 1821.0_dp, 1821.0_dp, 1821.0_dp, & + 1636.0_dp, 1636.0_dp, 1636.0_dp, 1617.0_dp, 1579.0_dp, 1541.0_dp, 1483.0_dp, 1405.0_dp, & + 1309.7_dp, 1234.3_dp, 1179.0_dp, 1123.7_dp, 1096.0_dp, 1096.0_dp, 1096.0_dp, 911.0_dp, & + 911.0_dp, 911.0_dp, 892.0_dp, 854.0_dp, 816.0_dp, 757.3_dp, 678.0_dp, 584.0_dp, & + 509.3_dp, 454.0_dp, 398.7_dp, 371.0_dp, 371.0_dp, 371.0_dp, 186.0_dp, 186.0_dp, & + 371.0_dp, 371.0_dp, 413.0_dp, 463.3_dp, 522.0_dp, 580.7_dp, 650.3_dp, 731.0_dp, & + 812.3_dp, 881.5_dp, 938.5_dp, 995.5_dp, 1037.7_dp, 1065.0_dp, 1124.0_dp, 1124.0_dp, & + 940.0_dp, 940.0_dp, 940.0_dp, 919.3_dp, 878.0_dp, 836.7_dp, 774.7_dp, 692.0_dp, & + 592.7_dp, 514.3_dp, 457.0_dp, 399.7_dp, 371.0_dp, 371.0_dp, 371.0_dp, 186.0_dp, & + 186.0_dp, 371.0_dp, 371.0_dp, 415.0_dp, 466.8_dp, 526.5_dp, 586.2_dp, 655.0_dp, & + 733.0_dp, 861.7_dp, 959.0_dp, 1025.0_dp, 1091.0_dp, 1124.0_dp, 1124.0_dp, 627.0_dp, & + 528.3_dp, 450.3_dp, 393.0_dp, 335.7_dp, 307.0_dp, 307.0_dp, 307.0_dp, 335.5_dp, & + 392.5_dp, 449.5_dp, 527.7_dp, 627.0_dp, 725.0_dp, 802.7_dp, 860.0_dp, 917.3_dp, & + 946.0_dp, 946.0_dp, 946.0_dp, 917.3_dp, 860.0_dp, 802.7_dp, 725.0_dp, 627.0_dp, & + 627.0_dp, 787.0_dp, 912.7_dp, 1004.0_dp, 1095.3_dp, 1141.0_dp, 1141.0_dp, 1141.0_dp, & + 1095.3_dp, 1004.0_dp, 912.7_dp, 787.0_dp, 627.0_dp, 466.3_dp, 340.5_dp, 249.5_dp, & + 158.5_dp, 113.0_dp, 113.0_dp, 113.0_dp, 158.5_dp, 249.5_dp, 340.5_dp, 466.3_dp, & + 627.0_dp, 371.0_dp, 371.0_dp, 186.0_dp, 186.0_dp, 371.0_dp, 371.0_dp, 409.7_dp, & + 458.5_dp, 517.5_dp, 576.5_dp, 647.0_dp, 729.0_dp, 865.0_dp, 975.5_dp, 1060.5_dp, & + 1145.5_dp, 1188.0_dp, 1188.0_dp, 1188.0_dp, 1145.5_dp, 1060.5_dp, 975.5_dp, 865.0_dp, & + 729.0_dp, 647.0_dp, 576.5_dp, 517.5_dp, 458.5_dp, 409.7_dp, 371.0_dp, 997.0_dp, & + 997.0_dp, 969.2_dp, 913.5_dp, 857.8_dp, 781.3_dp, 684.0_dp, 586.7_dp, 510.2_dp, & + 454.5_dp, 398.8_dp, 371.0_dp, 371.0_dp, 371.0_dp, 398.8_dp, 454.5_dp, 510.2_dp, & + 586.7_dp, 684.0_dp, 781.3_dp, 857.8_dp, 913.5_dp, 969.2_dp, 997.0_dp, 997.0_dp, & + 303.0_dp, 303.0_dp, 330.8_dp, 386.5_dp, 442.2_dp, 518.7_dp, 616.0_dp, 713.3_dp, & + 790.0_dp, 846.0_dp, 902.0_dp, 930.0_dp, 930.0_dp, 930.0_dp, 902.0_dp, 846.0_dp, & + 790.0_dp, 713.3_dp, 616.0_dp, 518.7_dp, 442.2_dp, 386.5_dp, 330.8_dp, 303.0_dp, & + 303.0_dp, 930.0_dp, 891.3_dp, 842.5_dp, 783.5_dp, 724.5_dp, 653.7_dp, 571.0_dp, & + 435.7_dp, 325.5_dp, 240.5_dp, 155.5_dp, 113.0_dp, 113.0_dp, 113.0_dp, 155.5_dp, & + 240.5_dp, 325.5_dp, 435.7_dp, 571.0_dp, 653.7_dp, 724.5_dp, 783.5_dp, 842.5_dp, & + 891.3_dp, 930.0_dp, 930.0_dp, 1114.0_dp, 1114.0_dp, 930.0_dp, 842.0_dp, 821.3_dp, & + 798.8_dp, 774.5_dp, 750.2_dp, 723.3_dp, 694.0_dp, 590.0_dp, 510.2_dp, 454.5_dp, & + 398.8_dp, 371.0_dp, 371.0_dp, 371.0_dp, 186.0_dp, 186.0_dp, 371.0_dp, 371.0_dp, & + 409.7_dp, 460.0_dp, 522.0_dp, 584.0_dp, 659.3_dp, 748.0_dp, 760.7_dp, 774.7_dp, & + 790.0_dp, 805.3_dp, 822.3_dp, 841.0_dp, 907.0_dp, 907.0_dp, 855.0_dp, 801.0_dp, & + 745.0_dp, 689.0_dp, 631.0_dp, 571.0_dp, 479.7_dp, 411.2_dp, 365.5_dp, 319.8_dp, & + 297.0_dp, 297.0_dp, 297.0_dp, 313.3_dp, 346.0_dp, 378.7_dp, 444.3_dp, 543.0_dp, & + 606.0_dp, 736.7_dp, 829.5_dp, 884.5_dp, 939.5_dp, 967.0_dp, 967.0_dp, 967.0_dp, & + 925.8_dp, 843.5_dp, 761.2_dp, 648.0_dp, 504.0_dp, 444.0_dp, 381.5_dp, 316.5_dp, & + 251.5_dp, 183.0_dp, 111.0_dp, 111.0_dp, 179.0_dp, 246.0_dp, 312.0_dp, 378.0_dp, & + 443.3_dp, 508.0_dp, 594.7_dp, 661.3_dp, 708.0_dp, 754.7_dp, 778.0_dp, 778.0_dp, & + 778.0_dp, 761.2_dp, 727.5_dp, 693.8_dp, 620.0_dp, 506.0_dp, 442.0_dp, 328.0_dp, & + 245.7_dp, 195.0_dp, 144.3_dp, 119.0_dp, 119.0_dp, 119.0_dp, 156.3_dp, 231.0_dp, & + 305.7_dp, 411.7_dp, 549.0_dp, 617.0_dp, 681.0_dp, 741.0_dp, 801.0_dp, 856.3_dp, & + 907.0_dp, 375.0_dp, 375.0_dp, 754.0_dp, 754.0_dp, 375.0_dp, 375.0_dp, 375.0_dp, & + 387.5_dp, 412.5_dp, 437.5_dp, 488.3_dp, 565.0_dp, 754.0_dp, 754.0_dp, 565.0_dp, & + 423.0_dp, 325.0_dp, 271.0_dp, 217.0_dp, 190.0_dp, 190.0_dp, 190.0_dp, 55.0_dp, & + 55.0_dp, 190.0_dp, 190.0_dp, 174.0_dp, 174.0_dp, 358.0_dp, 358.0_dp, 358.0_dp, & + 378.7_dp, 420.0_dp, 461.3_dp, 523.3_dp, 606.0_dp, 705.3_dp, 783.8_dp, 841.5_dp, & + 899.2_dp, 928.0_dp, 928.0_dp, 928.0_dp, 1112.0_dp, 1112.0_dp, 928.0_dp, 928.0_dp, & + 883.3_dp, 831.5_dp, 772.5_dp, 713.5_dp, 645.0_dp, 567.0_dp, 438.3_dp, 340.7_dp, & + 274.0_dp, 207.3_dp, 174.0_dp, 174.0_dp, 637.0_dp, 61.0_dp, 256.0_dp, 606.0_dp, & + 956.0_dp, 1151.0_dp, 731.0_dp, 481.0_dp, 86.0_dp, 270.0_dp, 500.0_dp, 729.0_dp, & + 946.0_dp, 1176.0_dp, 1405.0_dp, 1589.0_dp, 1296.0_dp, 1079.0_dp, 838.0_dp, 596.0_dp, & + 379.0_dp, 1124.0_dp, 719.0_dp, 1145.0_dp, 928.0_dp, 602.0_dp, 276.0_dp, 59.0_dp, & + 494.0_dp, 96.0_dp, 313.0_dp, 610.0_dp, 907.0_dp, 659.0_dp, 607.0_dp, 556.3_dp, & + 507.0_dp, 457.7_dp, 391.7_dp, 309.0_dp, 162.0_dp, 162.0_dp, 270.0_dp, 320.7_dp, & + 360.0_dp, 388.0_dp, 416.0_dp, 447.0_dp, 481.0_dp, 514.0_dp, 61.0_dp, 256.0_dp, & + 606.0_dp, 956.0_dp, 1151.0_dp, 113.0_dp, 987.0_dp, 987.0_dp, 295.0_dp, 987.0_dp, & + 987.0_dp, 88.0_dp, 88.0_dp, 780.0_dp, 113.0_dp, 1047.0_dp, 1047.0_dp, 985.0_dp, & + 819.0_dp, 707.8_dp, 651.5_dp, 595.2_dp, 567.0_dp, 567.0_dp, 567.0_dp, 567.0_dp, & + 549.0_dp, 513.0_dp, 477.0_dp, 411.7_dp, 317.0_dp, 256.0_dp, 256.0_dp, 317.0_dp, & + 412.3_dp, 477.8_dp, 513.5_dp, 549.2_dp, 567.0_dp, 567.0_dp, 567.0_dp, 567.0_dp, & + 595.2_dp, 651.5_dp, 707.8_dp, 819.0_dp, 985.0_dp, 1047.0_dp, 1047.0_dp, 979.0_dp, & + 885.0_dp, 823.7_dp, 795.0_dp, 766.3_dp, 752.0_dp, 752.0_dp, 752.0_dp, 752.0_dp, & + 736.8_dp, 706.5_dp, 676.2_dp, 624.3_dp, 551.0_dp, 625.0_dp, 677.0_dp, 707.0_dp, & + 737.0_dp, 752.0_dp, 752.0_dp, 752.0_dp, 752.0_dp, 766.3_dp, 795.0_dp, 823.7_dp, & + 885.0_dp, 979.0_dp, 430.0_dp, 430.0_dp, 260.0_dp, 260.0_dp, 256.0_dp, 326.0_dp, & + 419.3_dp, 480.2_dp, 508.5_dp, 536.8_dp, 551.0_dp, 551.0_dp, 551.0_dp, 551.0_dp, & + 566.0_dp, 596.0_dp, 626.0_dp, 678.0_dp, 752.0_dp, 678.0_dp, 626.0_dp, 596.0_dp, & + 566.0_dp, 551.0_dp, 551.0_dp, 551.0_dp, 551.0_dp, 536.8_dp, 508.5_dp, 480.2_dp, & + 419.3_dp, 326.0_dp, 256.0_dp, 256.0_dp, 319.0_dp, 485.0_dp, 595.8_dp, 651.5_dp, & + 707.2_dp, 735.0_dp, 735.0_dp, 735.0_dp, 735.0_dp, 753.0_dp, 789.0_dp, 825.0_dp, & + 890.3_dp, 985.0_dp, 1047.0_dp, 1047.0_dp, 985.0_dp, 890.3_dp, 825.0_dp, 789.0_dp, & + 753.0_dp, 735.0_dp, 735.0_dp, 735.0_dp, 735.0_dp, 707.2_dp, 651.5_dp, 595.8_dp, & + 485.0_dp, 319.0_dp, 256.0_dp, 1499.0_dp, 1499.0_dp, 1429.0_dp, 1364.2_dp, 1304.5_dp, & + 1244.8_dp, 1182.7_dp, 1118.0_dp, 1044.7_dp, 959.3_dp, 862.0_dp, 854.7_dp, 849.3_dp, & + 846.0_dp, 841.3_dp, 834.0_dp, 824.0_dp, 720.7_dp, 637.7_dp, 575.0_dp, 516.3_dp, & + 458.3_dp, 401.0_dp, 343.7_dp, 282.3_dp, 217.0_dp, 217.0_dp, 287.0_dp, 351.8_dp, & + 411.5_dp, 471.2_dp, 533.3_dp, 598.0_dp, 671.3_dp, 757.0_dp, 855.0_dp, 861.7_dp, & + 866.7_dp, 870.0_dp, 875.3_dp, 882.7_dp, 892.0_dp, 995.3_dp, 1078.3_dp, 1141.0_dp, & + 1198.3_dp, 1255.2_dp, 1311.5_dp, 1367.8_dp, 1430.3_dp, 1499.0_dp & + ] + + real(dp), parameter :: PY(NP) = [ & + 254.0_dp, 254.0_dp, 0.0_dp, 0.0_dp, 1493.0_dp, 1493.0_dp, 838.0_dp, 481.0_dp, & + 481.0_dp, 838.0_dp, 1493.0_dp, 938.0_dp, 938.0_dp, 1493.0_dp, 1493.0_dp, 938.0_dp, & + 938.0_dp, 1493.0_dp, 901.0_dp, 901.0_dp, 567.0_dp, 567.0_dp, 1470.0_dp, 1055.0_dp, & + 1055.0_dp, 1470.0_dp, 1470.0_dp, 1055.0_dp, 1055.0_dp, 901.0_dp, 901.0_dp, 567.0_dp, & + 567.0_dp, 414.0_dp, 414.0_dp, 0.0_dp, 0.0_dp, 414.0_dp, 414.0_dp, 0.0_dp, & + 0.0_dp, 414.0_dp, 414.0_dp, 567.0_dp, 567.0_dp, 901.0_dp, 901.0_dp, 1055.0_dp, & + 1055.0_dp, 1470.0_dp, -301.0_dp, -301.0_dp, 0.0_dp, 1.3_dp, 9.5_dp, 24.5_dp, & + 39.5_dp, 62.0_dp, 92.0_dp, 272.0_dp, 229.3_dp, 197.2_dp, 175.5_dp, 153.8_dp, & + 142.7_dp, 142.0_dp, 598.0_dp, 622.0_dp, 662.7_dp, 720.0_dp, 777.3_dp, 856.0_dp, & + 956.0_dp, 1064.7_dp, 1150.3_dp, 1213.0_dp, 1275.7_dp, 1311.7_dp, 1321.0_dp, 1556.0_dp, & + 1556.0_dp, 1324.0_dp, 1321.3_dp, 1314.8_dp, 1304.5_dp, 1294.2_dp, 1280.0_dp, 1262.0_dp, & + 1087.0_dp, 1115.0_dp, 1136.7_dp, 1152.0_dp, 1167.3_dp, 1176.3_dp, 1179.0_dp, 752.0_dp, & + 728.7_dp, 687.0_dp, 627.0_dp, 567.0_dp, 485.0_dp, 381.0_dp, 268.3_dp, 179.5_dp, & + 114.5_dp, 49.5_dp, 12.0_dp, 2.0_dp, 770.0_dp, 1180.0_dp, 1171.3_dp, 1149.3_dp, & + 1114.0_dp, 1078.7_dp, 1031.7_dp, 973.0_dp, 915.7_dp, 871.0_dp, 839.0_dp, 807.0_dp, & + 784.0_dp, 770.0_dp, 578.0_dp, 145.0_dp, 156.3_dp, 180.3_dp, 217.0_dp, 253.7_dp, & + 302.0_dp, 362.0_dp, 420.7_dp, 467.3_dp, 502.0_dp, 536.7_dp, 562.0_dp, 578.0_dp, & + 657.0_dp, 657.0_dp, 632.3_dp, 583.0_dp, 533.7_dp, 465.0_dp, 377.0_dp, 290.3_dp, & + 222.2_dp, 172.5_dp, 122.8_dp, 98.0_dp, 98.0_dp, 98.0_dp, 122.8_dp, 172.5_dp, & + 222.2_dp, 290.3_dp, 377.0_dp, 464.3_dp, 532.8_dp, 582.5_dp, 632.2_dp, 657.0_dp, & + 657.0_dp, 784.0_dp, 784.0_dp, 747.3_dp, 674.0_dp, 600.7_dp, 501.7_dp, 377.0_dp, & + 252.3_dp, 153.5_dp, 80.5_dp, 7.5_dp, -29.0_dp, -29.0_dp, -29.0_dp, 7.5_dp, & + 80.5_dp, 153.5_dp, 252.3_dp, 377.0_dp, 502.3_dp, 601.5_dp, 674.5_dp, 747.5_dp, & + 784.0_dp, 784.0_dp, 1393.0_dp, 1393.0_dp, 1368.2_dp, 1318.5_dp, 1268.8_dp, 1200.7_dp, & + 1114.0_dp, 1026.0_dp, 957.3_dp, 908.0_dp, 858.7_dp, 834.0_dp, 834.0_dp, 834.0_dp, & + 858.7_dp, 908.0_dp, 957.3_dp, 1026.0_dp, 1114.0_dp, 1200.0_dp, 1268.0_dp, 1318.0_dp, & + 1368.0_dp, 1393.0_dp, 1393.0_dp, 1520.0_dp, 1520.0_dp, -29.0_dp, -29.0_dp, 1520.0_dp, & + 1520.0_dp, 1483.5_dp, 1410.5_dp, 1337.5_dp, 1238.7_dp, 1114.0_dp, 988.0_dp, 888.7_dp, & + 816.0_dp, 743.3_dp, 707.0_dp, 707.0_dp, 707.0_dp, 743.5_dp, 816.5_dp, 889.5_dp, & + 988.7_dp, 1114.0_dp, 1238.0_dp, 1336.7_dp, 1410.0_dp, 1483.3_dp, 1520.0_dp, 1520.0_dp, & + 803.0_dp, 749.0_dp, 695.2_dp, 641.5_dp, 587.8_dp, 531.7_dp, 473.0_dp, 375.7_dp, & + 294.7_dp, 230.0_dp, 165.3_dp, 133.0_dp, 133.0_dp, 133.0_dp, 143.5_dp, 164.5_dp, & + 185.5_dp, 217.3_dp, 260.0_dp, 915.0_dp, 395.0_dp, 454.3_dp, 517.8_dp, 585.5_dp, & + 653.2_dp, 725.0_dp, 801.0_dp, 801.0_dp, 713.0_dp, 626.0_dp, 540.0_dp, 454.0_dp, & + 369.0_dp, 285.0_dp, 0.0_dp, 0.0_dp, 147.0_dp, 87.7_dp, 43.5_dp, 14.5_dp, & + -14.5_dp, -29.0_dp, -29.0_dp, -29.0_dp, 16.8_dp, 108.5_dp, 200.2_dp, 317.7_dp, & + 461.0_dp, 546.3_dp, 626.5_dp, 701.5_dp, 776.5_dp, 847.0_dp, 913.0_dp, 955.0_dp, & + 996.8_dp, 1038.5_dp, 1080.2_dp, 1121.0_dp, 1161.0_dp, 1269.0_dp, 1355.8_dp, 1421.5_dp, & + 1487.2_dp, 1520.0_dp, 1520.0_dp, 1520.0_dp, 1514.0_dp, 1502.0_dp, 1490.0_dp, 1472.0_dp, & + 1448.0_dp, 1266.0_dp, 1297.3_dp, 1321.2_dp, 1337.5_dp, 1353.8_dp, 1362.0_dp, 1362.0_dp, & + 1362.0_dp, 1343.5_dp, 1306.5_dp, 1269.5_dp, 1221.7_dp, 1163.0_dp, 1129.0_dp, 1094.8_dp, & + 1060.5_dp, 1026.2_dp, 977.7_dp, 915.0_dp, 1493.0_dp, 938.0_dp, 938.0_dp, 1493.0_dp, & + 1554.0_dp, 1400.7_dp, 1249.0_dp, 1099.0_dp, 949.0_dp, 797.0_dp, 643.0_dp, 489.0_dp, & + 336.5_dp, 185.5_dp, 34.5_dp, -117.3_dp, -270.0_dp, -270.0_dp, -113.3_dp, 40.7_dp, & + 192.0_dp, 343.3_dp, 493.7_dp, 643.0_dp, 791.7_dp, 941.3_dp, 1092.0_dp, 1242.7_dp, & + 1396.7_dp, 1554.0_dp, 1554.0_dp, 1554.0_dp, 1396.7_dp, 1242.7_dp, 1092.0_dp, 941.3_dp, & + 791.7_dp, 643.0_dp, 493.7_dp, 343.3_dp, 192.0_dp, 40.7_dp, -113.3_dp, -270.0_dp, & + -270.0_dp, -117.3_dp, 34.5_dp, 185.5_dp, 336.5_dp, 489.0_dp, 643.0_dp, 797.0_dp, & + 949.0_dp, 1099.0_dp, 1249.0_dp, 1400.7_dp, 1554.0_dp, 1247.0_dp, 1053.0_dp, 858.0_dp, & + 760.0_dp, 963.0_dp, 586.0_dp, 586.0_dp, 963.0_dp, 760.0_dp, 858.0_dp, 1053.0_dp, & + 1247.0_dp, 1346.0_dp, 1143.0_dp, 1520.0_dp, 1520.0_dp, 1143.0_dp, 1346.0_dp, 1284.0_dp, & + 727.0_dp, 727.0_dp, 557.0_dp, 557.0_dp, 0.0_dp, 0.0_dp, 557.0_dp, 557.0_dp, & + 727.0_dp, 727.0_dp, 1284.0_dp, 254.0_dp, 254.0_dp, 82.0_dp, -238.0_dp, -238.0_dp, & + 82.0_dp, 643.0_dp, 643.0_dp, 479.0_dp, 479.0_dp, 254.0_dp, 254.0_dp, 0.0_dp, & + 0.0_dp, 1493.0_dp, 1493.0_dp, -190.0_dp, -190.0_dp, 1360.0_dp, 1360.0_dp, 1308.8_dp, & + 1206.5_dp, 1104.2_dp, 950.3_dp, 745.0_dp, 540.3_dp, 386.8_dp, 284.5_dp, 182.2_dp, & + 131.0_dp, 131.0_dp, 131.0_dp, 182.2_dp, 284.5_dp, 386.8_dp, 540.3_dp, 745.0_dp, & + 950.3_dp, 1104.2_dp, 1206.5_dp, 1308.8_dp, 1360.0_dp, 1360.0_dp, 1520.0_dp, 1520.0_dp, & + 1453.8_dp, 1321.5_dp, 1189.2_dp, 997.0_dp, 745.0_dp, 493.7_dp, 301.8_dp, 169.5_dp, & + 37.2_dp, -29.0_dp, -29.0_dp, -29.0_dp, 37.2_dp, 169.5_dp, 301.8_dp, 493.7_dp, & + 745.0_dp, 997.0_dp, 1189.2_dp, 1321.5_dp, 1453.8_dp, 1520.0_dp, 1520.0_dp, 170.0_dp, & + 170.0_dp, 1309.0_dp, 1237.0_dp, 1421.0_dp, 1493.0_dp, 1493.0_dp, 170.0_dp, 170.0_dp, & + 0.0_dp, 0.0_dp, 170.0_dp, 170.0_dp, 0.0_dp, 0.0_dp, 170.0_dp, 249.3_dp, & + 355.8_dp, 489.5_dp, 623.2_dp, 709.3_dp, 748.0_dp, 820.7_dp, 882.2_dp, 932.5_dp, & + 982.8_dp, 1032.3_dp, 1081.0_dp, 1160.3_dp, 1225.0_dp, 1275.0_dp, 1325.0_dp, 1350.0_dp, & + 1350.0_dp, 1350.0_dp, 1339.0_dp, 1317.0_dp, 1295.0_dp, 1261.7_dp, 1217.0_dp, 1421.0_dp, & + 1453.7_dp, 1478.3_dp, 1495.0_dp, 1511.7_dp, 1520.0_dp, 1520.0_dp, 1520.0_dp, 1481.3_dp, & + 1404.0_dp, 1326.7_dp, 1223.3_dp, 1094.0_dp, 1032.7_dp, 974.5_dp, 919.5_dp, 864.5_dp, & + 799.7_dp, 725.0_dp, 705.7_dp, 649.8_dp, 557.5_dp, 465.2_dp, 336.0_dp, 170.0_dp, & + 805.0_dp, 784.3_dp, 741.3_dp, 676.0_dp, 610.7_dp, 530.0_dp, 434.0_dp, 286.7_dp, & + 172.7_dp, 92.0_dp, 11.3_dp, -29.0_dp, -29.0_dp, -29.0_dp, -22.8_dp, -10.5_dp, & + 1.8_dp, 20.3_dp, 45.0_dp, 240.0_dp, 207.3_dp, 182.7_dp, 166.0_dp, 149.3_dp, & + 141.0_dp, 141.0_dp, 141.0_dp, 166.0_dp, 216.0_dp, 266.0_dp, 338.7_dp, 434.0_dp, & + 522.0_dp, 590.8_dp, 640.5_dp, 690.2_dp, 715.0_dp, 715.0_dp, 715.0_dp, 881.0_dp, & + 881.0_dp, 881.0_dp, 900.8_dp, 940.5_dp, 980.2_dp, 1037.3_dp, 1112.0_dp, 1188.7_dp, & + 1247.5_dp, 1288.5_dp, 1329.5_dp, 1350.0_dp, 1350.0_dp, 1350.0_dp, 1344.0_dp, 1332.0_dp, & + 1320.0_dp, 1301.3_dp, 1276.0_dp, 1456.0_dp, 1477.3_dp, 1493.3_dp, 1504.0_dp, 1514.7_dp, & + 1520.0_dp, 1520.0_dp, 1520.0_dp, 1485.2_dp, 1415.5_dp, 1345.8_dp, 1251.7_dp, 1133.0_dp, & + 1050.3_dp, 980.5_dp, 923.5_dp, 866.5_dp, 827.0_dp, 805.0_dp, 1317.0_dp, 520.0_dp, & + 520.0_dp, 1493.0_dp, 1493.0_dp, 520.0_dp, 520.0_dp, 352.0_dp, 352.0_dp, 0.0_dp, & + 0.0_dp, 352.0_dp, 352.0_dp, 547.0_dp, 1493.0_dp, 1493.0_dp, 1323.0_dp, 1323.0_dp, & + 957.0_dp, 967.0_dp, 974.5_dp, 979.5_dp, 984.5_dp, 987.0_dp, 987.0_dp, 987.0_dp, & + 941.3_dp, 850.0_dp, 758.7_dp, 635.0_dp, 479.0_dp, 318.3_dp, 193.5_dp, 104.5_dp, & + 15.5_dp, -29.0_dp, -29.0_dp, -29.0_dp, -23.7_dp, -13.0_dp, -2.3_dp, 13.7_dp, & + 35.0_dp, 238.0_dp, 205.3_dp, 181.0_dp, 165.0_dp, 149.0_dp, 141.0_dp, 141.0_dp, & + 141.0_dp, 171.3_dp, 232.0_dp, 292.7_dp, 375.0_dp, 479.0_dp, 583.0_dp, 665.3_dp, & + 726.0_dp, 786.7_dp, 817.0_dp, 817.0_dp, 817.0_dp, 811.0_dp, 799.0_dp, 787.0_dp, & + 768.3_dp, 743.0_dp, 827.0_dp, 827.0_dp, 796.0_dp, 734.0_dp, 672.0_dp, 587.0_dp, & + 479.0_dp, 371.7_dp, 286.8_dp, 224.5_dp, 162.2_dp, 131.0_dp, 131.0_dp, 131.0_dp, & + 162.2_dp, 224.5_dp, 286.8_dp, 371.7_dp, 479.0_dp, 587.0_dp, 672.0_dp, 734.0_dp, & + 796.0_dp, 827.0_dp, 827.0_dp, 1460.0_dp, 1276.0_dp, 1300.0_dp, 1318.3_dp, 1331.0_dp, & + 1343.7_dp, 1350.0_dp, 1350.0_dp, 1350.0_dp, 1305.0_dp, 1215.0_dp, 1125.0_dp, 989.0_dp, & + 807.0_dp, 865.0_dp, 909.5_dp, 940.5_dp, 971.5_dp, 987.0_dp, 987.0_dp, 987.0_dp, & + 941.5_dp, 850.5_dp, 759.5_dp, 635.7_dp, 479.0_dp, 325.7_dp, 202.7_dp, 110.0_dp, & + 17.3_dp, -29.0_dp, -29.0_dp, -29.0_dp, 37.2_dp, 169.5_dp, 301.8_dp, 493.7_dp, & + 745.0_dp, 981.0_dp, 1169.2_dp, 1309.5_dp, 1449.8_dp, 1520.0_dp, 1520.0_dp, 1520.0_dp, & + 1515.0_dp, 1505.0_dp, 1495.0_dp, 1480.0_dp, 1460.0_dp, 1493.0_dp, 1493.0_dp, 1407.0_dp, & + 0.0_dp, 0.0_dp, 1323.0_dp, 1323.0_dp, 709.0_dp, 709.0_dp, 683.3_dp, 632.0_dp, & + 580.7_dp, 510.0_dp, 420.0_dp, 330.0_dp, 259.3_dp, 208.0_dp, 156.7_dp, 131.0_dp, & + 131.0_dp, 131.0_dp, 156.8_dp, 208.5_dp, 260.2_dp, 330.7_dp, 420.0_dp, 510.0_dp, & + 580.7_dp, 632.0_dp, 683.3_dp, 709.0_dp, 709.0_dp, 795.0_dp, 816.3_dp, 856.7_dp, & + 916.0_dp, 975.3_dp, 1047.7_dp, 1133.0_dp, 1252.3_dp, 1346.7_dp, 1416.0_dp, 1485.3_dp, & + 1520.0_dp, 1520.0_dp, 1520.0_dp, 1485.3_dp, 1416.0_dp, 1346.7_dp, 1252.3_dp, 1133.0_dp, & + 1047.7_dp, 975.3_dp, 916.0_dp, 856.7_dp, 816.3_dp, 795.0_dp, 772.3_dp, 728.0_dp, & + 662.0_dp, 596.0_dp, 515.3_dp, 420.0_dp, 275.3_dp, 164.3_dp, 87.0_dp, 9.7_dp, & + -29.0_dp, -29.0_dp, -29.0_dp, 9.7_dp, 87.0_dp, 164.3_dp, 275.3_dp, 420.0_dp, & + 515.3_dp, 596.0_dp, 662.0_dp, 728.0_dp, 772.3_dp, 795.0_dp, 1114.0_dp, 1036.7_dp, & + 976.3_dp, 933.0_dp, 889.7_dp, 868.0_dp, 868.0_dp, 868.0_dp, 889.7_dp, 933.0_dp, & + 976.3_dp, 1036.7_dp, 1114.0_dp, 1191.3_dp, 1251.7_dp, 1295.0_dp, 1338.3_dp, 1360.0_dp, & + 1360.0_dp, 1360.0_dp, 1338.3_dp, 1295.0_dp, 1251.7_dp, 1191.3_dp, 1114.0_dp, 31.0_dp, & + 215.0_dp, 191.0_dp, 172.7_dp, 160.0_dp, 147.3_dp, 141.0_dp, 141.0_dp, 141.0_dp, & + 185.8_dp, 275.5_dp, 365.2_dp, 501.3_dp, 684.0_dp, 626.7_dp, 582.7_dp, 552.0_dp, & + 521.3_dp, 506.0_dp, 506.0_dp, 506.0_dp, 551.2_dp, 641.5_dp, 731.8_dp, 855.3_dp, & + 1012.0_dp, 1165.3_dp, 1288.3_dp, 1381.0_dp, 1473.7_dp, 1520.0_dp, 1520.0_dp, 1520.0_dp, & + 1453.8_dp, 1321.5_dp, 1189.2_dp, 997.0_dp, 745.0_dp, 509.7_dp, 321.8_dp, 181.5_dp, & + 41.2_dp, -29.0_dp, -29.0_dp, -29.0_dp, -24.0_dp, -14.0_dp, -4.0_dp, 11.0_dp, & + 31.0_dp, 664.0_dp, 664.0_dp, 695.0_dp, 757.0_dp, 819.0_dp, 904.0_dp, 1012.0_dp, & + 1119.3_dp, 1204.2_dp, 1266.5_dp, 1328.8_dp, 1360.0_dp, 1360.0_dp, 1360.0_dp, 1328.8_dp, & + 1266.5_dp, 1204.2_dp, 1119.3_dp, 1012.0_dp, 904.0_dp, 819.0_dp, 757.0_dp, 695.0_dp, & + 664.0_dp, 664.0_dp, 254.0_dp, 254.0_dp, 0.0_dp, 0.0_dp, 1059.0_dp, 1059.0_dp, & + 805.0_dp, 805.0_dp, 1059.0_dp, 1059.0_dp, 805.0_dp, 805.0_dp, 254.0_dp, 254.0_dp, & + 82.0_dp, -238.0_dp, -238.0_dp, 82.0_dp, 1008.0_dp, 641.0_dp, 276.0_dp, 94.0_dp, & + 559.0_dp, 725.0_dp, 1190.0_dp, 930.0_dp, 930.0_dp, 762.0_dp, 762.0_dp, 522.0_dp, & + 522.0_dp, 352.0_dp, 352.0_dp, 1008.0_dp, 1190.0_dp, 725.0_dp, 559.0_dp, 94.0_dp, & + 276.0_dp, 641.0_dp, 254.0_dp, 254.0_dp, 0.0_dp, 0.0_dp, 401.0_dp, 401.0_dp, & + 555.0_dp, 622.3_dp, 677.7_dp, 721.0_dp, 764.3_dp, 814.7_dp, 872.0_dp, 961.0_dp, & + 996.3_dp, 1029.7_dp, 1061.0_dp, 1092.3_dp, 1124.3_dp, 1157.0_dp, 1216.3_dp, 1264.3_dp, & + 1301.0_dp, 1337.7_dp, 1356.0_dp, 1356.0_dp, 1356.0_dp, 1344.3_dp, 1321.0_dp, 1297.7_dp, & + 1263.7_dp, 1219.0_dp, 1407.0_dp, 1445.0_dp, 1473.3_dp, 1492.0_dp, 1510.7_dp, 1520.0_dp, & + 1520.0_dp, 1520.0_dp, 1487.7_dp, 1423.0_dp, 1358.3_dp, 1273.0_dp, 1167.0_dp, 1116.3_dp, & + 1068.2_dp, 1022.5_dp, 976.8_dp, 925.3_dp, 868.0_dp, 782.0_dp, 750.7_dp, 726.2_dp, & + 708.5_dp, 690.8_dp, 673.7_dp, 657.0_dp, 643.0_dp, 626.0_dp, 606.0_dp, 586.0_dp, & + 558.7_dp, 524.0_dp, 537.0_dp, 441.7_dp, 366.8_dp, 312.5_dp, 258.2_dp, 231.0_dp, & + 231.0_dp, 231.0_dp, 258.3_dp, 313.0_dp, 367.7_dp, 442.3_dp, 537.0_dp, 630.3_dp, & + 704.5_dp, 759.5_dp, 814.5_dp, 842.0_dp, 842.0_dp, 842.0_dp, 814.7_dp, 760.0_dp, & + 705.3_dp, 631.0_dp, 537.0_dp, 238.0_dp, 186.7_dp, 148.8_dp, 124.5_dp, 100.2_dp, & + 88.0_dp, 88.0_dp, 88.0_dp, 129.5_dp, 212.5_dp, 295.5_dp, 403.7_dp, 537.0_dp, & + 670.3_dp, 778.7_dp, 862.0_dp, 945.3_dp, 987.0_dp, 987.0_dp, 987.0_dp, 974.5_dp, & + 949.5_dp, 924.5_dp, 886.7_dp, 836.0_dp, 967.0_dp, 967.0_dp, 231.0_dp, 245.7_dp, & + 290.2_dp, 364.5_dp, 438.8_dp, 535.0_dp, 653.0_dp, 724.3_dp, 791.3_dp, 854.0_dp, & + 916.7_dp, 974.7_dp, 1028.0_dp, 1115.3_dp, 1182.2_dp, 1228.5_dp, 1274.8_dp, 1298.0_dp, & + 1298.0_dp, 1298.0_dp, 1287.2_dp, 1265.5_dp, 1243.8_dp, 1211.7_dp, 1169.0_dp, 1101.0_dp, & + 1011.8_dp, 901.5_dp, 791.2_dp, 671.7_dp, 543.0_dp, 437.0_dp, 337.7_dp, 245.0_dp, & + 152.3_dp, 70.7_dp, 0.0_dp, -69.3_dp, -122.2_dp, -158.5_dp, -194.8_dp, -213.0_dp, & + -213.0_dp, -213.0_dp, -198.5_dp, -169.5_dp, -140.5_dp, -99.0_dp, -45.0_dp, -156.0_dp, & + -220.7_dp, -270.2_dp, -304.5_dp, -338.8_dp, -356.0_dp, -356.0_dp, -356.0_dp, -334.2_dp, & + -290.5_dp, -246.8_dp, -183.3_dp, -100.0_dp, -16.7_dp, 79.8_dp, 189.5_dp, 299.2_dp, & + 417.0_dp, 543.0_dp, 664.3_dp, 780.0_dp, 890.0_dp, 1000.0_dp, 1096.7_dp, 1180.0_dp, & + 1264.7_dp, 1329.5_dp, 1374.5_dp, 1419.5_dp, 1442.0_dp, 1442.0_dp, 1442.0_dp, 1413.0_dp, & + 1355.0_dp, 1297.0_dp, 1214.7_dp, 1108.0_dp, 1042.7_dp, 971.7_dp, 895.0_dp, 818.3_dp, & + 739.0_dp, 657.0_dp, 481.7_dp, 343.3_dp, 242.0_dp, 140.7_dp, 88.0_dp, 84.0_dp, & + 1294.0_dp, 551.0_dp, 551.0_dp, 1493.0_dp, 1493.0_dp, 0.0_dp, 0.0_dp, 383.0_dp, & + 383.0_dp, 0.0_dp, 0.0_dp, 713.0_dp, 166.0_dp, 166.0_dp, 166.0_dp, 188.5_dp, & + 233.5_dp, 278.5_dp, 347.3_dp, 440.0_dp, 533.3_dp, 602.2_dp, 646.5_dp, 690.8_dp, & + 713.0_dp, 713.0_dp, 1327.0_dp, 877.0_dp, 877.0_dp, 877.0_dp, 895.5_dp, 932.5_dp, & + 969.5_dp, 1026.0_dp, 1102.0_dp, 1177.3_dp, 1233.7_dp, 1271.0_dp, 1308.3_dp, 1327.0_dp, & + 1327.0_dp, 1493.0_dp, 1493.0_dp, 1493.0_dp, 1461.0_dp, 1397.0_dp, 1333.0_dp, 1242.0_dp, & + 1124.0_dp, 1032.7_dp, 960.0_dp, 906.0_dp, 852.0_dp, 818.3_dp, 805.0_dp, 783.7_dp, & + 739.2_dp, 671.5_dp, 603.8_dp, 519.3_dp, 418.0_dp, 284.7_dp, 181.7_dp, 109.0_dp, & + 36.3_dp, 0.0_dp, 0.0_dp, 0.0_dp, 1378.0_dp, 1165.0_dp, 1228.3_dp, 1275.7_dp, & + 1307.0_dp, 1338.3_dp, 1354.0_dp, 1354.0_dp, 1354.0_dp, 1301.8_dp, 1197.5_dp, 1093.2_dp, & + 942.3_dp, 745.0_dp, 548.3_dp, 397.8_dp, 293.5_dp, 189.2_dp, 137.0_dp, 137.0_dp, & + 137.0_dp, 152.7_dp, 184.0_dp, 215.3_dp, 262.7_dp, 326.0_dp, 115.0_dp, 67.0_dp, & + 31.0_dp, 7.0_dp, -17.0_dp, -29.0_dp, -29.0_dp, -29.0_dp, 40.2_dp, 178.5_dp, & + 316.8_dp, 505.7_dp, 745.0_dp, 985.0_dp, 1174.2_dp, 1312.5_dp, 1450.8_dp, 1520.0_dp, & + 1520.0_dp, 1520.0_dp, 1508.2_dp, 1484.5_dp, 1460.8_dp, 1425.3_dp, 1378.0_dp, 1327.0_dp, & + 166.0_dp, 166.0_dp, 166.0_dp, 212.7_dp, 306.0_dp, 399.3_dp, 546.7_dp, 748.0_dp, & + 948.0_dp, 1094.5_dp, 1187.5_dp, 1280.5_dp, 1327.0_dp, 1327.0_dp, 1493.0_dp, 1493.0_dp, & + 1493.0_dp, 1432.8_dp, 1312.5_dp, 1192.2_dp, 1004.0_dp, 748.0_dp, 490.7_dp, 301.7_dp, & + 181.0_dp, 60.3_dp, 0.0_dp, 0.0_dp, 0.0_dp, 1493.0_dp, 1493.0_dp, 1323.0_dp, & + 1323.0_dp, 881.0_dp, 881.0_dp, 711.0_dp, 711.0_dp, 170.0_dp, 170.0_dp, 0.0_dp, & + 0.0_dp, 1493.0_dp, 1493.0_dp, 1323.0_dp, 1323.0_dp, 883.0_dp, 883.0_dp, 713.0_dp, & + 713.0_dp, 0.0_dp, 0.0_dp, 213.0_dp, 614.0_dp, 614.0_dp, 780.0_dp, 780.0_dp, & + 139.0_dp, 83.7_dp, 41.8_dp, 13.5_dp, -14.8_dp, -29.0_dp, -29.0_dp, -29.0_dp, & + 39.2_dp, 175.5_dp, 311.8_dp, 501.7_dp, 745.0_dp, 989.0_dp, 1179.2_dp, 1315.5_dp, & + 1451.8_dp, 1520.0_dp, 1520.0_dp, 1520.0_dp, 1508.0_dp, 1484.0_dp, 1460.0_dp, 1424.7_dp, & + 1378.0_dp, 1163.0_dp, 1226.3_dp, 1274.0_dp, 1306.0_dp, 1338.0_dp, 1354.0_dp, 1354.0_dp, & + 1354.0_dp, 1303.0_dp, 1201.0_dp, 1099.0_dp, 947.0_dp, 745.0_dp, 543.7_dp, 392.0_dp, & + 290.0_dp, 188.0_dp, 137.0_dp, 137.0_dp, 137.0_dp, 143.2_dp, 155.5_dp, 167.8_dp, & + 187.0_dp, 213.0_dp, 1493.0_dp, 1493.0_dp, 881.0_dp, 881.0_dp, 1493.0_dp, 1493.0_dp, & + 0.0_dp, 0.0_dp, 711.0_dp, 711.0_dp, 0.0_dp, 0.0_dp, 1493.0_dp, 1493.0_dp, & + 0.0_dp, 0.0_dp, 1493.0_dp, 1493.0_dp, 104.0_dp, -76.0_dp, -206.7_dp, -288.0_dp, & + -369.3_dp, -410.0_dp, -410.0_dp, -410.0_dp, -240.0_dp, -240.0_dp, -240.0_dp, -215.0_dp, & + -165.0_dp, -115.0_dp, -25.3_dp, 104.0_dp, 1493.0_dp, 1493.0_dp, 862.0_dp, 1493.0_dp, & + 1493.0_dp, 797.0_dp, 0.0_dp, 0.0_dp, 719.0_dp, 0.0_dp, 0.0_dp, 1493.0_dp, & + 1493.0_dp, 170.0_dp, 170.0_dp, 0.0_dp, 0.0_dp, 1493.0_dp, 1493.0_dp, 477.0_dp, & + 1493.0_dp, 1493.0_dp, 0.0_dp, 0.0_dp, 1311.0_dp, 287.0_dp, 287.0_dp, 1311.0_dp, & + 0.0_dp, 0.0_dp, 1493.0_dp, 1493.0_dp, 244.0_dp, 1493.0_dp, 1493.0_dp, 0.0_dp, & + 0.0_dp, 1249.0_dp, 0.0_dp, 0.0_dp, 1356.0_dp, 1356.0_dp, 1301.3_dp, 1192.0_dp, & + 1082.7_dp, 933.7_dp, 745.0_dp, 557.0_dp, 408.3_dp, 299.0_dp, 189.7_dp, 135.0_dp, & + 135.0_dp, 135.0_dp, 189.7_dp, 299.0_dp, 408.3_dp, 557.0_dp, 745.0_dp, 933.7_dp, & + 1082.7_dp, 1192.0_dp, 1301.3_dp, 1356.0_dp, 1356.0_dp, 1520.0_dp, 1520.0_dp, 1449.8_dp, & + 1309.5_dp, 1169.2_dp, 981.0_dp, 745.0_dp, 509.7_dp, 321.8_dp, 181.5_dp, 41.2_dp, & + -29.0_dp, -29.0_dp, -29.0_dp, 41.0_dp, 181.0_dp, 321.0_dp, 509.0_dp, 745.0_dp, & + 981.0_dp, 1169.2_dp, 1309.5_dp, 1449.8_dp, 1520.0_dp, 1520.0_dp, 1327.0_dp, 766.0_dp, & + 766.0_dp, 766.0_dp, 790.3_dp, 839.0_dp, 887.7_dp, 957.0_dp, 1047.0_dp, 1136.3_dp, & + 1205.3_dp, 1254.0_dp, 1302.7_dp, 1327.0_dp, 1327.0_dp, 1493.0_dp, 1493.0_dp, 1493.0_dp, & + 1455.2_dp, 1379.5_dp, 1303.8_dp, 1193.0_dp, 1047.0_dp, 899.7_dp, 788.3_dp, 713.0_dp, & + 637.7_dp, 600.0_dp, 600.0_dp, 600.0_dp, 0.0_dp, 0.0_dp, 1356.0_dp, 1356.0_dp, & + 1301.3_dp, 1192.0_dp, 1082.7_dp, 933.7_dp, 745.0_dp, 557.0_dp, 408.3_dp, 299.0_dp, & + 189.7_dp, 135.0_dp, 135.0_dp, 135.0_dp, 189.7_dp, 299.0_dp, 408.3_dp, 557.0_dp, & + 745.0_dp, 933.7_dp, 1082.7_dp, 1192.0_dp, 1301.3_dp, 1356.0_dp, 1356.0_dp, 27.0_dp, & + -264.0_dp, -264.0_dp, -25.0_dp, -26.3_dp, -27.3_dp, -28.0_dp, -28.7_dp, -29.0_dp, & + -29.0_dp, -29.0_dp, 41.2_dp, 181.5_dp, 321.8_dp, 509.7_dp, 745.0_dp, 981.0_dp, & + 1169.2_dp, 1309.5_dp, 1449.8_dp, 1520.0_dp, 1520.0_dp, 1520.0_dp, 1449.8_dp, 1309.5_dp, & + 1169.2_dp, 981.0_dp, 745.0_dp, 571.7_dp, 423.3_dp, 300.0_dp, 176.7_dp, 85.7_dp, & + 27.0_dp, 700.0_dp, 685.3_dp, 654.0_dp, 606.0_dp, 558.0_dp, 492.0_dp, 408.0_dp, & + 0.0_dp, 0.0_dp, 383.0_dp, 483.0_dp, 549.3_dp, 582.0_dp, 614.7_dp, 631.0_dp, & + 631.0_dp, 631.0_dp, 0.0_dp, 0.0_dp, 1493.0_dp, 1493.0_dp, 1493.0_dp, 1457.3_dp, & + 1386.0_dp, 1314.7_dp, 1207.0_dp, 1063.0_dp, 969.0_dp, 891.0_dp, 829.0_dp, 767.0_dp, & + 724.0_dp, 700.0_dp, 1327.0_dp, 797.0_dp, 797.0_dp, 797.0_dp, 819.5_dp, 864.5_dp, & + 909.5_dp, 975.7_dp, 1063.0_dp, 1150.3_dp, 1216.2_dp, 1260.5_dp, 1304.8_dp, 1327.0_dp, & + 1327.0_dp, 1444.0_dp, 1247.0_dp, 1283.7_dp, 1311.0_dp, 1329.0_dp, 1347.0_dp, 1356.0_dp, & + 1356.0_dp, 1356.0_dp, 1334.7_dp, 1292.0_dp, 1249.3_dp, 1188.7_dp, 1110.0_dp, 1044.0_dp, & + 994.2_dp, 960.5_dp, 926.8_dp, 899.7_dp, 879.0_dp, 854.0_dp, 825.3_dp, 774.8_dp, & + 702.5_dp, 630.2_dp, 533.3_dp, 412.0_dp, 267.3_dp, 157.7_dp, 83.0_dp, 8.3_dp, & + -29.0_dp, -29.0_dp, -29.0_dp, -21.0_dp, -5.0_dp, 11.0_dp, 34.7_dp, 66.0_dp, & + 274.0_dp, 228.0_dp, 193.3_dp, 170.0_dp, 146.7_dp, 135.0_dp, 135.0_dp, 135.0_dp, & + 157.7_dp, 203.0_dp, 248.3_dp, 313.0_dp, 397.0_dp, 470.3_dp, 527.7_dp, 569.0_dp, & + 610.3_dp, 641.3_dp, 662.0_dp, 686.0_dp, 716.0_dp, 763.0_dp, 827.0_dp, 891.0_dp, & + 980.0_dp, 1094.0_dp, 1226.0_dp, 1330.0_dp, 1406.0_dp, 1482.0_dp, 1520.0_dp, 1520.0_dp, & + 1520.0_dp, 1513.7_dp, 1501.0_dp, 1488.3_dp, 1469.3_dp, 1444.0_dp, 1493.0_dp, 1493.0_dp, & + 1323.0_dp, 1323.0_dp, 0.0_dp, 0.0_dp, 1323.0_dp, 1323.0_dp, 1493.0_dp, 1493.0_dp, & + 586.0_dp, 426.0_dp, 310.8_dp, 240.5_dp, 170.2_dp, 135.0_dp, 135.0_dp, 135.0_dp, & + 170.2_dp, 240.5_dp, 310.8_dp, 426.0_dp, 586.0_dp, 1493.0_dp, 1493.0_dp, 561.0_dp, & + 366.3_dp, 219.3_dp, 120.0_dp, 20.7_dp, -29.0_dp, -29.0_dp, -29.0_dp, 20.7_dp, & + 120.0_dp, 219.3_dp, 366.3_dp, 561.0_dp, 0.0_dp, 1493.0_dp, 1493.0_dp, 236.0_dp, & + 1493.0_dp, 1493.0_dp, 0.0_dp, 1493.0_dp, 1493.0_dp, 231.0_dp, 1493.0_dp, 1493.0_dp, & + 231.0_dp, 1493.0_dp, 1493.0_dp, 0.0_dp, 0.0_dp, 1296.0_dp, 0.0_dp, 0.0_dp, & + 1493.0_dp, 1493.0_dp, 938.0_dp, 1493.0_dp, 1493.0_dp, 776.0_dp, 0.0_dp, 0.0_dp, & + 635.0_dp, 0.0_dp, 0.0_dp, 797.0_dp, 1493.0_dp, 1493.0_dp, 879.0_dp, 1493.0_dp, & + 1493.0_dp, 711.0_dp, 0.0_dp, 0.0_dp, 711.0_dp, 1493.0_dp, 1493.0_dp, 1339.0_dp, & + 170.0_dp, 170.0_dp, 0.0_dp, 0.0_dp, 154.0_dp, 1323.0_dp, 1323.0_dp, 1556.0_dp, & + 1556.0_dp, 1413.0_dp, 1413.0_dp, -127.0_dp, -127.0_dp, -270.0_dp, -270.0_dp, 1493.0_dp, & + -190.0_dp, -190.0_dp, 1493.0_dp, 1556.0_dp, -270.0_dp, -270.0_dp, -127.0_dp, -127.0_dp, & + 1413.0_dp, 1413.0_dp, 1556.0_dp, 1493.0_dp, 936.0_dp, 936.0_dp, 1331.0_dp, 936.0_dp, & + 936.0_dp, 1493.0_dp, -340.0_dp, -483.0_dp, -483.0_dp, -340.0_dp, 1638.0_dp, 1264.0_dp, & + 1264.0_dp, 1638.0_dp, 563.0_dp, 563.0_dp, 546.0_dp, 512.0_dp, 478.0_dp, 420.0_dp, & + 338.0_dp, 272.7_dp, 220.8_dp, 182.5_dp, 144.2_dp, 125.0_dp, 125.0_dp, 125.0_dp, & + 161.2_dp, 233.5_dp, 305.8_dp, 402.0_dp, 522.0_dp, 563.0_dp, 639.0_dp, 0.0_dp, & + 0.0_dp, 170.0_dp, 102.0_dp, 51.8_dp, 19.5_dp, -12.8_dp, -29.0_dp, -29.0_dp, & + -29.0_dp, 3.2_dp, 67.5_dp, 131.8_dp, 218.0_dp, 326.0_dp, 452.0_dp, 547.0_dp, & + 611.0_dp, 675.0_dp, 707.0_dp, 707.0_dp, 707.0_dp, 725.0_dp, 809.7_dp, 875.2_dp, & + 921.5_dp, 967.8_dp, 991.0_dp, 991.0_dp, 991.0_dp, 983.3_dp, 968.0_dp, 952.7_dp, & + 929.7_dp, 899.0_dp, 1069.0_dp, 1095.0_dp, 1114.5_dp, 1127.5_dp, 1140.5_dp, 1147.0_dp, & + 1147.0_dp, 1147.0_dp, 1105.0_dp, 1021.0_dp, 937.0_dp, 809.7_dp, 639.0_dp, 559.0_dp, & + 694.3_dp, 800.5_dp, 877.5_dp, 954.5_dp, 993.0_dp, 993.0_dp, 993.0_dp, 954.5_dp, & + 877.5_dp, 800.5_dp, 694.3_dp, 559.0_dp, 423.7_dp, 317.5_dp, 240.5_dp, 163.5_dp, & + 125.0_dp, 125.0_dp, 125.0_dp, 163.5_dp, 240.5_dp, 317.5_dp, 423.7_dp, 559.0_dp, & + 950.0_dp, 1016.7_dp, 1066.2_dp, 1098.5_dp, 1130.8_dp, 1147.0_dp, 1147.0_dp, 1147.0_dp, & + 1093.0_dp, 985.0_dp, 877.0_dp, 735.0_dp, 559.0_dp, 383.0_dp, 241.0_dp, 133.0_dp, & + 25.0_dp, -29.0_dp, -29.0_dp, -29.0_dp, -12.8_dp, 19.5_dp, 51.8_dp, 101.3_dp, & + 168.0_dp, 0.0_dp, 0.0_dp, 1556.0_dp, 1556.0_dp, 1077.0_dp, 905.0_dp, 933.7_dp, & + 955.2_dp, 969.5_dp, 983.8_dp, 991.0_dp, 991.0_dp, 991.0_dp, 953.2_dp, 877.5_dp, & + 801.8_dp, 695.7_dp, 559.0_dp, 422.3_dp, 316.2_dp, 240.5_dp, 164.8_dp, 127.0_dp, & + 127.0_dp, 127.0_dp, 134.2_dp, 148.5_dp, 162.8_dp, 184.3_dp, 213.0_dp, 43.0_dp, & + 19.0_dp, 1.0_dp, -11.0_dp, -23.0_dp, -29.0_dp, -29.0_dp, -29.0_dp, 24.0_dp, & + 130.0_dp, 236.0_dp, 379.0_dp, 559.0_dp, 741.7_dp, 885.3_dp, 990.0_dp, 1094.7_dp, & + 1147.0_dp, 1147.0_dp, 1147.0_dp, 1141.2_dp, 1129.5_dp, 1117.8_dp, 1100.3_dp, 1077.0_dp, & + 950.0_dp, 1556.0_dp, 1556.0_dp, 0.0_dp, 0.0_dp, 168.0_dp, 101.3_dp, 51.8_dp, & + 19.5_dp, -12.8_dp, -29.0_dp, -29.0_dp, -29.0_dp, 25.0_dp, 133.0_dp, 241.0_dp, & + 383.0_dp, 559.0_dp, 735.0_dp, 877.0_dp, 985.0_dp, 1093.0_dp, 1147.0_dp, 1147.0_dp, & + 1147.0_dp, 1130.8_dp, 1098.5_dp, 1066.2_dp, 1016.7_dp, 950.0_dp, 559.0_dp, 423.7_dp, & + 317.5_dp, 240.5_dp, 163.5_dp, 125.0_dp, 125.0_dp, 125.0_dp, 163.5_dp, 240.5_dp, & + 317.5_dp, 423.7_dp, 559.0_dp, 694.3_dp, 800.5_dp, 877.5_dp, 954.5_dp, 993.0_dp, & + 993.0_dp, 993.0_dp, 954.5_dp, 877.5_dp, 800.5_dp, 694.3_dp, 559.0_dp, 606.0_dp, & + 516.0_dp, 516.0_dp, 389.3_dp, 292.8_dp, 226.5_dp, 160.2_dp, 127.0_dp, 127.0_dp, & + 127.0_dp, 135.7_dp, 153.0_dp, 170.3_dp, 196.3_dp, 231.0_dp, 57.0_dp, 29.0_dp, & + 7.7_dp, -7.0_dp, -21.7_dp, -29.0_dp, -29.0_dp, -29.0_dp, 23.0_dp, 127.0_dp, & + 231.0_dp, 371.7_dp, 549.0_dp, 732.3_dp, 877.8_dp, 985.5_dp, 1093.2_dp, 1147.0_dp, & + 1147.0_dp, 1147.0_dp, 1098.5_dp, 1001.5_dp, 904.5_dp, 772.7_dp, 606.0_dp, 660.0_dp, & + 760.7_dp, 841.0_dp, 901.0_dp, 961.0_dp, 991.0_dp, 991.0_dp, 991.0_dp, 962.0_dp, & + 904.0_dp, 846.0_dp, 764.3_dp, 659.0_dp, 1556.0_dp, 1403.0_dp, 1403.0_dp, 1403.0_dp, & + 1389.7_dp, 1363.0_dp, 1336.3_dp, 1288.3_dp, 1219.0_dp, 1120.0_dp, 1120.0_dp, 977.0_dp, & + 977.0_dp, 0.0_dp, 0.0_dp, 977.0_dp, 977.0_dp, 1120.0_dp, 1120.0_dp, 1198.0_dp, & + 1322.7_dp, 1413.5_dp, 1470.5_dp, 1527.5_dp, 1556.0_dp, 1556.0_dp, 573.0_dp, 706.3_dp, & + 809.7_dp, 883.0_dp, 956.3_dp, 993.0_dp, 993.0_dp, 993.0_dp, 956.3_dp, 883.0_dp, & + 809.7_dp, 706.3_dp, 573.0_dp, 440.3_dp, 337.3_dp, 264.0_dp, 190.7_dp, 154.0_dp, & + 154.0_dp, 154.0_dp, 190.7_dp, 264.0_dp, 337.3_dp, 440.3_dp, 573.0_dp, 139.0_dp, & + -51.7_dp, -193.5_dp, -286.5_dp, -379.5_dp, -426.0_dp, -426.0_dp, -426.0_dp, -421.2_dp, & + -411.5_dp, -401.8_dp, -387.0_dp, -367.0_dp, -188.0_dp, -217.3_dp, -239.0_dp, -253.0_dp, & + -267.0_dp, -274.0_dp, -274.0_dp, -274.0_dp, -242.5_dp, -179.5_dp, -116.5_dp, -21.3_dp, & + 106.0_dp, 197.0_dp, 131.0_dp, 81.7_dp, 49.0_dp, 16.3_dp, 0.0_dp, 0.0_dp, & + 0.0_dp, 52.3_dp, 157.0_dp, 261.7_dp, 400.3_dp, 573.0_dp, 746.3_dp, 885.3_dp, & + 990.0_dp, 1094.7_dp, 1147.0_dp, 1147.0_dp, 1147.0_dp, 1130.7_dp, 1098.0_dp, 1065.3_dp, & + 1016.0_dp, 950.0_dp, 1120.0_dp, 1120.0_dp, 676.0_dp, 0.0_dp, 0.0_dp, 670.0_dp, & + 776.0_dp, 855.3_dp, 908.0_dp, 960.7_dp, 987.0_dp, 987.0_dp, 987.0_dp, 955.3_dp, & + 892.0_dp, 828.7_dp, 742.3_dp, 633.0_dp, 0.0_dp, 0.0_dp, 1556.0_dp, 1556.0_dp, & + 946.0_dp, 1013.3_dp, 1063.7_dp, 1097.0_dp, 1130.3_dp, 1147.0_dp, 1147.0_dp, 1147.0_dp, & + 1107.2_dp, 1027.5_dp, 947.8_dp, 830.7_dp, 676.0_dp, 1120.0_dp, 1120.0_dp, 0.0_dp, & + 0.0_dp, 1556.0_dp, 1556.0_dp, 1323.0_dp, 1323.0_dp, 1120.0_dp, 1120.0_dp, -20.0_dp, & + -162.7_dp, -266.0_dp, -330.0_dp, -394.0_dp, -426.0_dp, -426.0_dp, -426.0_dp, -270.0_dp, & + -270.0_dp, -270.0_dp, -253.8_dp, -221.5_dp, -189.2_dp, -122.0_dp, -20.0_dp, 1556.0_dp, & + 1556.0_dp, 1323.0_dp, 1323.0_dp, 1556.0_dp, 1556.0_dp, 637.0_dp, 1120.0_dp, 1120.0_dp, & + 596.0_dp, 0.0_dp, 0.0_dp, 547.0_dp, 0.0_dp, 0.0_dp, 1556.0_dp, 1556.0_dp, & + 0.0_dp, 0.0_dp, 905.0_dp, 987.7_dp, 1048.7_dp, 1088.0_dp, 1127.3_dp, 1147.0_dp, & + 1147.0_dp, 1147.0_dp, 1106.2_dp, 1024.5_dp, 942.8_dp, 826.7_dp, 676.0_dp, 0.0_dp, & + 0.0_dp, 670.0_dp, 777.3_dp, 857.0_dp, 909.0_dp, 961.0_dp, 987.0_dp, 987.0_dp, & + 987.0_dp, 955.3_dp, 892.0_dp, 828.7_dp, 742.3_dp, 633.0_dp, 0.0_dp, 0.0_dp, & + 670.0_dp, 778.0_dp, 857.8_dp, 909.5_dp, 961.2_dp, 987.0_dp, 987.0_dp, 987.0_dp, & + 955.2_dp, 891.5_dp, 827.8_dp, 741.7_dp, 633.0_dp, 0.0_dp, 0.0_dp, 1120.0_dp, & + 1120.0_dp, 946.0_dp, 1014.7_dp, 1065.3_dp, 1098.0_dp, 1130.7_dp, 1147.0_dp, 1147.0_dp, & + 1147.0_dp, 1126.3_dp, 1085.0_dp, 1043.7_dp, 983.7_dp, 905.0_dp, 676.0_dp, 0.0_dp, & + 0.0_dp, 670.0_dp, 776.0_dp, 855.3_dp, 908.0_dp, 960.7_dp, 987.0_dp, 987.0_dp, & + 987.0_dp, 955.3_dp, 892.0_dp, 828.7_dp, 742.3_dp, 633.0_dp, 0.0_dp, 0.0_dp, & + 1120.0_dp, 1120.0_dp, 946.0_dp, 1013.3_dp, 1063.7_dp, 1097.0_dp, 1130.3_dp, 1147.0_dp, & + 1147.0_dp, 1147.0_dp, 1107.2_dp, 1027.5_dp, 947.8_dp, 830.7_dp, 676.0_dp, 991.0_dp, & + 991.0_dp, 952.5_dp, 875.5_dp, 798.5_dp, 693.0_dp, 559.0_dp, 425.0_dp, 319.5_dp, & + 242.5_dp, 165.5_dp, 127.0_dp, 127.0_dp, 127.0_dp, 165.7_dp, 243.0_dp, 320.3_dp, & + 425.7_dp, 559.0_dp, 691.7_dp, 796.8_dp, 874.5_dp, 952.2_dp, 991.0_dp, 991.0_dp, & + 1147.0_dp, 1147.0_dp, 1095.0_dp, 991.0_dp, 887.0_dp, 743.0_dp, 559.0_dp, 375.7_dp, & + 231.8_dp, 127.5_dp, 23.2_dp, -29.0_dp, -29.0_dp, -29.0_dp, 23.2_dp, 127.5_dp, & + 231.8_dp, 375.7_dp, 559.0_dp, 743.0_dp, 887.0_dp, 991.0_dp, 1095.0_dp, 1147.0_dp, & + 1147.0_dp, 168.0_dp, -426.0_dp, -426.0_dp, 1120.0_dp, 1120.0_dp, 950.0_dp, 1016.7_dp, & + 1066.2_dp, 1098.5_dp, 1130.8_dp, 1147.0_dp, 1147.0_dp, 1147.0_dp, 1093.0_dp, 985.0_dp, & + 877.0_dp, 735.0_dp, 559.0_dp, 383.0_dp, 241.0_dp, 133.0_dp, 25.0_dp, -29.0_dp, & + -29.0_dp, -29.0_dp, -12.8_dp, 19.5_dp, 51.8_dp, 101.3_dp, 168.0_dp, 559.0_dp, & + 694.3_dp, 800.5_dp, 877.5_dp, 954.5_dp, 993.0_dp, 993.0_dp, 993.0_dp, 954.5_dp, & + 877.5_dp, 800.5_dp, 694.3_dp, 559.0_dp, 423.7_dp, 317.5_dp, 240.5_dp, 163.5_dp, & + 125.0_dp, 125.0_dp, 125.0_dp, 163.5_dp, 240.5_dp, 317.5_dp, 423.7_dp, 559.0_dp, & + 559.0_dp, 423.7_dp, 317.5_dp, 240.5_dp, 163.5_dp, 125.0_dp, 125.0_dp, 125.0_dp, & + 163.5_dp, 240.5_dp, 317.5_dp, 423.7_dp, 559.0_dp, 694.3_dp, 800.5_dp, 877.5_dp, & + 954.5_dp, 993.0_dp, 993.0_dp, 993.0_dp, 954.5_dp, 877.5_dp, 800.5_dp, 694.3_dp, & + 559.0_dp, 168.0_dp, 101.3_dp, 51.8_dp, 19.5_dp, -12.8_dp, -29.0_dp, -29.0_dp, & + -29.0_dp, 25.0_dp, 133.0_dp, 241.0_dp, 383.0_dp, 559.0_dp, 735.0_dp, 877.0_dp, & + 985.0_dp, 1093.0_dp, 1147.0_dp, 1147.0_dp, 1147.0_dp, 1130.8_dp, 1098.5_dp, 1066.2_dp, & + 1016.7_dp, 950.0_dp, 1120.0_dp, 1120.0_dp, -426.0_dp, -426.0_dp, 948.0_dp, 960.0_dp, & + 968.8_dp, 974.5_dp, 980.2_dp, 983.0_dp, 983.0_dp, 983.0_dp, 949.2_dp, 881.5_dp, & + 813.8_dp, 716.7_dp, 590.0_dp, 0.0_dp, 0.0_dp, 1120.0_dp, 1120.0_dp, 946.0_dp, & + 1014.0_dp, 1064.5_dp, 1097.5_dp, 1130.5_dp, 1147.0_dp, 1147.0_dp, 1147.0_dp, 1146.2_dp, & + 1144.5_dp, 1142.8_dp, 1140.3_dp, 1137.0_dp, 1087.0_dp, 913.0_dp, 939.7_dp, 959.7_dp, & + 973.0_dp, 986.3_dp, 993.0_dp, 993.0_dp, 993.0_dp, 979.0_dp, 951.0_dp, 923.0_dp, & + 881.0_dp, 825.0_dp, 782.3_dp, 748.8_dp, 724.5_dp, 700.2_dp, 677.0_dp, 655.0_dp, & + 641.0_dp, 613.0_dp, 573.5_dp, 522.5_dp, 471.5_dp, 400.3_dp, 309.0_dp, 205.0_dp, & + 122.7_dp, 62.0_dp, 1.3_dp, -29.0_dp, -29.0_dp, -29.0_dp, -23.2_dp, -11.5_dp, & + 0.2_dp, 17.7_dp, 41.0_dp, 231.0_dp, 195.7_dp, 169.2_dp, 151.5_dp, 133.8_dp, & + 125.0_dp, 125.0_dp, 125.0_dp, 139.8_dp, 169.5_dp, 199.2_dp, 241.0_dp, 295.0_dp, & + 345.0_dp, 383.3_dp, 410.0_dp, 436.7_dp, 462.3_dp, 487.0_dp, 502.0_dp, 526.0_dp, & + 562.8_dp, 612.5_dp, 662.2_dp, 730.3_dp, 817.0_dp, 922.3_dp, 1003.7_dp, 1061.0_dp, & + 1118.3_dp, 1147.0_dp, 1147.0_dp, 1147.0_dp, 1142.0_dp, 1132.0_dp, 1122.0_dp, 1107.0_dp, & + 1087.0_dp, 1438.0_dp, 1120.0_dp, 1120.0_dp, 977.0_dp, 977.0_dp, 369.0_dp, 277.7_dp, & + 219.0_dp, 193.0_dp, 167.0_dp, 154.0_dp, 154.0_dp, 154.0_dp, 0.0_dp, 0.0_dp, & + 0.0_dp, 26.5_dp, 79.5_dp, 132.5_dp, 229.0_dp, 369.0_dp, 977.0_dp, 977.0_dp, & + 1120.0_dp, 1120.0_dp, 1438.0_dp, 442.0_dp, 1120.0_dp, 1120.0_dp, 449.0_dp, 343.0_dp, & + 263.5_dp, 210.5_dp, 157.5_dp, 131.0_dp, 131.0_dp, 131.0_dp, 162.7_dp, 226.0_dp, & + 289.3_dp, 375.7_dp, 485.0_dp, 1120.0_dp, 1120.0_dp, 0.0_dp, 0.0_dp, 172.0_dp, & + 104.0_dp, 53.5_dp, 20.5_dp, -12.5_dp, -29.0_dp, -29.0_dp, -29.0_dp, 11.0_dp, & + 91.0_dp, 171.0_dp, 288.0_dp, 442.0_dp, 1147.0_dp, 1120.0_dp, 1120.0_dp, 180.0_dp, & + 1120.0_dp, 1120.0_dp, 0.0_dp, 0.0_dp, 1120.0_dp, 1120.0_dp, 246.0_dp, 1120.0_dp, & + 1120.0_dp, 246.0_dp, 1120.0_dp, 1120.0_dp, 0.0_dp, 0.0_dp, 918.0_dp, 0.0_dp, & + 0.0_dp, 1120.0_dp, 575.0_dp, 0.0_dp, 0.0_dp, 440.0_dp, 0.0_dp, 0.0_dp, & + 586.0_dp, 1120.0_dp, 1120.0_dp, 721.0_dp, 1120.0_dp, -104.0_dp, -237.3_dp, -324.3_dp, & + -365.0_dp, -405.7_dp, -426.0_dp, -426.0_dp, -426.0_dp, -272.0_dp, -272.0_dp, -272.0_dp, & + -260.0_dp, -236.0_dp, -212.0_dp, -155.3_dp, -66.0_dp, 18.0_dp, 1120.0_dp, 1120.0_dp, & + 244.0_dp, 1120.0_dp, 1120.0_dp, 1120.0_dp, 1120.0_dp, 952.0_dp, 147.0_dp, 147.0_dp, & + 0.0_dp, 0.0_dp, 168.0_dp, 973.0_dp, 973.0_dp, -190.0_dp, -334.0_dp, -334.0_dp, & + -334.0_dp, -309.3_dp, -260.0_dp, -210.7_dp, -112.3_dp, 35.0_dp, 274.0_dp, 374.7_dp, & + 444.3_dp, 483.0_dp, 521.7_dp, 541.0_dp, 541.0_dp, 541.0_dp, 684.0_dp, 684.0_dp, & + 684.0_dp, 703.2_dp, 741.5_dp, 779.8_dp, 848.7_dp, 948.0_dp, 1188.0_dp, 1335.3_dp, & + 1433.5_dp, 1482.5_dp, 1531.5_dp, 1556.0_dp, 1556.0_dp, 1556.0_dp, 1413.0_dp, 1413.0_dp, & + 1413.0_dp, 1398.3_dp, 1369.0_dp, 1339.7_dp, 1278.0_dp, 1184.0_dp, 936.0_dp, 831.3_dp, & + 755.3_dp, 708.0_dp, 660.7_dp, 628.7_dp, 612.0_dp, 594.0_dp, 561.3_dp, 514.0_dp, & + 466.7_dp, 391.0_dp, 287.0_dp, 39.0_dp, -55.0_dp, -116.7_dp, -146.0_dp, -175.3_dp, & + -190.0_dp, -190.0_dp, 1565.0_dp, -483.0_dp, -483.0_dp, 1565.0_dp, -190.0_dp, -190.0_dp, & + -190.0_dp, -175.7_dp, -147.0_dp, -118.3_dp, -56.3_dp, 39.0_dp, 287.0_dp, 391.0_dp, & + 466.7_dp, 514.0_dp, 561.3_dp, 594.0_dp, 612.0_dp, 628.7_dp, 660.7_dp, 708.0_dp, & + 755.3_dp, 831.3_dp, 936.0_dp, 1184.0_dp, 1278.7_dp, 1340.5_dp, 1369.5_dp, 1398.5_dp, & + 1413.0_dp, 1413.0_dp, 1413.0_dp, 1556.0_dp, 1556.0_dp, 1556.0_dp, 1531.5_dp, 1482.5_dp, & + 1433.5_dp, 1335.3_dp, 1188.0_dp, 948.0_dp, 848.7_dp, 779.8_dp, 741.5_dp, 703.2_dp, & + 684.0_dp, 684.0_dp, 684.0_dp, 541.0_dp, 541.0_dp, 541.0_dp, 521.7_dp, 483.0_dp, & + 444.3_dp, 374.7_dp, 274.0_dp, 35.0_dp, -112.3_dp, -210.7_dp, -260.0_dp, -309.3_dp, & + -334.0_dp, -334.0_dp, -334.0_dp, 817.0_dp, 639.0_dp, 586.3_dp, 548.7_dp, 526.0_dp, & + 503.3_dp, 492.0_dp, 492.0_dp, 492.0_dp, 511.7_dp, 551.0_dp, 553.7_dp, 555.7_dp, & + 557.0_dp, 559.0_dp, 561.7_dp, 565.0_dp, 606.3_dp, 627.0_dp, 627.0_dp, 627.0_dp, & + 614.2_dp, 588.5_dp, 562.8_dp, 522.3_dp, 467.0_dp, 645.0_dp, 697.7_dp, 735.5_dp, & + 758.5_dp, 781.5_dp, 793.0_dp, 793.0_dp, 793.0_dp, 773.0_dp, 733.0_dp, 730.3_dp, & + 728.3_dp, 727.0_dp, 725.0_dp, 722.3_dp, 719.0_dp, 677.7_dp, 657.0_dp, 657.0_dp, & + 657.0_dp, 669.7_dp, 695.0_dp, 720.3_dp, 761.0_dp, 817.0_dp & + ] + +contains + + ! Advance width in font units. Characters outside the table are + ! treated as spaces so that an unexpected byte cannot crash a plot. + function glyph_advance(c) result(a) + integer, intent(in) :: c + real(dp) :: a + if (c < GLYPH_FIRST .or. c > GLYPH_LAST) then + a = real(ADV(1), dp) + else + a = real(ADV(c - GLYPH_FIRST + 1), dp) + end if + end function glyph_advance + + ! The verbs for one character, empty when there is nothing to draw. + subroutine glyph_verbs(c, v, n) + integer, intent(in) :: c + integer, allocatable, intent(out) :: v(:) + integer, intent(out) :: n + integer :: i + if (c < GLYPH_FIRST .or. c > GLYPH_LAST) then + n = 0 + allocate (v(0)) + return + end if + i = c - GLYPH_FIRST + 1 + n = VLEN(i) + allocate (v(n)) + if (n > 0) v = VERBS(VBEG(i):VBEG(i) + n - 1) + end subroutine glyph_verbs + + ! The points for one character, in font units. + subroutine glyph_points(c, x, y, n) + integer, intent(in) :: c + real(dp), allocatable, intent(out) :: x(:), y(:) + integer, intent(out) :: n + integer :: i, j, nv_, k, p0 + if (c < GLYPH_FIRST .or. c > GLYPH_LAST) then + n = 0 + allocate (x(0), y(0)) + return + end if + i = c - GLYPH_FIRST + 1 + nv_ = VLEN(i) + n = 0 + do j = 1, nv_ + k = VERBS(VBEG(i) + j - 1) + if (k == 1 .or. k == 2) n = n + 1 + if (k == 3) n = n + 3 + end do + allocate (x(n), y(n)) + p0 = PBEG(i) + if (n > 0) then + x = PX(p0:p0 + n - 1) + y = PY(p0:p0 + n - 1) + end if + end subroutine glyph_points + +end module fplot_glyphs diff --git a/src/fplot_png.f90 b/src/fplot_png.f90 new file mode 100644 index 0000000..76f9d9d --- /dev/null +++ b/src/fplot_png.f90 @@ -0,0 +1,457 @@ +! fplot_png — the PNG container: filtering, deflate and chunks. +! +! Kept apart from the rasterizer because the two have nothing to say to each +! other: this module turns a finished pixel buffer into file bytes and knows +! nothing about drawing, and the rasterizer knows nothing about file format. +! +! The compressor is a real deflate rather than stored blocks. Stored blocks +! would be a dozen lines and would work, but a plot is mostly flat background, +! which is exactly what the row filters and LZ77 are good at: a typical figure +! comes out around thirty times smaller than it would uncompressed, which is +! the difference between a file worth attaching to an email and one that is +! not. Fixed Huffman codes are used rather than dynamic ones, which costs a +! few percent of size and saves the whole code-length-tree machinery. +! +! No zlib, no libpng, nothing to link. + +module fplot_png + implicit none + private + + public :: png_encode + + ! LZ77 parameters. WSIZE is fixed by the format; CHAIN bounds how hard the + ! matcher looks before settling, which is the usual size/speed dial. + integer, parameter :: WSIZE = 32768 + integer, parameter :: HSIZE = 16384 + integer, parameter :: MIN_MATCH = 3, MAX_MATCH = 258 + integer, parameter :: CHAIN = 64 + + ! IEND carries no payload. + integer, parameter :: NO_DATA(0) = [integer ::] + + integer, parameter :: LEN_BASE(257:285) = [ & + 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, & + 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258] + integer, parameter :: LEN_EXTRA(257:285) = [ & + 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, & + 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0] + integer, parameter :: DIST_BASE(0:29) = [ & + 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, & + 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, & + 16385, 24577] + integer, parameter :: DIST_EXTRA(0:29) = [ & + 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, & + 9, 9, 10, 10, 11, 11, 12, 12, 13, 13] + + ! A growable byte buffer with a bit-level cursor, since deflate codes are + ! not byte aligned but PNG chunks are. + type :: bitbuf_t + integer, allocatable :: b(:) + integer :: n = 0 + integer :: acc = 0 + integer :: nbits = 0 + end type bitbuf_t + +contains + + ! ------------------------------------------------------------------ + ! Byte buffer + ! ------------------------------------------------------------------ + + subroutine push(buf, v) + type(bitbuf_t), intent(inout) :: buf + integer, intent(in) :: v + integer, allocatable :: t(:) + + if (.not. allocated(buf%b)) allocate (buf%b(65536)) + if (buf%n >= size(buf%b)) then + allocate (t(2*size(buf%b))) + t(1:buf%n) = buf%b(1:buf%n) + call move_alloc(t, buf%b) + end if + buf%n = buf%n + 1 + buf%b(buf%n) = iand(v, 255) + end subroutine push + + ! Deflate is little endian at the bit level: the first bit of the stream + ! is the least significant bit of the first byte. + subroutine put_bits(buf, code, nb) + type(bitbuf_t), intent(inout) :: buf + integer, intent(in) :: code, nb + integer :: i + + do i = 0, nb - 1 + if (btest(code, i)) buf%acc = ibset(buf%acc, buf%nbits) + buf%nbits = buf%nbits + 1 + if (buf%nbits == 8) then + call push(buf, buf%acc) + buf%acc = 0 + buf%nbits = 0 + end if + end do + end subroutine put_bits + + ! Huffman codes are written most significant bit first, the opposite of + ! everything else in the stream, so they are reversed on the way out. + subroutine put_code(buf, code, nb) + type(bitbuf_t), intent(inout) :: buf + integer, intent(in) :: code, nb + integer :: i + + do i = nb - 1, 0, -1 + call put_bits(buf, merge(1, 0, btest(code, i)), 1) + end do + end subroutine put_code + + subroutine flush_bits(buf) + type(bitbuf_t), intent(inout) :: buf + + if (buf%nbits > 0) then + call push(buf, buf%acc) + buf%acc = 0 + buf%nbits = 0 + end if + end subroutine flush_bits + + ! ------------------------------------------------------------------ + ! Fixed Huffman alphabet, straight from RFC 1951 section 3.2.6. + ! ------------------------------------------------------------------ + + subroutine lit_code(sym, code, nb) + integer, intent(in) :: sym + integer, intent(out) :: code, nb + + if (sym <= 143) then + code = 48 + sym; nb = 8 + else if (sym <= 255) then + code = 400 + (sym - 144); nb = 9 + else if (sym <= 279) then + code = sym - 256; nb = 7 + else + code = 192 + (sym - 280); nb = 8 + end if + end subroutine lit_code + + subroutine emit_literal(buf, v) + type(bitbuf_t), intent(inout) :: buf + integer, intent(in) :: v + integer :: c, nb + + call lit_code(v, c, nb) + call put_code(buf, c, nb) + end subroutine emit_literal + + subroutine emit_match(buf, len, dist) + type(bitbuf_t), intent(inout) :: buf + integer, intent(in) :: len, dist + integer :: s, c, nb, d + + do s = 285, 257, -1 + if (len >= LEN_BASE(s)) exit + end do + if (s == 285 .and. len < 258) s = 284 + call lit_code(s, c, nb) + call put_code(buf, c, nb) + if (LEN_EXTRA(s) > 0) call put_bits(buf, len - LEN_BASE(s), LEN_EXTRA(s)) + + do d = 29, 0, -1 + if (dist >= DIST_BASE(d)) exit + end do + call put_code(buf, d, 5) + if (DIST_EXTRA(d) > 0) call put_bits(buf, dist - DIST_BASE(d), DIST_EXTRA(d)) + end subroutine emit_match + + ! ------------------------------------------------------------------ + ! Deflate: greedy LZ77 over a hash chain, fixed Huffman output. + ! ------------------------------------------------------------------ + + function deflate(src, n) result(out) + integer, intent(in) :: src(:), n + integer, allocatable :: out(:) + type(bitbuf_t) :: buf + integer, allocatable :: head(:), prev(:) + integer :: i, h, j, k, best_len, best_dist, cur, limit, mlen, nchain + + allocate (head(0:HSIZE - 1), prev(0:WSIZE - 1)) + head = 0 + prev = 0 + + call put_bits(buf, 1, 1) ! final block + call put_bits(buf, 1, 2) ! fixed Huffman + + i = 1 + do while (i <= n) + best_len = 0 + best_dist = 0 + if (i + MIN_MATCH - 1 <= n) then + h = hash3(src(i), src(i + 1), src(i + 2)) + cur = head(h) + nchain = 0 + do while (cur > 0 .and. nchain < CHAIN) + if (i - cur >= WSIZE) exit + limit = min(MAX_MATCH, n - i + 1) + mlen = 0 + do k = 0, limit - 1 + if (src(cur + k) /= src(i + k)) exit + mlen = mlen + 1 + end do + if (mlen > best_len) then + best_len = mlen + best_dist = i - cur + if (mlen >= MAX_MATCH) exit + end if + cur = prev(iand(cur, WSIZE - 1)) + nchain = nchain + 1 + end do + end if + + if (best_len >= MIN_MATCH) then + call emit_match(buf, best_len, best_dist) + do j = i, i + best_len - 1 + if (j + MIN_MATCH - 1 <= n) then + h = hash3(src(j), src(j + 1), src(j + 2)) + prev(iand(j, WSIZE - 1)) = head(h) + head(h) = j + end if + end do + i = i + best_len + else + call emit_literal(buf, src(i)) + if (i + MIN_MATCH - 1 <= n) then + h = hash3(src(i), src(i + 1), src(i + 2)) + prev(iand(i, WSIZE - 1)) = head(h) + head(h) = i + end if + i = i + 1 + end if + end do + + call emit_literal(buf, 256) ! end of block + call flush_bits(buf) + + allocate (out(buf%n)) + if (buf%n > 0) out = buf%b(1:buf%n) + end function deflate + + ! Zlib's rolling hash, folded over three bytes. Deliberately not a + ! multiplicative hash: the usual 2654435761 constant does not fit in a + ! default integer, and a shift-xor mix costs nothing here. + pure function hash3(a, b, c) result(h) + integer, intent(in) :: a, b, c + integer :: h + + h = iand(ieor(ishft(a, 5), b), HSIZE - 1) + h = iand(ieor(ishft(h, 5), c), HSIZE - 1) + end function hash3 + + ! ------------------------------------------------------------------ + ! Checksums + ! ------------------------------------------------------------------ + + function crc32(buf, n) result(c) + integer, intent(in) :: buf(:), n + integer :: c, i, k, tmp + + c = -1 + do i = 1, n + c = ieor(c, buf(i)) + do k = 1, 8 + tmp = iand(c, 1) + c = ishft(c, -1) + c = iand(c, 2147483647) ! keep the shift logical + if (tmp /= 0) c = ieor(c, -306674912) + end do + end do + c = not(c) + end function crc32 + + function adler32(buf, n) result(a) + integer, intent(in) :: buf(:), n + integer :: a, s1, s2, i + + s1 = 1 + s2 = 0 + do i = 1, n + s1 = mod(s1 + buf(i), 65521) + s2 = mod(s2 + s1, 65521) + end do + a = s2*65536 + s1 + end function adler32 + + ! ------------------------------------------------------------------ + ! Row filtering. PNG lets each row pick its own predictor; libpng's rule + ! of thumb is to take whichever leaves the smallest total magnitude, on + ! the grounds that small residuals are what deflate compresses best. + ! It matters here: a plot has many identical rows, and the Up filter + ! turns those into runs of zeros. + ! ------------------------------------------------------------------ + + pure function paeth(a, b, c) result(p) + integer, intent(in) :: a, b, c + integer :: p, pa, pb, pc + + pa = abs(b - c) + pb = abs(a - c) + pc = abs(a + b - 2*c) + if (pa <= pb .and. pa <= pc) then + p = a + else if (pb <= pc) then + p = b + else + p = c + end if + end function paeth + + ! ------------------------------------------------------------------ + ! Encode an RGBA buffer, row major from the top left, as PNG bytes. + ! ------------------------------------------------------------------ + + function png_encode(w, h, rgba) result(bytes) + integer, intent(in) :: w, h + integer, intent(in) :: rgba(4, w, h) + character(len=:), allocatable :: bytes + integer, allocatable :: raw(:), comp(:), cur(:), up(:), try(:), best(:) + integer :: bpp, stride, nraw, x, y, c, i, p, f, score, bscore, bf + integer :: a_, b_, c_, v + type(bitbuf_t) :: out + + bpp = 4 + stride = w*bpp + nraw = h*(stride + 1) + allocate (raw(nraw), cur(stride), up(stride), try(stride), best(stride)) + up = 0 + p = 0 + + do y = 1, h + i = 0 + do x = 1, w + do c = 1, 4 + i = i + 1 + cur(i) = iand(rgba(c, x, y), 255) + end do + end do + + bf = 0 + bscore = huge(0) + do f = 0, 4 + score = 0 + do i = 1, stride + a_ = 0 + if (i > bpp) a_ = cur(i - bpp) + b_ = up(i) + c_ = 0 + if (i > bpp) c_ = up(i - bpp) + select case (f) + case (0); v = cur(i) + case (1); v = cur(i) - a_ + case (2); v = cur(i) - b_ + case (3); v = cur(i) - (a_ + b_)/2 + case default; v = cur(i) - paeth(a_, b_, c_) + end select + v = iand(v, 255) + try(i) = v + if (v > 127) v = v - 256 + score = score + abs(v) + end do + if (score < bscore) then + bscore = score + bf = f + best = try + end if + end do + + p = p + 1 + raw(p) = bf + raw(p + 1:p + stride) = best(1:stride) + p = p + stride + up = cur + end do + + comp = deflate(raw, nraw) + + ! Signature + call push(out, 137); call push(out, 80); call push(out, 78) + call push(out, 71); call push(out, 13); call push(out, 10) + call push(out, 26); call push(out, 10) + + call ihdr(out, w, h) + call chunk(out, "IDAT", zlib_wrap(comp, raw, nraw)) + call chunk(out, "IEND", NO_DATA) + + allocate (character(len=out%n) :: bytes) + do i = 1, out%n + bytes(i:i) = achar(out%b(i)) + end do + end function png_encode + + function zlib_wrap(comp, raw, nraw) result(z) + integer, intent(in) :: comp(:), raw(:), nraw + integer, allocatable :: z(:) + integer :: n, a + + n = size(comp) + allocate (z(n + 6)) + z(1) = 120 ! deflate, 32K window + z(2) = 1 ! check bits, no preset dictionary + z(3:n + 2) = comp + a = adler32(raw, nraw) + z(n + 3) = iand(ishft(a, -24), 255) + z(n + 4) = iand(ishft(a, -16), 255) + z(n + 5) = iand(ishft(a, -8), 255) + z(n + 6) = iand(a, 255) + end function zlib_wrap + + subroutine ihdr(out, w, h) + type(bitbuf_t), intent(inout) :: out + integer, intent(in) :: w, h + integer :: d(13) + + call be32(w, d(1:4)) + call be32(h, d(5:8)) + d(9) = 8 ! bit depth + d(10) = 6 ! truecolor with alpha + d(11) = 0 ! deflate + d(12) = 0 ! adaptive filtering + d(13) = 0 ! no interlace + call chunk(out, "IHDR", d) + end subroutine ihdr + + pure subroutine be32(v, d) + integer, intent(in) :: v + integer, intent(out) :: d(4) + + d(1) = iand(ishft(v, -24), 255) + d(2) = iand(ishft(v, -16), 255) + d(3) = iand(ishft(v, -8), 255) + d(4) = iand(v, 255) + end subroutine be32 + + subroutine chunk(out, name, data) + type(bitbuf_t), intent(inout) :: out + character(len=4), intent(in) :: name + integer, intent(in) :: data(:) + integer :: d(4), i, n + integer, allocatable :: tc(:) + + n = size(data) + call be32(n, d) + do i = 1, 4 + call push(out, d(i)) + end do + allocate (tc(4 + n)) + do i = 1, 4 + tc(i) = iachar(name(i:i)) + call push(out, tc(i)) + end do + if (n > 0) tc(5:4 + n) = data + do i = 1, n + call push(out, data(i)) + end do + call be32(crc32(tc, 4 + n), d) + do i = 1, 4 + call push(out, d(i)) + end do + end subroutine chunk + +end module fplot_png diff --git a/src/fplot_raster.f90 b/src/fplot_raster.f90 new file mode 100644 index 0000000..51abf4d --- /dev/null +++ b/src/fplot_raster.f90 @@ -0,0 +1,721 @@ +! fplot_raster — an antialiased scanline rasterizer. +! +! This is the part PNG has that the vector backends get for free: something +! has to decide what colour each pixel is. It is kept separate from the PNG +! container and from the backend so that it can be reasoned about, and tested, +! as pure geometry. +! +! Coverage is computed by signed-area accumulation rather than by supersampling. +! Each edge deposits the exact area it cuts from every pixel it crosses into an +! accumulation buffer; a running sum along the row then turns those deltas into +! coverage. Two things recommend it. The antialiasing is analytic, so a shallow +! diagonal comes out smooth instead of stepping between a fixed number of +! sample levels, which is what matplotlib's Agg does and therefore what fplot +! has to do to look the same. And the running sum *is* the winding number, so +! the nonzero fill rule falls out of the algorithm rather than needing sorted +! crossing lists. Overlapping contours of the same orientation saturate to one +! and opposite orientations cancel, which is exactly nonzero. +! +! The catch, and the reason to say so plainly: this gives nonzero only. Even-odd +! would need a different accumulator. fplot never asks for it. +! +! Everything else reduces to filling a path. Strokes are converted to outlines +! and filled; text is glyph outlines and filled; markers are a shape filled once +! per point. There is one rasterizer here, not five. + +module fplot_raster + use fplot_style, only: dp + use fplot_render, only: paint_t, clip_t, VERB_MOVE, VERB_LINE, VERB_CUBIC, & + VERB_CLOSE, CAP_BUTT, CAP_ROUND, CAP_SQUARE, & + JOIN_MITER, JOIN_ROUND, JOIN_BEVEL, MAX_DASH + implicit none + private + + public :: canvas_t, pathbuf_t + public :: canvas_init, canvas_rgba, canvas_free + public :: pb_reset, pb_move, pb_line, pb_cubic, pb_close, pb_poly + public :: pb_should_snap, pb_snap + public :: fill_path_buf, stroke_path_buf, blit_image + + ! A path flattened to polylines: every curve is already line segments, so + ! the rasterizer never sees a Bezier. + type :: pathbuf_t + real(dp), allocatable :: x(:), y(:) + integer, allocatable :: sub_beg(:), sub_len(:) + logical, allocatable :: sub_closed(:) + integer :: n = 0, ns = 0 + end type pathbuf_t + + type :: canvas_t + integer :: w = 0, h = 0 + real(dp) :: scale = 1.0_dp ! pixels per point + real(dp), allocatable :: px(:, :, :) ! (4, w, h), premultiplied + real(dp), allocatable :: acc(:, :) ! (0:w, h) area deltas + end type canvas_t + +contains + + ! ------------------------------------------------------------------ + ! Canvas + ! ------------------------------------------------------------------ + + subroutine canvas_init(cv, w, h, scale, bg_rgb, bg_alpha) + type(canvas_t), intent(out) :: cv + integer, intent(in) :: w, h + real(dp), intent(in) :: scale + integer, intent(in) :: bg_rgb(3) + real(dp), intent(in) :: bg_alpha + integer :: c + + cv%w = w + cv%h = h + cv%scale = scale + allocate (cv%px(4, w, h)) + allocate (cv%acc(0:w, h)) + cv%acc = 0.0_dp + ! Stored premultiplied, so the background is scaled by its own alpha. + do c = 1, 3 + cv%px(c, :, :) = (real(bg_rgb(c), dp)/255.0_dp)*bg_alpha + end do + cv%px(4, :, :) = bg_alpha + end subroutine canvas_init + + subroutine canvas_free(cv) + type(canvas_t), intent(inout) :: cv + + if (allocated(cv%px)) deallocate (cv%px) + if (allocated(cv%acc)) deallocate (cv%acc) + end subroutine canvas_free + + ! Undo the premultiplication on the way out, because PNG stores straight + ! alpha. Fully transparent pixels have no colour to recover and are left + ! black, which is what every other encoder does. + function canvas_rgba(cv) result(out) + type(canvas_t), intent(in) :: cv + integer, allocatable :: out(:, :, :) + integer :: x, y, c + real(dp) :: a, v + + allocate (out(4, cv%w, cv%h)) + do y = 1, cv%h + do x = 1, cv%w + a = cv%px(4, x, y) + if (a <= 0.0_dp) then + out(:, x, y) = 0 + else + do c = 1, 3 + v = cv%px(c, x, y)/a + out(c, x, y) = nint(min(1.0_dp, max(0.0_dp, v))*255.0_dp) + end do + out(4, x, y) = nint(min(1.0_dp, a)*255.0_dp) + end if + end do + end do + end function canvas_rgba + + ! ------------------------------------------------------------------ + ! Path building. Coordinates go in as points and come out as pixels, so + ! the rest of the module never has to think about the scale factor. + ! ------------------------------------------------------------------ + + subroutine pb_reset(pb) + type(pathbuf_t), intent(inout) :: pb + + pb%n = 0 + pb%ns = 0 + if (.not. allocated(pb%x)) then + allocate (pb%x(256), pb%y(256)) + allocate (pb%sub_beg(32), pb%sub_len(32), pb%sub_closed(32)) + end if + end subroutine pb_reset + + subroutine pb_grow(pb) + type(pathbuf_t), intent(inout) :: pb + real(dp), allocatable :: t(:) + + if (pb%n < size(pb%x)) return + allocate (t(2*size(pb%x))) + t(1:pb%n) = pb%x(1:pb%n); call move_alloc(t, pb%x) + allocate (t(2*size(pb%y))) + t(1:pb%n) = pb%y(1:pb%n); call move_alloc(t, pb%y) + end subroutine pb_grow + + subroutine pb_grow_sub(pb) + type(pathbuf_t), intent(inout) :: pb + integer, allocatable :: t(:) + logical, allocatable :: l(:) + + if (pb%ns < size(pb%sub_beg)) return + allocate (t(2*size(pb%sub_beg))) + t(1:pb%ns) = pb%sub_beg(1:pb%ns); call move_alloc(t, pb%sub_beg) + allocate (t(2*size(pb%sub_len))) + t(1:pb%ns) = pb%sub_len(1:pb%ns); call move_alloc(t, pb%sub_len) + allocate (l(2*size(pb%sub_closed))) + l(1:pb%ns) = pb%sub_closed(1:pb%ns); call move_alloc(l, pb%sub_closed) + end subroutine pb_grow_sub + + subroutine pb_move(pb, x, y) + type(pathbuf_t), intent(inout) :: pb + real(dp), intent(in) :: x, y + + call pb_grow(pb) + call pb_grow_sub(pb) + pb%n = pb%n + 1 + pb%x(pb%n) = x + pb%y(pb%n) = y + pb%ns = pb%ns + 1 + pb%sub_beg(pb%ns) = pb%n + pb%sub_len(pb%ns) = 1 + pb%sub_closed(pb%ns) = .false. + end subroutine pb_move + + subroutine pb_line(pb, x, y) + type(pathbuf_t), intent(inout) :: pb + real(dp), intent(in) :: x, y + + if (pb%ns == 0) then + call pb_move(pb, x, y) + return + end if + call pb_grow(pb) + pb%n = pb%n + 1 + pb%x(pb%n) = x + pb%y(pb%n) = y + pb%sub_len(pb%ns) = pb%sub_len(pb%ns) + 1 + end subroutine pb_line + + subroutine pb_close(pb) + type(pathbuf_t), intent(inout) :: pb + + if (pb%ns > 0) pb%sub_closed(pb%ns) = .true. + end subroutine pb_close + + ! Flatten a cubic. The segment count follows the control polygon length so + ! that a big sweeping curve gets more segments than a marker-sized one; the + ! ceiling keeps a pathological control net from costing thousands. + subroutine pb_cubic(pb, x1, y1, x2, y2, x3, y3) + type(pathbuf_t), intent(inout) :: pb + real(dp), intent(in) :: x1, y1, x2, y2, x3, y3 + real(dp) :: x0, y0, t, mt, d + integer :: i, n + + if (pb%ns == 0) call pb_move(pb, x1, y1) + x0 = pb%x(pb%n) + y0 = pb%y(pb%n) + d = hypot(x1 - x0, y1 - y0) + hypot(x2 - x1, y2 - y1) + hypot(x3 - x2, y3 - y2) + n = max(3, min(128, ceiling(d*0.6_dp))) + do i = 1, n + t = real(i, dp)/real(n, dp) + mt = 1.0_dp - t + call pb_line(pb, & + mt**3*x0 + 3.0_dp*mt*mt*t*x1 + 3.0_dp*mt*t*t*x2 + t**3*x3, & + mt**3*y0 + 3.0_dp*mt*mt*t*y1 + 3.0_dp*mt*t*t*y2 + t**3*y3) + end do + end subroutine pb_cubic + + ! Append a closed polygon, forcing the orientation. Nonzero winding unions + ! shapes only when they turn the same way, and a stroke is built by piling + ! quads and join wedges on top of each other, so every piece has to be + ! wound the same way or the overlaps would cancel into holes. + subroutine pb_poly(pb, px, py, n, negative) + type(pathbuf_t), intent(inout) :: pb + real(dp), intent(in) :: px(:), py(:) + integer, intent(in) :: n + logical, intent(in) :: negative + real(dp) :: a + integer :: i, j + + if (n < 3) return + a = 0.0_dp + do i = 1, n + j = merge(1, i + 1, i == n) + a = a + px(i)*py(j) - px(j)*py(i) + end do + + if ((a < 0.0_dp) .eqv. negative) then + call pb_move(pb, px(1), py(1)) + do i = 2, n + call pb_line(pb, px(i), py(i)) + end do + else + call pb_move(pb, px(n), py(n)) + do i = n - 1, 1, -1 + call pb_line(pb, px(i), py(i)) + end do + end if + call pb_close(pb) + end subroutine pb_poly + + ! ------------------------------------------------------------------ + ! Pixel snapping. + ! + ! A one point gridline landing across two pixel columns comes out as two + ! half-dark columns, which reads as blurred rather than thin. Matplotlib + ! avoids this by rounding paths made only of horizontal and vertical + ! segments onto the pixel grid, and since fplot is trying to look like + ! matplotlib it has to do the same thing by the same rule: axis spines, + ! gridlines, ticks and rectangles are exactly the paths this catches. + ! + ! Curved or diagonal paths are left alone, because snapping them would + ! distort the shape for no gain. + ! ------------------------------------------------------------------ + + function pb_should_snap(pb) result(yes) + type(pathbuf_t), intent(in) :: pb + logical :: yes + integer :: s, b, n, i + + yes = .false. + if (pb%n < 2 .or. pb%n > 1024) return + do s = 1, pb%ns + b = pb%sub_beg(s) + n = pb%sub_len(s) + do i = b, b + n - 2 + if (abs(pb%x(i) - pb%x(i + 1)) >= 1.0e-4_dp .and. & + abs(pb%y(i) - pb%y(i + 1)) >= 1.0e-4_dp) return + end do + end do + yes = .true. + end function pb_should_snap + + ! An odd number of pixels of width is centred on a pixel centre and an + ! even number on a pixel boundary; that is the whole of the offset rule. + subroutine pb_snap(pb, line_width_px) + type(pathbuf_t), intent(inout) :: pb + real(dp), intent(in) :: line_width_px + real(dp) :: sv + integer :: i + + sv = 0.0_dp + if (modulo(nint(line_width_px), 2) == 1) sv = 0.5_dp + do i = 1, pb%n + pb%x(i) = floor(pb%x(i) + 0.5_dp) + sv + pb%y(i) = floor(pb%y(i) + 0.5_dp) + sv + end do + end subroutine pb_snap + + ! ------------------------------------------------------------------ + ! The accumulator. + ! + ! One edge, deposited as area deltas. Adapted from the standard signed + ! area method: for each scanline the edge crosses, work out how much of + ! each pixel lies to its left and record the difference from the pixel + ! before. x is clamped into the canvas, which is exact rather than + ! approximate: geometry off the left edge contributes full winding to + ! everything to its right, and that is what clamping to column zero says. + ! ------------------------------------------------------------------ + + subroutine acc_edge(cv, ax, ay, bx, by, y0b, y1b) + type(canvas_t), intent(inout) :: cv + real(dp), intent(in) :: ax, ay, bx, by + integer, intent(inout) :: y0b, y1b + real(dp) :: x0, y0, x1, y1, dir, dxdy, x, xn, dy, d, xa, xb + real(dp) :: s, x0f, x1f, a0, am, a1, a2, xmf, x0fl, x1cl + integer :: y, yi0, yi1, i0, i1, xi + + if (abs(ay - by) < 1.0e-12_dp) return + if (ay < by) then + x0 = ax; y0 = ay; x1 = bx; y1 = by; dir = 1.0_dp + else + x0 = bx; y0 = by; x1 = ax; y1 = ay; dir = -1.0_dp + end if + if (y1 <= 0.0_dp .or. y0 >= real(cv%h, dp)) return + + dxdy = (x1 - x0)/(y1 - y0) + x = x0 + if (y0 < 0.0_dp) x = x - y0*dxdy + yi0 = max(0, floor(y0)) + yi1 = min(cv%h, ceiling(y1)) + y0b = min(y0b, yi0 + 1) + y1b = max(y1b, yi1) + + do y = yi0, yi1 - 1 + dy = min(real(y + 1, dp), y1) - max(real(y, dp), y0) + if (dy <= 0.0_dp) cycle + xn = x + dxdy*dy + d = dy*dir + xa = min(x, xn) + xb = max(x, xn) + xa = min(max(xa, 0.0_dp), real(cv%w, dp)) + xb = min(max(xb, 0.0_dp), real(cv%w, dp)) + x0fl = real(floor(xa), dp) + x1cl = real(ceiling(xb), dp) + i0 = int(x0fl) + i1 = int(x1cl) + if (i0 > cv%w) i0 = cv%w + if (i1 > cv%w) i1 = cv%w + + if (i1 <= i0 + 1) then + ! Narrower than a pixel: split the area between two columns. + xmf = 0.5_dp*(xa + xb) - x0fl + cv%acc(i0, y + 1) = cv%acc(i0, y + 1) + d*(1.0_dp - xmf) + if (i0 + 1 <= cv%w) & + cv%acc(i0 + 1, y + 1) = cv%acc(i0 + 1, y + 1) + d*xmf + else + s = 1.0_dp/(xb - xa) + x0f = xa - x0fl + a0 = 0.5_dp*s*(1.0_dp - x0f)**2 + x1f = xb - x1cl + 1.0_dp + am = 0.5_dp*s*x1f**2 + cv%acc(i0, y + 1) = cv%acc(i0, y + 1) + d*a0 + if (i1 == i0 + 2) then + cv%acc(i0 + 1, y + 1) = cv%acc(i0 + 1, y + 1) + d*(1.0_dp - a0 - am) + else + a1 = s*(1.5_dp - x0f) + cv%acc(i0 + 1, y + 1) = cv%acc(i0 + 1, y + 1) + d*(a1 - a0) + do xi = i0 + 2, i1 - 2 + cv%acc(xi, y + 1) = cv%acc(xi, y + 1) + d*s + end do + a2 = a1 + real(i1 - i0 - 3, dp)*s + cv%acc(i1 - 1, y + 1) = cv%acc(i1 - 1, y + 1) + d*(1.0_dp - a2 - am) + end if + if (i1 <= cv%w) cv%acc(i1, y + 1) = cv%acc(i1, y + 1) + d*am + end if + x = xn + end do + end subroutine acc_edge + + ! ------------------------------------------------------------------ + ! Fill a flattened path. Subpaths are treated as closed whether or not + ! they were declared so, because an unclosed fill is meaningless and every + ! renderer closes it implicitly. + ! ------------------------------------------------------------------ + + subroutine fill_path_buf(cv, pb, rgb, alpha, clip) + type(canvas_t), intent(inout) :: cv + type(pathbuf_t), intent(in) :: pb + integer, intent(in) :: rgb(3) + real(dp), intent(in) :: alpha + type(clip_t), intent(in) :: clip + integer :: s, i, b, n, y, x, cx0, cx1, cy0, cy1, y0b, y1b + real(dp) :: run, cov, sa, sr, sg, sb_, ia + + if (pb%ns == 0 .or. alpha <= 0.0_dp) return + + y0b = cv%h + 1 + y1b = 0 + do s = 1, pb%ns + b = pb%sub_beg(s) + n = pb%sub_len(s) + if (n < 2) cycle + do i = b, b + n - 2 + call acc_edge(cv, pb%x(i), pb%y(i), pb%x(i + 1), pb%y(i + 1), y0b, y1b) + end do + call acc_edge(cv, pb%x(b + n - 1), pb%y(b + n - 1), pb%x(b), pb%y(b), y0b, y1b) + end do + if (y1b < y0b) return + + call clip_box(cv, clip, cx0, cx1, cy0, cy1) + sr = real(rgb(1), dp)/255.0_dp + sg = real(rgb(2), dp)/255.0_dp + sb_ = real(rgb(3), dp)/255.0_dp + + do y = y0b, y1b + run = 0.0_dp + do x = 1, cv%w + run = run + cv%acc(x - 1, y) + cv%acc(x - 1, y) = 0.0_dp + if (y < cy0 .or. y > cy1 .or. x < cx0 .or. x > cx1) cycle + cov = min(1.0_dp, abs(run)) + if (cov <= 0.0_dp) cycle + sa = cov*alpha + ia = 1.0_dp - sa + cv%px(1, x, y) = sr*sa + cv%px(1, x, y)*ia + cv%px(2, x, y) = sg*sa + cv%px(2, x, y)*ia + cv%px(3, x, y) = sb_*sa + cv%px(3, x, y)*ia + cv%px(4, x, y) = sa + cv%px(4, x, y)*ia + end do + cv%acc(cv%w, y) = 0.0_dp + end do + end subroutine fill_path_buf + + subroutine clip_box(cv, clip, x0, x1, y0, y1) + type(canvas_t), intent(in) :: cv + type(clip_t), intent(in) :: clip + integer, intent(out) :: x0, x1, y0, y1 + + if (clip%on) then + x0 = max(1, nint(clip%x*cv%scale) + 1) + y0 = max(1, nint(clip%y*cv%scale) + 1) + x1 = min(cv%w, nint((clip%x + clip%w)*cv%scale)) + y1 = min(cv%h, nint((clip%y + clip%h)*cv%scale)) + else + x0 = 1; y0 = 1; x1 = cv%w; y1 = cv%h + end if + end subroutine clip_box + + ! ------------------------------------------------------------------ + ! Stroking: turn a centreline into an outline and fill it. + ! + ! Rather than compute one exact offset polygon, which needs self + ! intersection handling, each segment contributes a quad and each joint a + ! wedge, all wound the same way. Nonzero winding then unions them for + ! free. This is both far shorter and more robust than offsetting, and the + ! result is identical wherever the pen is round or the corner convex. + ! ------------------------------------------------------------------ + + subroutine stroke_path_buf(cv, pb, paint) + type(canvas_t), intent(inout) :: cv + type(pathbuf_t), intent(in) :: pb + type(paint_t), intent(in) :: paint + type(pathbuf_t) :: out + real(dp), allocatable :: dx(:), dy(:) + integer :: s, b, n, nd + real(dp) :: hw + + hw = 0.5_dp*paint%line_width*cv%scale + if (hw <= 0.0_dp) return + call pb_reset(out) + + do s = 1, pb%ns + b = pb%sub_beg(s) + n = pb%sub_len(s) + if (n < 1) cycle + call gather(pb, b, n, pb%sub_closed(s), dx, dy, nd) + if (paint%n_dash > 0) then + call stroke_dashed(out, dx, dy, nd, hw, paint, cv%scale) + else + call stroke_run(out, dx, dy, nd, hw, paint, pb%sub_closed(s)) + end if + end do + + call fill_path_buf(cv, out, paint%stroke_rgb, paint%stroke_alpha, paint%clip) + end subroutine stroke_path_buf + + subroutine gather(pb, b, n, closed, dx, dy, nd) + type(pathbuf_t), intent(in) :: pb + integer, intent(in) :: b, n + logical, intent(in) :: closed + real(dp), allocatable, intent(out) :: dx(:), dy(:) + integer, intent(out) :: nd + + nd = n + if (closed) nd = n + 1 + allocate (dx(nd), dy(nd)) + dx(1:n) = pb%x(b:b + n - 1) + dy(1:n) = pb%y(b:b + n - 1) + if (closed) then + dx(nd) = pb%x(b) + dy(nd) = pb%y(b) + end if + end subroutine gather + + ! Walk the polyline handing out alternating on and off runs of the dash + ! pattern. Each on run is stroked as its own open subpath, which is why + ! dash caps are applied per dash and not per line. + subroutine stroke_dashed(out, px, py, n, hw, paint, scale) + type(pathbuf_t), intent(inout) :: out + real(dp), intent(in) :: px(:), py(:), hw, scale + integer, intent(in) :: n + type(paint_t), intent(in) :: paint + real(dp) :: rem, seg, t0, t1, ex, ey, sx, sy, l + real(dp), allocatable :: rx(:), ry(:) + integer :: i, di, nr + logical :: on + + allocate (rx(n + 4), ry(n + 4)) + di = 1 + rem = paint%dash(1)*scale + on = .true. + nr = 0 + + do i = 1, n - 1 + sx = px(i); sy = py(i) + l = hypot(px(i + 1) - sx, py(i + 1) - sy) + if (l <= 0.0_dp) cycle + t0 = 0.0_dp + do while (t0 < l) + seg = min(rem, l - t0) + t1 = t0 + seg + ex = sx + (px(i + 1) - sx)*(t1/l) + ey = sy + (py(i + 1) - sy)*(t1/l) + if (on) then + if (nr == 0) then + nr = 1 + rx(1) = sx + (px(i + 1) - sx)*(t0/l) + ry(1) = sy + (py(i + 1) - sy)*(t0/l) + end if + nr = nr + 1 + rx(nr) = ex + ry(nr) = ey + end if + rem = rem - seg + t0 = t1 + if (rem <= 1.0e-12_dp) then + if (on .and. nr > 1) then + call stroke_run(out, rx, ry, nr, hw, paint, .false.) + nr = 0 + end if + on = .not. on + di = di + 1 + if (di > paint%n_dash) di = 1 + rem = paint%dash(di)*scale + nr = 0 + end if + end do + end do + if (on .and. nr > 1) call stroke_run(out, rx, ry, nr, hw, paint, .false.) + end subroutine stroke_dashed + + subroutine stroke_run(out, px, py, n, hw, paint, closed) + type(pathbuf_t), intent(inout) :: out + real(dp), intent(in) :: px(:), py(:), hw + integer, intent(in) :: n + type(paint_t), intent(in) :: paint + logical, intent(in) :: closed + real(dp) :: qx(4), qy(4), ux, uy, nx, ny, l + integer :: i + + ! A degenerate run still shows as a dot under a round pen, which is + ! how a zero length dash draws a point. + if (n < 2) then + if (n == 1 .and. paint%cap == CAP_ROUND) call add_disc(out, px(1), py(1), hw) + return + end if + + do i = 1, n - 1 + ux = px(i + 1) - px(i) + uy = py(i + 1) - py(i) + l = hypot(ux, uy) + if (l <= 0.0_dp) cycle + ux = ux/l; uy = uy/l + nx = -uy*hw; ny = ux*hw + qx(1) = px(i) + nx; qy(1) = py(i) + ny + qx(2) = px(i + 1) + nx; qy(2) = py(i + 1) + ny + qx(3) = px(i + 1) - nx; qy(3) = py(i + 1) - ny + qx(4) = px(i) - nx; qy(4) = py(i) - ny + if (paint%cap == CAP_SQUARE .and. .not. closed) then + if (i == 1) then + qx(1) = qx(1) - ux*hw; qy(1) = qy(1) - uy*hw + qx(4) = qx(4) - ux*hw; qy(4) = qy(4) - uy*hw + end if + if (i == n - 1) then + qx(2) = qx(2) + ux*hw; qy(2) = qy(2) + uy*hw + qx(3) = qx(3) + ux*hw; qy(3) = qy(3) + uy*hw + end if + end if + call pb_poly(out, qx, qy, 4, .true.) + end do + + do i = 2, n - 1 + call add_join(out, px(i - 1), py(i - 1), px(i), py(i), & + px(i + 1), py(i + 1), hw, paint%join, paint%miter_limit) + end do + if (closed .and. n > 2) then + call add_join(out, px(n - 1), py(n - 1), px(1), py(1), px(2), py(2), & + hw, paint%join, paint%miter_limit) + else if (paint%cap == CAP_ROUND) then + call add_disc(out, px(1), py(1), hw) + call add_disc(out, px(n), py(n), hw) + end if + end subroutine stroke_run + + ! The wedge that fills the outer side of a corner. The inner side needs + ! nothing: the two segment quads already overlap there. + ! + ! With n1 and n2 the outward unit normals, the two offset edges meet at + ! b + v*2*hw/|v|**2 where v = n1 + n2, since |v| is twice the cosine of the + ! half angle. That ratio 2/|v| is also the miter length in pen widths, + ! which is exactly what the miter limit bounds, so it is tested directly + ! and the join falls back to a bevel when the corner is too sharp. + subroutine add_join(out, ax, ay, bx, by, cx, cy, hw, join, miter_limit) + type(pathbuf_t), intent(inout) :: out + real(dp), intent(in) :: ax, ay, bx, by, cx, cy, hw, miter_limit + integer, intent(in) :: join + real(dp) :: u1x, u1y, u2x, u2y, l1, l2, cross, s, vx, vy, vl + real(dp) :: n1x, n1y, n2x, n2y + real(dp) :: tx(4), ty(4) + + u1x = bx - ax; u1y = by - ay + l1 = hypot(u1x, u1y) + u2x = cx - bx; u2y = cy - by + l2 = hypot(u2x, u2y) + if (l1 <= 0.0_dp .or. l2 <= 0.0_dp) return + u1x = u1x/l1; u1y = u1y/l1 + u2x = u2x/l2; u2y = u2y/l2 + cross = u1x*u2y - u1y*u2x + if (abs(cross) < 1.0e-12_dp) return + + if (join == JOIN_ROUND) then + call add_disc(out, bx, by, hw) + return + end if + + s = -sign(1.0_dp, cross) + n1x = s*(-u1y); n1y = s*u1x + n2x = s*(-u2y); n2y = s*u2x + + tx(1) = bx; ty(1) = by + tx(2) = bx + n1x*hw; ty(2) = by + n1y*hw + + if (join == JOIN_MITER) then + vx = n1x + n2x + vy = n1y + n2y + vl = hypot(vx, vy) + if (vl > 1.0e-12_dp .and. 2.0_dp/vl <= miter_limit) then + tx(3) = bx + vx*2.0_dp*hw/(vl*vl) + ty(3) = by + vy*2.0_dp*hw/(vl*vl) + tx(4) = bx + n2x*hw; ty(4) = by + n2y*hw + call pb_poly(out, tx, ty, 4, .true.) + return + end if + end if + + tx(3) = bx + n2x*hw; ty(3) = by + n2y*hw + call pb_poly(out, tx(1:3), ty(1:3), 3, .true.) + end subroutine add_join + + subroutine add_disc(out, cx, cy, r) + type(pathbuf_t), intent(inout) :: out + real(dp), intent(in) :: cx, cy, r + real(dp), allocatable :: px(:), py(:) + real(dp), parameter :: TWOPI = 6.283185307179586_dp + integer :: i, n + + n = max(8, min(64, ceiling(r*4.0_dp))) + allocate (px(n), py(n)) + do i = 1, n + px(i) = cx + r*cos(TWOPI*real(i - 1, dp)/real(n, dp)) + py(i) = cy + r*sin(TWOPI*real(i - 1, dp)/real(n, dp)) + end do + call pb_poly(out, px, py, n, .true.) + end subroutine add_disc + + ! ------------------------------------------------------------------ + ! Images. Nearest neighbour, which is what imshow asks for by default and + ! the only sampling that leaves data values visibly untouched. + ! ------------------------------------------------------------------ + + subroutine blit_image(cv, x, y, w, h, rgba, nx, ny, alpha, clip) + type(canvas_t), intent(inout) :: cv + real(dp), intent(in) :: x, y, w, h, alpha + integer, intent(in) :: nx, ny + integer, intent(in) :: rgba(4, nx, ny) + type(clip_t), intent(in) :: clip + integer :: px0, px1, py0, py1, ix, iy, sx, sy, cx0, cx1, cy0, cy1 + real(dp) :: sa, ia, u, v + + call clip_box(cv, clip, cx0, cx1, cy0, cy1) + px0 = max(cx0, nint(x*cv%scale) + 1) + py0 = max(cy0, nint(y*cv%scale) + 1) + px1 = min(cx1, nint((x + w)*cv%scale)) + py1 = min(cy1, nint((y + h)*cv%scale)) + + do iy = py0, py1 + v = (real(iy, dp) - 0.5_dp)/cv%scale - y + sy = min(ny, max(1, int(v/h*real(ny, dp)) + 1)) + do ix = px0, px1 + u = (real(ix, dp) - 0.5_dp)/cv%scale - x + sx = min(nx, max(1, int(u/w*real(nx, dp)) + 1)) + sa = alpha*real(rgba(4, sx, sy), dp)/255.0_dp + if (sa <= 0.0_dp) cycle + ia = 1.0_dp - sa + cv%px(1, ix, iy) = real(rgba(1, sx, sy), dp)/255.0_dp*sa + cv%px(1, ix, iy)*ia + cv%px(2, ix, iy) = real(rgba(2, sx, sy), dp)/255.0_dp*sa + cv%px(2, ix, iy)*ia + cv%px(3, ix, iy) = real(rgba(3, sx, sy), dp)/255.0_dp*sa + cv%px(3, ix, iy)*ia + cv%px(4, ix, iy) = sa + cv%px(4, ix, iy)*ia + end do + end do + end subroutine blit_image + +end module fplot_raster diff --git a/src/fplot_render.f90 b/src/fplot_render.f90 new file mode 100644 index 0000000..ab44411 --- /dev/null +++ b/src/fplot_render.f90 @@ -0,0 +1,310 @@ +! fplot_render — the backend rendering API. +! +! One abstract renderer that SVG, PNG, PDF and EPS each implement. The plotting +! code above it never learns which one it is talking to, so a new output format +! is a new module here and nothing else. +! +! Four rules keep the API from leaking any one format's habits: +! +! 1. No state machine. Every drawing call carries the paint it should be +! drawn with, so nothing depends on what was called before it. A backend +! may cache the last paint internally to avoid re-emitting attributes, +! but that is its own business and cannot be observed through the API. +! +! 2. Purely additive, in painter's order. Calls are composited in the order +! they arrive; later drawings cover earlier ones. A raster backend can +! therefore rasterize each call immediately and never has to keep a +! display list. Only the container has to wait: PNG deflates, PDF writes +! its xref and SVG closes its root element in close_canvas. +! +! 3. One coordinate system, fixed here rather than negotiated. Points +! (1/72 inch), origin at the top left, x right, y down, angles in degrees +! clockwise. PDF and EPS flip this internally; PNG scales by its own dpi. +! Callers never ask which way is up. +! +! 4. Geometry is reduced to what all four can express: straight lines and +! cubic Beziers. There is no arc primitive, because only SVG has one; +! the front end flattens its pie wedges into cubics that every backend +! can already draw. +! +! What counts as a primitive +! -------------------------- +! A call earns a place in the backend contract only when a backend has a real +! decision to make about it. Those are all deferred, so the compiler tells a +! backend author exactly what has to be written: +! +! draw_path the universal fallback shape +! draw_rect SVG , PDF "re", EPS rectfill, PNG an exact axis +! aligned span fill that needs no scanline work at all +! draw_circle SVG , EPS "arc", PNG an exact antialiased disc; +! PDF alone has to approximate with four cubics +! draw_markers one shape stamped at many points: SVG plus , +! PDF a form XObject, EPS a defined procedure, PNG a cached +! sprite. Matplotlib emits 3330 elements across our +! reference figures, so this is where scatter plots are won +! draw_text see the note on text below +! draw_image a raster block, for imshow +! +! Anything a backend would only ever implement one way is not a primitive, and +! it is not in this file at all. Lines, polylines and polygons are just paths, +! so the front end builds the path and calls draw_path; there is nothing for a +! backend to decide and so nothing to declare. Matplotlib goes further still +! and has draw_path as its only geometry primitive; its SVG backend emits no +! or at all, trading file size for a smaller contract. +! +! By the same rule this module is interface and nothing else. Text metrics, +! arc flattening and circle-to-cubic conversion are all absent: either a +! backend has a real reason to do them its own way, in which case that is the +! backend's own code, or it does not, in which case it belongs to whoever +! calls the API rather than to the API. +! +! begin_group and end_group are different again: they are concrete and default +! to doing nothing, because doing nothing is the correct implementation for a +! format without a document tree, not a lazy one. See the note on groups. + +module fplot_render + use fplot_style, only: dp + implicit none + private + + public :: renderer_t, paint_t, font_t, clip_t + public :: VERB_MOVE, VERB_LINE, VERB_CUBIC, VERB_CLOSE + public :: CAP_BUTT, CAP_ROUND, CAP_SQUARE + public :: JOIN_MITER, JOIN_ROUND, JOIN_BEVEL + public :: FILL_NONZERO, FILL_EVENODD + public :: ANCHOR_START, ANCHOR_MIDDLE, ANCHOR_END + public :: BASE_ALPHABETIC, BASE_MIDDLE, BASE_TOP, BASE_BOTTOM + public :: FAMILY_SANS, FAMILY_SERIF, FAMILY_MONO + public :: WEIGHT_NORMAL, WEIGHT_BOLD, SLANT_ROMAN, SLANT_ITALIC + public :: MAX_DASH + + ! Path verbs. A path is x/y vertex arrays plus one verb per vertex group: + ! MOVE and LINE consume one vertex, CUBIC consumes three (two controls and + ! an endpoint), CLOSE consumes none. + integer, parameter :: VERB_MOVE = 1 + integer, parameter :: VERB_LINE = 2 + integer, parameter :: VERB_CUBIC = 3 + integer, parameter :: VERB_CLOSE = 4 + + integer, parameter :: CAP_BUTT = 0, CAP_ROUND = 1, CAP_SQUARE = 2 + integer, parameter :: JOIN_MITER = 0, JOIN_ROUND = 1, JOIN_BEVEL = 2 + integer, parameter :: FILL_NONZERO = 0, FILL_EVENODD = 1 + + integer, parameter :: ANCHOR_START = 0, ANCHOR_MIDDLE = 1, ANCHOR_END = 2 + integer, parameter :: BASE_ALPHABETIC = 0, BASE_MIDDLE = 1 + integer, parameter :: BASE_TOP = 2, BASE_BOTTOM = 3 + + ! A closed set, not a font name string. PDF has to map these onto embedded + ! or core fonts and PNG onto compiled-in glyph outlines, and neither can + ! honour an arbitrary family the user invents. + integer, parameter :: FAMILY_SANS = 0, FAMILY_SERIF = 1, FAMILY_MONO = 2 + integer, parameter :: WEIGHT_NORMAL = 0, WEIGHT_BOLD = 1 + integer, parameter :: SLANT_ROMAN = 0, SLANT_ITALIC = 1 + + integer, parameter :: MAX_DASH = 8 + + ! Rectangular clip. fplot only ever clips series to the axes box, and a + ! rectangle is the one clip shape all four formats support cheaply. + ! Carried inside the paint so that drawing stays stateless; a backend is + ! free to notice a run of identical clips and emit one SVG for them. + type :: clip_t + logical :: on = .false. + real(dp) :: x = 0.0_dp, y = 0.0_dp, w = 0.0_dp, h = 0.0_dp + end type clip_t + + ! Everything needed to draw one thing, passed by value. This is the + ! equivalent of a graphics pipeline object in a modern GPU API: build it + ! once, hand it to many draw calls, and no call can disturb another. + ! + ! Colors are numeric, not "#rrggbb": a rasterizer needs the components + ! and only the SVG backend wants the hex spelling. + type :: paint_t + logical :: filled = .false. + integer :: fill_rgb(3) = [0, 0, 0] + real(dp) :: fill_alpha = 1.0_dp + integer :: fill_rule = FILL_NONZERO + + logical :: stroked = .false. + integer :: stroke_rgb(3) = [0, 0, 0] + real(dp) :: stroke_alpha = 1.0_dp + real(dp) :: line_width = 1.0_dp + integer :: cap = CAP_BUTT + integer :: join = JOIN_MITER + real(dp) :: miter_limit = 10.0_dp + + ! Dash pattern in points, empty means solid. + integer :: n_dash = 0 + real(dp) :: dash(MAX_DASH) = 0.0_dp + real(dp) :: dash_offset = 0.0_dp + + type(clip_t) :: clip + end type paint_t + + type :: font_t + real(dp) :: size = 10.0_dp + integer :: family = FAMILY_SANS + integer :: weight = WEIGHT_NORMAL + integer :: slant = SLANT_ROMAN + end type font_t + + type, abstract :: renderer_t + real(dp) :: width = 0.0_dp ! canvas size in points + real(dp) :: height = 0.0_dp + logical :: is_open = .false. + contains + ! Lifecycle. + procedure(open_canvas_i), deferred :: open_canvas + procedure(close_canvas_i), deferred :: close_canvas + procedure(bytes_i), deferred :: bytes + + ! The primitives. Every one is a decision a backend has to make for + ! itself, which is why none of them has a default. + procedure(draw_path_i), deferred :: draw_path + procedure(draw_rect_i), deferred :: draw_rect + procedure(draw_circle_i), deferred :: draw_circle + procedure(draw_markers_i), deferred :: draw_markers + procedure(draw_text_i), deferred :: draw_text + procedure(draw_image_i), deferred :: draw_image + + ! Structure. Concrete, and doing nothing is a correct implementation + ! for any format without a document tree. + procedure :: begin_group + procedure :: end_group + end type renderer_t + + abstract interface + + ! Canvas size is fixed up front because PDF needs a MediaBox, EPS a + ! BoundingBox and SVG a width/height before any drawing is emitted. + ! + ! x0/y0 move the canvas rectangle's top left corner, which is how a + ! figure gets cropped: drawing coordinates are untouched and the + ! window onto them moves instead. Every format takes this natively, + ! as an SVG viewBox, a PDF MediaBox or an EPS BoundingBox, so it + ! costs nothing to expose and saves the caller translating geometry. + subroutine open_canvas_i(self, width, height, bg_rgb, bg_alpha, x0, y0) + import :: renderer_t, dp + class(renderer_t), intent(inout) :: self + real(dp), intent(in) :: width, height + integer, intent(in), optional :: bg_rgb(3) + real(dp), intent(in), optional :: bg_alpha + real(dp), intent(in), optional :: x0, y0 + end subroutine open_canvas_i + + ! Finalizes the container: deflate and CRC for PNG, xref for PDF, + ! trailer for EPS, closing tag for SVG. + subroutine close_canvas_i(self) + import :: renderer_t + class(renderer_t), intent(inout) :: self + end subroutine close_canvas_i + + ! The finished document. A character string rather than a specific + ! file type, so binary formats can return raw bytes and callers can + ! write a file or hand it to a notebook without a temporary file. + function bytes_i(self) result(b) + import :: renderer_t + class(renderer_t), intent(in) :: self + character(len=:), allocatable :: b + end function bytes_i + + ! n_verb entries of verbs() describe the vertices in x()/y(): MOVE and + ! LINE take one vertex, CUBIC takes three, CLOSE takes none. + subroutine draw_path_i(self, x, y, verbs, n_verb, paint) + import :: renderer_t, paint_t, dp + class(renderer_t), intent(inout) :: self + real(dp), intent(in) :: x(:), y(:) + integer, intent(in) :: verbs(:), n_verb + type(paint_t), intent(in) :: paint + end subroutine draw_path_i + + ! radius rounds the corners, as the legend box needs. Axis aligned, so + ! a rasterizer can fill whole spans and skip antialiasing on the two + ! edges that fall on pixel boundaries. + subroutine draw_rect_i(self, x, y, w, h, paint, radius) + import :: renderer_t, paint_t, dp + class(renderer_t), intent(inout) :: self + real(dp), intent(in) :: x, y, w, h + type(paint_t), intent(in) :: paint + real(dp), intent(in), optional :: radius + end subroutine draw_rect_i + + subroutine draw_circle_i(self, cx, cy, r, paint) + import :: renderer_t, paint_t, dp + class(renderer_t), intent(inout) :: self + real(dp), intent(in) :: cx, cy, r + type(paint_t), intent(in) :: paint + end subroutine draw_circle_i + + ! One shape, described once about the origin, stamped at every point + ! in x/y with a single paint. The whole reason this is a primitive is + ! that every format can express the shape once and then reference it. + subroutine draw_markers_i(self, x, y, mx, my, mverbs, n_mverb, paint) + import :: renderer_t, paint_t, dp + class(renderer_t), intent(inout) :: self + real(dp), intent(in) :: x(:), y(:) + real(dp), intent(in) :: mx(:), my(:) + integer, intent(in) :: mverbs(:), n_mverb + type(paint_t), intent(in) :: paint + end subroutine draw_markers_i + + ! Text stays text. The string, not an outline, is handed to the + ! backend so that SVG and PDF can keep it selectable and searchable; + ! only PNG is obliged to turn it into glyphs. angle is degrees + ! clockwise about (x, y). + subroutine draw_text_i(self, x, y, s, font, paint, anchor, baseline, angle) + import :: renderer_t, paint_t, font_t, dp + class(renderer_t), intent(inout) :: self + real(dp), intent(in) :: x, y + character(len=*), intent(in) :: s + type(font_t), intent(in) :: font + type(paint_t), intent(in) :: paint + integer, intent(in) :: anchor, baseline + real(dp), intent(in) :: angle + end subroutine draw_text_i + + ! An RGBA raster placed in the rectangle (x, y, w, h), row major from + ! the top left. Used by imshow; PNG blits it, the vector backends + ! embed or tile it. + subroutine draw_image_i(self, x, y, w, h, rgba, nx, ny, paint) + import :: renderer_t, paint_t, dp + class(renderer_t), intent(inout) :: self + real(dp), intent(in) :: x, y, w, h + integer, intent(in) :: nx, ny + integer, intent(in) :: rgba(4, nx, ny) + type(paint_t), intent(in) :: paint + end subroutine draw_image_i + + end interface + +contains + + ! ------------------------------------------------------------------ + ! Structure. + ! + ! Why a group is worth an API call at all: it carries meaning the backend + ! cannot reconstruct. Matplotlib nests every figure as figure_1 > axes_1 > + ! line2d_1, and that tree is what lets someone open the figure in Inkscape + ! or Illustrator and select "the second curve" as one object to restyle. + ! For a paper figure that workflow matters more than anything else in the + ! file. The same names give CSS and Javascript something to target. + ! + ! Note what is deliberately not a reason. Hoisting a shared clip onto one + ! rather than repeating it on fifty children is an + ! optimization the SVG backend can find by itself, by noticing a run of + ! identical clips in the paint. Optimizations a backend can discover on + ! its own do not belong in the contract. + ! + ! PDF can map these onto marked content, which Illustrator reads as + ! layers. EPS and PNG have no document tree, so ignoring them is correct. + ! ------------------------------------------------------------------ + + subroutine begin_group(self, name) + class(renderer_t), intent(inout) :: self + character(len=*), intent(in) :: name + end subroutine begin_group + + subroutine end_group(self) + class(renderer_t), intent(inout) :: self + end subroutine end_group + +end module fplot_render diff --git a/tests/compare_pdfs.py b/tests/compare_pdfs.py new file mode 100644 index 0000000..57bde57 --- /dev/null +++ b/tests/compare_pdfs.py @@ -0,0 +1,131 @@ +"""Compare fplot's PDFs to matplotlib's by rasterizing both. + +A PDF cannot be compared as bytes and is not worth comparing as structure: +two files can describe the same page with completely different operators. So +both sides are handed to the same rasterizer and the resulting images are +compared, which asks the only question that matters, whether the page looks +the same. + +Text is the expected difference and it is a real one, not an artifact of the +measurement. Matplotlib embeds a subset of DejaVu Sans and draws text with +the same outlines it uses everywhere else. fplot's PDF backend refers to the +base-14 Helvetica instead, so the file needs no embedded font at all, but the +glyphs are a different typeface and are spaced by Helvetica's widths while +the surrounding layout was computed from DejaVu's. Everything that is not +text should agree closely, and the per-case limit is set to catch it when it +does not. + + pixi run compare-pdf +""" + +import shutil +import subprocess +import sys +import tempfile +from pathlib import Path + +import numpy as np +from PIL import Image + +ROOT = Path(__file__).resolve().parent +REF = ROOT / "refs" +OUT = ROOT / "out" + +DPI = 100 + +# Mean absolute channel difference out of 255. Looser than the PNG limit +# because a whole typeface differs, not just its hinting. +MEAN_LIMIT = 12.0 + +SIZE_TOLERANCE = {"savefig_tight": 0.02} + + +def render(pdf: Path, tmp: Path) -> np.ndarray | None: + stem = str(tmp / pdf.stem) + r = subprocess.run( + ["pdftoppm", "-r", str(DPI), "-png", "-singlefile", str(pdf), stem], + capture_output=True, + ) + png = Path(stem + ".png") + if r.returncode != 0 or not png.exists(): + return None + return np.asarray(Image.open(png).convert("RGB")).astype(np.int16) + + +def main() -> int: + if shutil.which("pdftoppm") is None: + print("pdftoppm not found; it comes from the poppler dependency") + return 1 + if not REF.exists(): + print("no matplotlib references; run: pixi run mpl-refs") + return 1 + + results = [] + broken = [] + with tempfile.TemporaryDirectory() as td: + # Reference and output share a stem, so they are rendered into + # separate directories to keep pdftoppm from overwriting one with + # the other. + ours = Path(td) / "out" + theirs = Path(td) / "ref" + ours.mkdir() + theirs.mkdir() + for out in sorted(OUT.glob("*.pdf")): + ref = REF / out.name + if not ref.exists(): + continue + b = render(out, ours) + a = render(ref, theirs) + if b is None: + broken.append(out.name) + continue + if a is None: + continue + if a.shape != b.shape: + tol = SIZE_TOLERANCE.get(out.stem, 0.0) + dh = abs(a.shape[0] - b.shape[0]) / a.shape[0] + dw = abs(a.shape[1] - b.shape[1]) / a.shape[1] + if dh > tol or dw > tol: + results.append((out.name, -1.0, f"size {b.shape} vs {a.shape}")) + continue + h, w = min(a.shape[0], b.shape[0]), min(a.shape[1], b.shape[1]) + a, b = a[:h, :w], b[:h, :w] + d = np.abs(a - b) + results.append( + ( + out.name, + float(d.mean()), + "%.1f%% of pixels differ by >32" % ((d.max(axis=2) > 32).mean() * 100), + ) + ) + + if broken: + print(f"FAIL: {len(broken)} PDF(s) could not be rendered at all:") + for n in broken: + print(f" {n}") + return 1 + if not results: + print("no PDFs to compare; run: pixi run test-flang") + return 1 + + print("Comparing fplot PDFs to matplotlib references\n") + results.sort(key=lambda r: -r[1]) + failed = [r for r in results if r[1] < 0 or r[1] > MEAN_LIMIT] + + for name, mean, note in results[:10]: + flag = "FAIL" if (mean < 0 or mean > MEAN_LIMIT) else "ok " + shown = "n/a" if mean < 0 else "%5.2f" % mean + print(f" {flag} {name:<24} mean={shown} {note}") + + good = [r[1] for r in results if r[1] >= 0] + print(f"\n {len(results)} cases, mean absolute difference {np.mean(good):.2f}/255") + + if failed: + print(f"\nFAIL: {len(failed)} case(s) over the limit of {MEAN_LIMIT}") + return 1 + print("\nOK: every case within tolerance") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tests/compare_pngs.py b/tests/compare_pngs.py new file mode 100644 index 0000000..d95b484 --- /dev/null +++ b/tests/compare_pngs.py @@ -0,0 +1,101 @@ +"""Compare fplot's PNGs to matplotlib's, pixel by pixel. + +The SVG comparison next door checks structure, because two renderers can +draw the same SVG differently and a bit comparison would be meaningless. For +PNG there is no such excuse: fplot rasterizes the figure itself, so it can be +held to the actual pixels matplotlib produces. + +The figures are not expected to be identical. fplot lays out independently, so +a tick can land a fraction of a point away from matplotlib's, and after pixel +snapping a fraction of a point becomes a whole pixel. Text is the other source +of difference: matplotlib hints glyphs through FreeType and fplot fills the +outlines unhinted, so stems land on slightly different pixels. What this test +is for is catching the errors that are not sub-pixel: a missing element, a +wrong colour, a shape drawn in the wrong place. + + pixi run compare-png +""" + +import sys +from pathlib import Path + +import numpy as np +from PIL import Image + +ROOT = Path(__file__).resolve().parent +REF = ROOT / "refs" +OUT = ROOT / "out" + +# Mean absolute channel difference, out of 255, averaged over the image. At +# these levels the figures are visually indistinguishable side by side; the +# number is dominated by antialiasing along shared edges. +MEAN_LIMIT = 8.0 + +# Cases whose canvas fplot sizes itself, where a small disagreement about the +# bounding box is expected rather than a fault. +SIZE_TOLERANCE = {"savefig_tight": 0.02} +DEFAULT_SIZE_TOLERANCE = 0.0 + + +def compare(name: str, ref: Path, out: Path) -> tuple[float, str]: + a = np.asarray(Image.open(ref).convert("RGB")).astype(np.int16) + b = np.asarray(Image.open(out).convert("RGB")).astype(np.int16) + + if a.shape != b.shape: + tol = SIZE_TOLERANCE.get(name, DEFAULT_SIZE_TOLERANCE) + dh = abs(a.shape[0] - b.shape[0]) / a.shape[0] + dw = abs(a.shape[1] - b.shape[1]) / a.shape[1] + if dh > tol or dw > tol: + return -1.0, f"size {b.shape[1]}x{b.shape[0]} vs {a.shape[1]}x{a.shape[0]}" + # Same figure, marginally different crop: compare the shared region. + h = min(a.shape[0], b.shape[0]) + w = min(a.shape[1], b.shape[1]) + a, b = a[:h, :w], b[:h, :w] + + d = np.abs(a - b) + return float(d.mean()), "%.1f%% of pixels differ by >32" % ( + (d.max(axis=2) > 32).mean() * 100 + ) + + +def main() -> int: + if not REF.exists(): + print("no matplotlib references; run: pixi run mpl-refs") + return 1 + + results = [] + missing = [] + for out in sorted(OUT.glob("*.png")): + ref = REF / out.name + if not ref.exists(): + missing.append(out.name) + continue + results.append((out.name, *compare(out.stem, ref, out))) + + if not results: + print("no PNGs to compare; run: pixi run test-flang") + return 1 + + print("Comparing fplot PNGs to matplotlib references\n") + results.sort(key=lambda r: -r[1]) + failed = [r for r in results if r[1] < 0 or r[1] > MEAN_LIMIT] + + for name, mean, note in results[:10]: + flag = "FAIL" if (mean < 0 or mean > MEAN_LIMIT) else "ok " + shown = "n/a" if mean < 0 else "%5.2f" % mean + print(f" {flag} {name:<24} mean={shown} {note}") + + good = [r[1] for r in results if r[1] >= 0] + print(f"\n {len(results)} cases, mean absolute difference {np.mean(good):.2f}/255") + + if missing: + print(f"\n no reference for: {', '.join(missing)}") + if failed: + print(f"\nFAIL: {len(failed)} case(s) over the limit of {MEAN_LIMIT}") + return 1 + print("\nOK: every case within tolerance") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tests/compare_svgs.py b/tests/compare_svgs.py index 1f68ac9..2e8065c 100644 --- a/tests/compare_svgs.py +++ b/tests/compare_svgs.py @@ -135,18 +135,19 @@ def num(s: str | None) -> float | None: print(f" ok: {label} matches expected {a}") print( - f" elements mpl: line={count_tags(ref, 'path')} " - f"(paths) text≈{ref.count(' vs fplot clip paths. + # Both now group each axes as , so the same pattern + # reads either file. n_ax_mpl = len(re.findall(r' None: REF.mkdir(parents=True, exist_ok=True) path = REF / f"{name}.svg" fig.savefig(path, format="svg", **kw) + # A PNG reference too: the raster backend has to be measured against + # matplotlib's own pixels, not against a rasterization of its SVG, which + # would only tell us how the SVG renderer differs. + fig.savefig(REF / f"{name}.png", format="png", **kw) + fig.savefig(REF / f"{name}.pdf", format="pdf", **kw) plt.close(fig) print(f"wrote {path}") diff --git a/tests/test_plots.f90 b/tests/test_plots.f90 index 092f582..9bd7a40 100644 --- a/tests/test_plots.f90 +++ b/tests/test_plots.f90 @@ -60,6 +60,8 @@ program test_plots call xlim(0.0_dp, 2.0_dp * pi) call ylim(-1.2_dp, 1.2_dp) call savefig("tests/out/basic_line.svg") + call savefig("tests/out/basic_line.pdf") + call savefig("tests/out/basic_line.png") print *, "wrote tests/out/basic_line.svg" ! 2) multi_style @@ -75,6 +77,8 @@ program test_plots call xlim(0.0_dp, 2.0_dp * pi) call ylim(-1.5_dp, 1.5_dp) call savefig("tests/out/multi_style.svg") + call savefig("tests/out/multi_style.pdf") + call savefig("tests/out/multi_style.png") print *, "wrote tests/out/multi_style.svg" ! 3) markers_only @@ -89,6 +93,8 @@ program test_plots call xlim(0.0_dp, 2.0_dp * pi) call ylim(-1.5_dp, 1.5_dp) call savefig("tests/out/markers_only.svg") + call savefig("tests/out/markers_only.pdf") + call savefig("tests/out/markers_only.png") print *, "wrote tests/out/markers_only.svg" ! 4) semilogx @@ -102,6 +108,8 @@ program test_plots call xlim(0.1_dp, 10.0_dp) call ylim(0.0_dp, 120.0_dp) call savefig("tests/out/semilogx.svg") + call savefig("tests/out/semilogx.pdf") + call savefig("tests/out/semilogx.png") print *, "wrote tests/out/semilogx.svg" ! 5) semilogy @@ -115,6 +123,8 @@ program test_plots call xlim(0.1_dp, 10.0_dp) call ylim(1.0e-4_dp, 20.0_dp) call savefig("tests/out/semilogy.svg") + call savefig("tests/out/semilogy.pdf") + call savefig("tests/out/semilogy.png") print *, "wrote tests/out/semilogy.svg" ! 6) loglog @@ -128,6 +138,8 @@ program test_plots call xlim(0.1_dp, 10.0_dp) call ylim(0.01_dp, 100.0_dp) call savefig("tests/out/loglog.svg") + call savefig("tests/out/loglog.pdf") + call savefig("tests/out/loglog.png") print *, "wrote tests/out/loglog.svg" ! 7) subplots_2x1 (two rows, one column; exercises subplot(m,n,i), @@ -151,6 +163,8 @@ program test_plots call suptitle("fplot subplots") call savefig("tests/out/subplots_2x1.svg") + call savefig("tests/out/subplots_2x1.pdf") + call savefig("tests/out/subplots_2x1.png") print *, "wrote tests/out/subplots_2x1.svg" ! 8) subplots_2x2 (four panels; also checks per-axes log scale) @@ -177,6 +191,8 @@ program test_plots call suptitle("fplot 2x2 subplots") call savefig("tests/out/subplots_2x2.svg") + call savefig("tests/out/subplots_2x2.pdf") + call savefig("tests/out/subplots_2x2.png") print *, "wrote tests/out/subplots_2x2.svg" ! 9) markers gallery (one row per marker code) @@ -193,6 +209,8 @@ program test_plots call xlim(0.0_dp, real(mk + 1, dp)) call ylim(0.0_dp, real(n_marks + 1, dp)) call savefig("tests/out/markers_gallery.svg") + call savefig("tests/out/markers_gallery.pdf") + call savefig("tests/out/markers_gallery.png") print *, "wrote tests/out/markers_gallery.svg" ! 10) scatter @@ -208,6 +226,8 @@ program test_plots call grid(.true.) call legend() call savefig("tests/out/scatter.svg") + call savefig("tests/out/scatter.pdf") + call savefig("tests/out/scatter.png") print *, "wrote tests/out/scatter.svg" ! 11) bar @@ -218,6 +238,8 @@ program test_plots call ylabel("value") call legend() call savefig("tests/out/bar.svg") + call savefig("tests/out/bar.pdf") + call savefig("tests/out/bar.png") print *, "wrote tests/out/bar.svg" ! 12) hist @@ -228,6 +250,8 @@ program test_plots call ylabel("count") call legend() call savefig("tests/out/hist.svg") + call savefig("tests/out/hist.pdf") + call savefig("tests/out/hist.png") print *, "wrote tests/out/hist.svg" ! 13) fill_between @@ -240,6 +264,8 @@ program test_plots call grid(.true.) call legend() call savefig("tests/out/fill_between.svg") + call savefig("tests/out/fill_between.pdf") + call savefig("tests/out/fill_between.png") print *, "wrote tests/out/fill_between.svg" ! 14) errorbar @@ -256,6 +282,8 @@ program test_plots call grid(.true.) call legend() call savefig("tests/out/errorbar.svg") + call savefig("tests/out/errorbar.pdf") + call savefig("tests/out/errorbar.png") print *, "wrote tests/out/errorbar.svg" ! 15) hv_lines @@ -269,6 +297,8 @@ program test_plots call grid(.true.) call legend() call savefig("tests/out/hv_lines.svg") + call savefig("tests/out/hv_lines.pdf") + call savefig("tests/out/hv_lines.png") print *, "wrote tests/out/hv_lines.svg" ! 16) text and annotate @@ -281,6 +311,8 @@ program test_plots call ylabel("y") call grid(.true.) call savefig("tests/out/text_annotate.svg") + call savefig("tests/out/text_annotate.pdf") + call savefig("tests/out/text_annotate.png") print *, "wrote tests/out/text_annotate.svg" ! 17) custom ticks, minor ticks and legend placement @@ -296,6 +328,8 @@ program test_plots call xlabel("x") call ylabel("y") call savefig("tests/out/ticks_legend.svg") + call savefig("tests/out/ticks_legend.pdf") + call savefig("tests/out/ticks_legend.png") print *, "wrote tests/out/ticks_legend.svg" ! 18) a non-default figure size @@ -307,6 +341,8 @@ program test_plots call grid(.true.) call legend() call savefig("tests/out/figsize.svg") + call savefig("tests/out/figsize.pdf") + call savefig("tests/out/figsize.png") print *, "wrote tests/out/figsize.svg" ! 19) more series than the old fixed 32-slot cap. @@ -319,6 +355,8 @@ program test_plots call xlabel("x") call ylabel("y") call savefig("tests/out/many_series.svg") + call savefig("tests/out/many_series.pdf") + call savefig("tests/out/many_series.png") print *, "wrote tests/out/many_series.svg" ! 20) alpha on lines and markers @@ -331,6 +369,8 @@ program test_plots call ylabel("y") call legend() call savefig("tests/out/alpha.svg") + call savefig("tests/out/alpha.pdf") + call savefig("tests/out/alpha.png") print *, "wrote tests/out/alpha.svg" ! 21) a non-default figure facecolor @@ -341,6 +381,8 @@ program test_plots call ylabel("y") call grid(.true.) call savefig("tests/out/facecolor.svg", facecolor="#eeeeee") + call savefig("tests/out/facecolor.pdf", facecolor="#eeeeee") + call savefig("tests/out/facecolor.png", facecolor="#eeeeee") print *, "wrote tests/out/facecolor.svg" do i = 1, nzr @@ -359,6 +401,8 @@ program test_plots call imshow(zimg) call title("imshow") call savefig("tests/out/imshow.svg") + call savefig("tests/out/imshow.pdf") + call savefig("tests/out/imshow.png") print *, "wrote tests/out/imshow.svg" ! 23) imshow with an extent, lower origin and a colorbar @@ -370,6 +414,8 @@ program test_plots call xlabel("x") call ylabel("y") call savefig("tests/out/imshow_cbar.svg") + call savefig("tests/out/imshow_cbar.pdf") + call savefig("tests/out/imshow_cbar.png") print *, "wrote tests/out/imshow_cbar.svg" ! 24) scatter with per-point sizes and color-mapped values @@ -380,6 +426,8 @@ program test_plots call xlabel("x") call ylabel("y") call savefig("tests/out/scatter_cmap.svg") + call savefig("tests/out/scatter_cmap.pdf") + call savefig("tests/out/scatter_cmap.png") print *, "wrote tests/out/scatter_cmap.svg" ! 25) contour lines @@ -389,6 +437,8 @@ program test_plots call xlabel("x") call ylabel("y") call savefig("tests/out/contour.svg") + call savefig("tests/out/contour.pdf") + call savefig("tests/out/contour.png") print *, "wrote tests/out/contour.svg" ! 26) filled contours with a colorbar @@ -399,6 +449,8 @@ program test_plots call xlabel("x") call ylabel("y") call savefig("tests/out/contourf.svg") + call savefig("tests/out/contourf.pdf") + call savefig("tests/out/contourf.png") print *, "wrote tests/out/contourf.svg" ! 27) step @@ -406,6 +458,8 @@ program test_plots call step(xb, hb, where="mid") call title("step") call savefig("tests/out/step.svg") + call savefig("tests/out/step.pdf") + call savefig("tests/out/step.png") print *, "wrote tests/out/step.svg" ! 28) stem @@ -413,6 +467,8 @@ program test_plots call stem(xb, hb) call title("stem") call savefig("tests/out/stem.svg") + call savefig("tests/out/stem.pdf") + call savefig("tests/out/stem.png") print *, "wrote tests/out/stem.svg" ! 29) barh @@ -420,6 +476,8 @@ program test_plots call barh(xb, hb) call title("barh") call savefig("tests/out/barh.svg") + call savefig("tests/out/barh.pdf") + call savefig("tests/out/barh.png") print *, "wrote tests/out/barh.svg" ! 30) pie @@ -427,6 +485,8 @@ program test_plots call pie(hb, labels=["a", "b", "c", "d", "e", "f"]) call title("pie") call savefig("tests/out/pie.svg") + call savefig("tests/out/pie.pdf") + call savefig("tests/out/pie.png") print *, "wrote tests/out/pie.svg" do i = 1, nd @@ -440,6 +500,8 @@ program test_plots call boxplot(dist2) call title("boxplot") call savefig("tests/out/boxplot.svg") + call savefig("tests/out/boxplot.pdf") + call savefig("tests/out/boxplot.png") print *, "wrote tests/out/boxplot.svg" ! 32) violinplot @@ -448,6 +510,8 @@ program test_plots call violinplot(dist2) call title("violinplot") call savefig("tests/out/violinplot.svg") + call savefig("tests/out/violinplot.pdf") + call savefig("tests/out/violinplot.png") print *, "wrote tests/out/violinplot.svg" do i = 1, nsym @@ -460,6 +524,8 @@ program test_plots call set_yscale("symlog") call title("symlog") call savefig("tests/out/symlog.svg") + call savefig("tests/out/symlog.pdf") + call savefig("tests/out/symlog.png") print *, "wrote tests/out/symlog.svg" ! 34) axis("equal") @@ -468,6 +534,8 @@ program test_plots call axis("equal") call title("axis equal") call savefig("tests/out/axis_equal.svg") + call savefig("tests/out/axis_equal.pdf") + call savefig("tests/out/axis_equal.png") print *, "wrote tests/out/axis_equal.svg" ! 35) tick styling and hidden spines @@ -478,6 +546,8 @@ program test_plots call spines(top=.false., right=.false.) call title("tick_params") call savefig("tests/out/tick_style.svg") + call savefig("tests/out/tick_style.pdf") + call savefig("tests/out/tick_style.png") print *, "wrote tests/out/tick_style.svg" ! 36) tight_layout with long tick labels @@ -491,6 +561,8 @@ program test_plots call xlabel("category") call tight_layout() call savefig("tests/out/tight_layout.svg") + call savefig("tests/out/tight_layout.pdf") + call savefig("tests/out/tight_layout.png") print *, "wrote tests/out/tight_layout.svg" ! 37) subplots_adjust @@ -501,6 +573,8 @@ program test_plots call plot(xb, hb) call subplots_adjust(left=0.2_dp, hspace=0.5_dp) call savefig("tests/out/subplots_adjust.svg") + call savefig("tests/out/subplots_adjust.pdf") + call savefig("tests/out/subplots_adjust.png") print *, "wrote tests/out/subplots_adjust.svg" ! 38) twinx @@ -513,6 +587,8 @@ program test_plots call ylabel("right") call title("twinx") call savefig("tests/out/twinx.svg") + call savefig("tests/out/twinx.pdf") + call savefig("tests/out/twinx.png") print *, "wrote tests/out/twinx.svg" ! 39) color spellings @@ -530,6 +606,8 @@ program test_plots end do call title("color names") call savefig("tests/out/colors.svg") + call savefig("tests/out/colors.pdf") + call savefig("tests/out/colors.png") print *, "wrote tests/out/colors.svg" ! 40) font sizes @@ -541,6 +619,8 @@ program test_plots call tick_params(labelsize=13.0_dp) call legend(fontsize=12.0_dp) call savefig("tests/out/fontsize.svg") + call savefig("tests/out/fontsize.pdf") + call savefig("tests/out/fontsize.png") print *, "wrote tests/out/fontsize.svg" ! 41) legend options @@ -551,6 +631,8 @@ program test_plots call plot(xb, hb * 0.125_dp, label="delta") call legend(loc="upper right", ncol=2, title="series", frameon=.false.) call savefig("tests/out/legend_opts.svg") + call savefig("tests/out/legend_opts.pdf") + call savefig("tests/out/legend_opts.png") print *, "wrote tests/out/legend_opts.svg" ! 42) savefig(bbox_inches="tight") @@ -560,6 +642,8 @@ program test_plots call xlabel("x") call ylabel("y") call savefig("tests/out/savefig_tight.svg", bbox_inches="tight", dpi=200.0_dp) + call savefig("tests/out/savefig_tight.pdf", bbox_inches="tight", dpi=200.0_dp) + call savefig("tests/out/savefig_tight.png", bbox_inches="tight", dpi=200.0_dp) print *, "wrote tests/out/savefig_tight.svg" ! 43) two live figures kept apart, then closed @@ -573,6 +657,8 @@ program test_plots call close() call figure(num=fig1) call savefig("tests/out/figures.svg") + call savefig("tests/out/figures.pdf") + call savefig("tests/out/figures.png") print *, "wrote tests/out/figures.svg" call close(all=.true.) diff --git a/tools/gen_glyphs.py b/tools/gen_glyphs.py new file mode 100644 index 0000000..eed2a56 --- /dev/null +++ b/tools/gen_glyphs.py @@ -0,0 +1,266 @@ +"""Regenerate src/fplot_glyphs.f90 from DejaVu Sans. + +The vector backends hand the string to the format and let the viewer find +the font. PNG has no such luxury: it has to turn text into filled outlines +itself, so the outlines have to be in the library. + +DejaVu Sans is matplotlib's default face, so using it is what makes fplot's +PNG text land in the same place as matplotlib's. Only the regular weight is +emitted because that is the only face fplot ever asks for; if bold or italic +are ever exposed, add them here rather than synthesizing them. + +TrueType curves are quadratic and the rendering API only speaks cubics, so +the quadratics are converted exactly on the way out. Coordinates are kept in +font units and scaled by the backend at draw time. + + pixi run python tools/gen_glyphs.py +""" + +import os +from pathlib import Path + +import matplotlib +from fontTools.pens.recordingPen import RecordingPen +from fontTools.ttLib import TTFont + +OUT = Path(__file__).resolve().parent.parent / "src" / "fplot_glyphs.f90" +TTF = ( + Path(os.path.dirname(matplotlib.__file__)) + / "mpl-data" + / "fonts" + / "ttf" + / "DejaVuSans.ttf" +) + +FIRST, LAST = 32, 126 + +VERB_MOVE, VERB_LINE, VERB_CUBIC, VERB_CLOSE = 1, 2, 3, 4 + + +def quad_to_cubic(p0, q, p2): + """Exact degree elevation of a quadratic Bezier to a cubic.""" + c1 = (p0[0] + 2.0 / 3.0 * (q[0] - p0[0]), p0[1] + 2.0 / 3.0 * (q[1] - p0[1])) + c2 = (p2[0] + 2.0 / 3.0 * (q[0] - p2[0]), p2[1] + 2.0 / 3.0 * (q[1] - p2[1])) + return c1, c2 + + +def outline(glyphset, name): + """Return (verbs, points) for one glyph, cubics only.""" + pen = RecordingPen() + glyphset[name].draw(pen) + + verbs: list[int] = [] + pts: list[tuple[float, float]] = [] + cur = (0.0, 0.0) + start = (0.0, 0.0) + + for op, args in pen.value: + if op == "moveTo": + cur = start = args[0] + verbs.append(VERB_MOVE) + pts.append(cur) + elif op == "lineTo": + cur = args[0] + verbs.append(VERB_LINE) + pts.append(cur) + elif op == "curveTo": + for i in range(0, len(args), 3): + c1, c2, p = args[i], args[i + 1], args[i + 2] + verbs.append(VERB_CUBIC) + pts.extend([c1, c2, p]) + cur = p + elif op == "qCurveTo": + # TrueType packs consecutive off-curve points with the on-curve + # point between them implied at the midpoint. A trailing None + # means the contour is entirely off-curve and wraps around. + pl = list(args) + if pl[-1] is None: + pl = pl[:-1] + mid = ( + (pl[0][0] + pl[-1][0]) / 2.0, + (pl[0][1] + pl[-1][1]) / 2.0, + ) + cur = start = mid + verbs.append(VERB_MOVE) + pts.append(cur) + pl = pl + [mid] + for i in range(len(pl) - 1): + q = pl[i] + nxt = pl[i + 1] + end = nxt if i == len(pl) - 2 else ((q[0] + nxt[0]) / 2.0, (q[1] + nxt[1]) / 2.0) + c1, c2 = quad_to_cubic(cur, q, end) + verbs.append(VERB_CUBIC) + pts.extend([c1, c2, end]) + cur = end + elif op == "closePath": + verbs.append(VERB_CLOSE) + cur = start + elif op == "endPath": + pass + else: + raise SystemExit(f"unhandled pen op {op}") + + return verbs, pts + + +def wrap(values, per_line, indent): + """Fortran continuation-friendly wrapping of an array constructor body.""" + out = [] + for i in range(0, len(values), per_line): + chunk = ", ".join(values[i : i + per_line]) + cont = ", &" if i + per_line < len(values) else " &" + out.append(f"{indent}{chunk}{cont}") + return out + + +def main() -> None: + font = TTFont(TTF) + upem = font["head"].unitsPerEm + hmtx = font["hmtx"] + cmap = font.getBestCmap() + glyphset = font.getGlyphSet() + + asc = font["hhea"].ascent + desc = font["hhea"].descent + # sxHeight only exists in OS/2 version 2 and later; DejaVu predates it, + # so take it from the top of the "x" outline, which is its definition. + xh = max(pt[1] for pt in outline(glyphset, cmap[ord("x")])[1]) + + all_verbs: list[int] = [] + all_x: list[float] = [] + all_y: list[float] = [] + vbeg, vlen, pbeg, adv = [], [], [], [] + + for code in range(FIRST, LAST + 1): + name = cmap.get(code) + if name is None: + raise SystemExit(f"DejaVu Sans has no glyph for U+{code:04X}") + verbs, pts = outline(glyphset, name) + vbeg.append(len(all_verbs) + 1) + vlen.append(len(verbs)) + pbeg.append(len(all_x) + 1) + adv.append(hmtx[name][0]) + all_verbs.extend(verbs) + all_x.extend(p[0] for p in pts) + all_y.extend(p[1] for p in pts) + + nv, np_ = len(all_verbs), len(all_x) + + L = [ + "! fplot_glyphs — DejaVu Sans outlines, generated by tools/gen_glyphs.py.", + "!", + "! Do not edit. The PNG backend fills text as outlines because a raster", + "! file cannot defer to a font the way SVG and PDF do, and these are the", + "! outlines matplotlib itself draws with.", + "!", + "! Coordinates are in font units; divide by EM and multiply by the point", + "! size. Curves are cubic, matching fplot_render's path verbs, so the", + "! backend fills a glyph with exactly the code that fills any other path.", + "", + "module fplot_glyphs", + " use fplot_style, only: dp", + " implicit none", + " private", + "", + " public :: EM, ASCENT, DESCENT, XHEIGHT, GLYPH_FIRST, GLYPH_LAST", + " public :: glyph_advance, glyph_verbs, glyph_points", + "", + f" real(dp), parameter :: EM = {float(upem)}_dp", + f" real(dp), parameter :: ASCENT = {float(asc)}_dp", + f" real(dp), parameter :: DESCENT = {float(desc)}_dp", + f" real(dp), parameter :: XHEIGHT = {float(xh)}_dp", + f" integer, parameter :: GLYPH_FIRST = {FIRST}", + f" integer, parameter :: GLYPH_LAST = {LAST}", + "", + f" integer, parameter :: NCH = {LAST - FIRST + 1}", + f" integer, parameter :: NV = {nv}", + f" integer, parameter :: NP = {np_}", + "", + " ! Per character: where its verbs and points start, how many verbs,", + " ! and the advance width to the next character.", + " integer, parameter :: VBEG(NCH) = [ &", + ] + L += wrap([str(v) for v in vbeg], 12, " ") + L += [" ]", "", " integer, parameter :: VLEN(NCH) = [ &"] + L += wrap([str(v) for v in vlen], 12, " ") + L += [" ]", "", " integer, parameter :: PBEG(NCH) = [ &"] + L += wrap([str(v) for v in pbeg], 12, " ") + L += [" ]", "", " integer, parameter :: ADV(NCH) = [ &"] + L += wrap([str(v) for v in adv], 12, " ") + L += [" ]", "", " integer, parameter :: VERBS(NV) = [ &"] + L += wrap([str(v) for v in all_verbs], 25, " ") + L += [" ]", "", " real(dp), parameter :: PX(NP) = [ &"] + L += wrap([f"{v:.1f}_dp" for v in all_x], 8, " ") + L += [" ]", "", " real(dp), parameter :: PY(NP) = [ &"] + L += wrap([f"{v:.1f}_dp" for v in all_y], 8, " ") + L += [ + " ]", + "", + "contains", + "", + " ! Advance width in font units. Characters outside the table are", + " ! treated as spaces so that an unexpected byte cannot crash a plot.", + " function glyph_advance(c) result(a)", + " integer, intent(in) :: c", + " real(dp) :: a", + " if (c < GLYPH_FIRST .or. c > GLYPH_LAST) then", + " a = real(ADV(1), dp)", + " else", + " a = real(ADV(c - GLYPH_FIRST + 1), dp)", + " end if", + " end function glyph_advance", + "", + " ! The verbs for one character, empty when there is nothing to draw.", + " subroutine glyph_verbs(c, v, n)", + " integer, intent(in) :: c", + " integer, allocatable, intent(out) :: v(:)", + " integer, intent(out) :: n", + " integer :: i", + " if (c < GLYPH_FIRST .or. c > GLYPH_LAST) then", + " n = 0", + " allocate (v(0))", + " return", + " end if", + " i = c - GLYPH_FIRST + 1", + " n = VLEN(i)", + " allocate (v(n))", + " if (n > 0) v = VERBS(VBEG(i):VBEG(i) + n - 1)", + " end subroutine glyph_verbs", + "", + " ! The points for one character, in font units.", + " subroutine glyph_points(c, x, y, n)", + " integer, intent(in) :: c", + " real(dp), allocatable, intent(out) :: x(:), y(:)", + " integer, intent(out) :: n", + " integer :: i, j, nv_, k, p0", + " if (c < GLYPH_FIRST .or. c > GLYPH_LAST) then", + " n = 0", + " allocate (x(0), y(0))", + " return", + " end if", + " i = c - GLYPH_FIRST + 1", + " nv_ = VLEN(i)", + " n = 0", + " do j = 1, nv_", + " k = VERBS(VBEG(i) + j - 1)", + " if (k == 1 .or. k == 2) n = n + 1", + " if (k == 3) n = n + 3", + " end do", + " allocate (x(n), y(n))", + " p0 = PBEG(i)", + " if (n > 0) then", + " x = PX(p0:p0 + n - 1)", + " y = PY(p0:p0 + n - 1)", + " end if", + " end subroutine glyph_points", + "", + "end module fplot_glyphs", + "", + ] + + OUT.write_text("\n".join(L)) + print(f"wrote {OUT} ({nv} verbs, {np_} points)") + + +if __name__ == "__main__": + main()