Repository: ManimCommunity/manim

Issue:
Text position issue when using both newline and t2c
## Description of bug / unexpected behavior
Using both \n and a t2c dictionary causes the colored text to reset its position causing it to overlap with existing text.

## Expected behavior
For it to show a multi-line text with certain words colored

## How to reproduce the issue


<details><summary>Code for reproducing the problem</summary>

```py
class TestScene(Scene):
    def construct(self):
        text = Text("A B \nC D E F", t2c={"B": RED, "D": GREEN, "E" : BLUE})
        self.add(text)
```

</details>


## Additional media files

<details><summary>Images/GIFs</summary>

Unexpected behavior
![TestScene_ManimCE_v0 14 0](https://user-images.githubusercontent.com/8254110/154390685-b5d80d0b-aa64-4b56-afab-0debfab86eb4.png)

Expected behavior
![image](https://user-images.githubusercontent.com/8254110/154392544-85dbb618-af19-4b8d-9271-201363ce1168.png)

</details>

## System specifications

<details><summary>System Details</summary>

- OS (with version, e.g., Windows 10 v2004 or macOS 10.15 (Catalina)): Windows 10 Pro 21H2
- RAM: 16GB
- Python version (`python/py/python3 --version`):  3.10.2
- Installed modules (provide output from `pip list`): 
```
Package                   Version
------------------------- -----------
backports.cached-property 1.0.1
certifi                   2021.10.8
charset-normalizer        2.0.12
click                     8.0.3
click-default-group       1.2.2
cloup                     0.7.1
colorama                  0.4.4
colour                    0.1.5
commonmark                0.9.1
decorator                 5.1.1
glcontext                 2.3.4
idna                      3.3
isosurfaces               0.1.0
manim                     0.14.0
ManimPango                0.4.0.post2
mapbox-earcut             0.12.11
moderngl                  5.6.4
moderngl-window           2.4.1
multipledispatch          0.6.0
networkx                  2.6.3
numpy                     1.22.2
Pillow                    9.0.1
pip                       22.0.3
pycairo                   1.20.1
pydub                     0.25.1
pyglet                    1.5.21
Pygments                  2.11.2
pyrr                      0.10.3
requests                  2.27.1
rich                      11.2.0
scipy                     1.8.0
screeninfo                0.8
setuptools                60.9.2
six                       1.16.0
skia-pathops              0.7.2
srt                       3.5.1
tqdm                      4.62.3
urllib3                   1.26.8
watchdog                  2.1.6
wheel                     0.37.1
```
</details>

<details><summary>FFMPEG</summary>

Output of `ffmpeg -version`:

```
ffmpeg version 5.0-essentials_build-www.gyan.dev Copyright (c) 2000-2022 the FFmpeg developers
built with gcc 11.2.0 (Rev5, Built by MSYS2 project)
configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-lzma --enable-zlib --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-sdl2 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-libass --enable-libfreetype --enable-libfribidi --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-libgme --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libtheora --enable-libvo-amrwbenc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-librubberband
libavutil      57. 17.100 / 57. 17.100
libavcodec     59. 18.100 / 59. 18.100
libavformat    59. 16.100 / 59. 16.100
libavdevice    59.  4.100 / 59.  4.100
libavfilter     8. 24.100 /  8. 24.100
libswscale      6.  4.100 /  6.  4.100
libswresample   4.  3.100 /  4.  3.100
libpostproc    56.  3.100 / 56.  3.100
```
</details>



Discussion:
Can you try previous versions of manim(last 2 major versions should do) and see from which version does this issue exists? I remember this being rendered correctly on an older version.
Just checked on `v0.13.1`
![manim text bug v0.13.1](https://user-images.githubusercontent.com/49693820/154519568-d00904ee-5308-49cf-8c0a-617c3ab6ac81.png)
on `v0.14.0`
![manim text bug v0.14.0](https://user-images.githubusercontent.com/49693820/154519827-7b8aeaff-68f6-49c8-8d14-9095653e567c.png)


The created SVG in the last case is
![687d72999024b539](https://user-images.githubusercontent.com/49693820/154520778-a3885c8f-1b32-4720-a4aa-2368be6e4101.svg)
so something has to be done inside of ManimPango and not just manim. 

CC: @marcin-serwin (since you introduced this change)
It's actually a problem with manim, not ManimPango and it was broken before but with other `t2x`, e.g. this code on `v0.13.1`
```python
class TestScene(Scene):
    def construct(self):
        text = Text("A B \nC D E F", t2s={"B": OBLIQUE, "D": ITALIC})
        self.add(text)
```
produces
![image](https://user-images.githubusercontent.com/12128106/154658154-249b94dc-df5c-4726-b2f7-fb76561e55c1.png)
I'll open a PR soon.

Resolving patch:
diff --git a/manim/mobject/svg/text_mobject.py b/manim/mobject/svg/text_mobject.py
--- a/manim/mobject/svg/text_mobject.py
+++ b/manim/mobject/svg/text_mobject.py
@@ -719,8 +719,8 @@ def _text2settings(self, color: Color):
             temp_settings.append(TextSetting(start, len(self.text), **default_args))
         settings = sorted(temp_settings, key=lambda setting: setting.start)
 
+        line_num = 0
         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:
@@ -736,7 +736,7 @@ def _text2settings(self, color: Color):
                         break
         for setting in settings:
             if setting.line_num == -1:
-                setting.line_num = 0
+                setting.line_num = line_num
         return settings
 
     def _text2svg(self, color: Color):


Test patch: