Skip to content

Commit

Permalink
Don't depend on Manim while testing (#61)
Browse files Browse the repository at this point in the history
* Don't depend on Manim while testing
Also test with python3.10-dev and pypy3
Fixes #37

Signed-off-by: Naveen M K <naveen@syrusdark.website>

* fix lint

* Pin correct pytest version
typo

* Ok Cython is broken on py3.10 and pypy
they will fix it
  • Loading branch information
naveen521kk committed May 31, 2021
1 parent c6d40fa commit 7e2b17a
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Expand Up @@ -38,7 +38,7 @@ jobs:
CIBW_ENVIRONMENT_WINDOWS: "PKG_CONFIG_PATH='C:\\cibw\\vendor\\lib\\pkgconfig'"
CIBW_ENVIRONMENT_MACOS: "PKG_CONFIG_PATH='/Users/runner/pangobuild/lib/pkgconfig'"
CIBW_REPAIR_WHEEL_COMMAND_WINDOWS: python packing/inject-dlls.py {wheel} {dest_dir} C:\cibw\vendor\bin
CIBW_TEST_REQUIRES: pytest Cython
CIBW_TEST_REQUIRES: pytest Cython pytest-cov
CIBW_TEST_COMMAND: "bash {project}/packing/test_wheels.sh {project}"
steps:
- uses: actions/checkout@v2
Expand Down
13 changes: 2 additions & 11 deletions .github/workflows/tests.yml
Expand Up @@ -14,7 +14,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
python-version: [3.7, 3.8, 3.9]
python-version: ['3.6', '3.7', '3.8', '3.9']
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
Expand Down Expand Up @@ -52,7 +52,6 @@ jobs:
python setup.py build_ext -i --coverage
python setup.py sdist
pip install .
pip install manim
pytest
- name: Coverage
run: |
Expand Down Expand Up @@ -91,14 +90,7 @@ jobs:
mingw-w64-${{ matrix.arch }}-pango
mingw-w64-${{ matrix.arch }}-python
mingw-w64-${{ matrix.arch }}-python-pip
mingw-w64-${{ matrix.arch }}-cairo
mingw-w64-${{ matrix.arch }}-cython
mingw-w64-${{ matrix.arch }}-python-numpy
mingw-w64-${{ matrix.arch }}-python-pillow
mingw-w64-${{ matrix.arch }}-python-scipy
mingw-w64-${{ matrix.arch }}-python-cairo
mingw-w64-${{ matrix.arch }}-mapbox-earcut
mingw-w64-${{ matrix.arch }}-python-moderngl
- name: Install dependencies
shell: msys2 {0}
Expand All @@ -108,7 +100,6 @@ jobs:
shell: msys2 {0}
run: |
pip install .
pip install manim
python setup.py build_ext -i --coverage
python setup.py sdist
python -m pip install dist/*
Expand All @@ -132,7 +123,7 @@ jobs:
strategy:
matrix:
os: [windows-2016]
python-version: [3.7, 3.8, 3.9]
python-version: ['3.6', '3.7', '3.8', '3.9']
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }} for x64
Expand Down
3 changes: 1 addition & 2 deletions packing/test_wheels.sh
Expand Up @@ -8,8 +8,7 @@ FILE_PATH="`( cd \"$FILE_PATH\" && pwd )`"
if [ -z "$FILE_PATH" ] ; then
exit 1
fi
pip install manim --no-deps
pip install colour numpy Pillow progressbar scipy tqdm pydub pygments rich pycairo networkx mapbox-earcut moderngl-window moderngl importlib-metadata watchdog

cd $TMP
cp -r $package/tests mtests
pytest -s mtests
Expand Down
9 changes: 4 additions & 5 deletions requirements-dev.txt
@@ -1,5 +1,4 @@
pytest-cov
pytest
Cython
coverage
manim>0.2.0
pytest-cov==2.12.0
pytest==6.2.4
Cython==0.29.23
coverage==5.5
117 changes: 115 additions & 2 deletions tests/_manim.py
Expand Up @@ -2,11 +2,12 @@
"""This file contains helpers for the tests copied and modified
from Manim.
"""

import copy
import os
import re
from pathlib import Path

from manimpango import Alignment, MarkupUtils
from manimpango import Alignment, MarkupUtils, TextSetting, text2svg


class MarkupText:
Expand Down Expand Up @@ -104,3 +105,115 @@ def text2svg(self):

def __repr__(self):
return f"MarkupText({repr(self.original_text)})"


class Text:
def __init__(
self,
text: str,
fill_opacity: float = 1.0,
stroke_width: int = 0,
size: int = 1,
line_spacing: int = -1,
font: str = "",
slant: str = "NORMAL",
weight: str = "NORMAL",
gradient: tuple = None,
tab_width: int = 4,
disable_ligatures: bool = False,
filename: str = "text.svg",
**kwargs,
) -> None:
self.size = size
self.filename = filename
self.line_spacing = line_spacing
self.font = font
self.slant = slant
self.weight = weight
self.gradient = gradient
self.tab_width = tab_width
self.original_text = text
self.disable_ligatures = disable_ligatures
text_without_tabs = text
self.t2f = self.t2s = self.t2w = {}
if text.find("\t") != -1:
text_without_tabs = text.replace("\t", " " * self.tab_width)
self.text = text_without_tabs
if self.line_spacing == -1:
self.line_spacing = self.size + self.size * 0.3
else:
self.line_spacing = self.size + self.size * self.line_spacing
self.text2svg()

def text2settings(self):
"""Internally used function. Converts the texts and styles
to a setting for parsing."""
settings = []
t2x = [self.t2f, self.t2s, self.t2w]
for i in range(len(t2x)):
fsw = [self.font, self.slant, self.weight]
if t2x[i]:
for word, x in list(t2x[i].items()):
for start, end in self.find_indexes(word, self.text):
fsw[i] = x
settings.append(TextSetting(start, end, *fsw))
# Set all text settings (default font, slant, weight)
fsw = [self.font, self.slant, self.weight]
settings.sort(key=lambda setting: setting.start)
temp_settings = settings.copy()
start = 0
for setting in settings:
if setting.start != start:
temp_settings.append(TextSetting(start, setting.start, *fsw))
start = setting.end
if start != len(self.text):
temp_settings.append(TextSetting(start, len(self.text), *fsw))
settings = sorted(temp_settings, key=lambda setting: setting.start)

if re.search(r"\n", self.text):
line_num = 0
for start, end in self.find_indexes("\n", self.text):
for setting in settings:
if setting.line_num == -1:
setting.line_num = line_num
if start < setting.end:
line_num += 1
new_setting = copy.copy(setting)
setting.end = end
new_setting.start = end
new_setting.line_num = line_num
settings.append(new_setting)
settings.sort(key=lambda setting: setting.start)
break
for setting in settings:
if setting.line_num == -1:
setting.line_num = 0
return settings

def text2svg(self):
"""Internally used function.
Convert the text to SVG using Pango
"""
size = self.size * 10
line_spacing = self.line_spacing * 10
dir_name = Path(self.filename).parent
disable_liga = self.disable_ligatures
if not os.path.exists(dir_name):
os.makedirs(dir_name)
file_name = self.filename
settings = self.text2settings()
width = 600
height = 400

return text2svg(
settings,
size,
line_spacing,
disable_liga,
file_name,
30,
30,
width,
height,
self.text,
)
5 changes: 2 additions & 3 deletions tests/test_fonts.py
Expand Up @@ -3,13 +3,12 @@
from pathlib import Path
from shutil import copyfile

import manim
import pytest

import manimpango

from . import FONT_DIR
from ._manim import MarkupText
from ._manim import MarkupText, Text

font_lists = {
(FONT_DIR / "AdobeVFPrototype.ttf").absolute(): "Adobe Variable Font Prototype",
Expand Down Expand Up @@ -38,7 +37,7 @@ def test_register_font(font_name):
@pytest.mark.parametrize("font_name", font_lists.values())
def test_warning(capfd, font_name):
print(font_name)
manim.Text("Testing", font=font_name)
Text("Testing", font=font_name)
captured = capfd.readouterr()
assert "Pango-WARNING **" not in captured.err, "Looks like pango raised a warning?"

Expand Down

0 comments on commit 7e2b17a

Please sign in to comment.