Skip to content

Commit

Permalink
Working on a new render text to improve overall quality
Browse files Browse the repository at this point in the history
  • Loading branch information
nath54 committed Oct 29, 2022
1 parent 4a1f18f commit 5d86098
Show file tree
Hide file tree
Showing 11 changed files with 370 additions and 97 deletions.
8 changes: 7 additions & 1 deletion global/Global.gd
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ var settings: Dictionary = {
"language": 0,
"data_path": DATA_PATH,
"type_page_princip": 0,
"theme_app": 0
"theme_app": 0,
"deb_lat": "\\[",
"end_lat": "\\]"
};

var original_font_sizes: Dictionary = {};
Expand Down Expand Up @@ -88,6 +90,10 @@ func load_params() -> void:
file.open(SETTINGS_PATH, File.READ);
settings = JSON.parse(file.get_as_text()).result;
file.close();
if not "deb_lat" in settings.keys():
settings["deb_lat"] = "\\[";
if not "end_lat" in settings.keys():
settings["end_lat"] = "\\]";

func save_params():
file.open(SETTINGS_PATH, File.WRITE);
Expand Down
Binary file added icon_192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icon_432.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
81 changes: 41 additions & 40 deletions pages/RenderText.tscn

Large diffs are not rendered by default.

150 changes: 150 additions & 0 deletions pages/RenderTextNew.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
extends Control

var text: String = "";
var min_scale: float = 0.4;
onready var hflow: HFlowContainer = $Control/HFlowContainer;

var dl: String = Global.settings["deb_lat"];
var el: String = Global.settings["end_lat"];

var changed: bool = false;
var count_changed: int = 0;

var parties: Array = []; # pour chaque élément : 0 = text, 1 = type (0=label, 1 = math)

func create_label(t: String) -> Label:
var lbl: Label = Label.new();
lbl.text = t;
lbl.mouse_filter =Control.MOUSE_FILTER_IGNORE;
lbl.add_color_override("font_color", Color(0, 0, 0));
return lbl;

func create_math(t: String) -> Control:
var ctr: Control = Control.new();
ctr.name = "Control";
ctr.mouse_filter = Control.MOUSE_FILTER_IGNORE;
# ctr.anchor_left = 0;
# ctr.anchor_top = 0;
# ctr.anchor_bottom = 1;
# ctr.anchor_right = 1;
# ctr.size_flags_horizontal = SIZE_EXPAND_FILL;
# ctr.size_flags_vertical = SIZE_EXPAND_FILL;
# FOR DEBUGGING
var pnl: Panel = Panel.new();
pnl.anchor_left = 0;
pnl.anchor_top = 0;
pnl.anchor_bottom = 1;
pnl.anchor_right = 1;
ctr.add_child(pnl);
#
var ctr2: Control = Control.new();
ctr2.name = "Control";
ctr2.mouse_filter = Control.MOUSE_FILTER_IGNORE;
ctr.anchor_left = 0.5;
ctr.anchor_top = 0.5;
ctr.anchor_bottom = 0.5;
ctr.anchor_right = 0.5;
ctr.add_child(ctr2);
var sprt: Sprite = Sprite.new();
sprt.name = "Sprite";
ctr2.add_child(sprt);
sprt.set_script(load("res://addons/GodoTeX/LaTeX.cs"));
sprt.LatexExpression = t;
sprt.Render();
#
sprt.position = Vector2(0,0);
ctr.rect_min_size = sprt.texture.size * sprt.scale;
return ctr;

func create_empty_row() -> Node:
var lbl = Panel.new();
lbl.modulate = Color(0, 0, 0, 0);
lbl.rect_min_size.x = hflow.rect_size.x*1.5;
lbl.mouse_filter = Control.MOUSE_FILTER_IGNORE;
return lbl;

func decompose_text(tt: String) -> void: # On est sur qu'il n'y a pas de latex dedans
var at: PoolStringArray = tt.split("\n");
for i in range(len(at)):
if i != 0:
parties.append(["", 2])
var t: String = at[i];
var ii: int = 0; # On va séparer les espaces en plusieurs sous label, pour avoir un meilleur rendu (? au coût de performances ?, je ne pense pas trop, a moins que le nombre d'espace ne soit bcp trop grand, mais faut faire exprès la quand même)
if ii == -1:
parties.append([t, 0]);
else:
while ii != -1:
var nii:int = t.find(" ", ii+1);
if nii != -1:
parties.append([t.substr(ii, nii-ii), 0]);
else:
parties.append([t.substr(ii), 0]); # C'est la dernière partie sans espaces
ii = nii;

func decompose_parties_base(txt: String) -> void:
# On va décomposer le texte entre les différentes parties :
# Une balise de math est séparée entre "\[" et "\]"
# Si on trouve un "\[" et que l'on ne trouve pas de "\]" après, cela ne compte pas comme une balise de math
var i0: int = 0; # current position on text
var idm: int = txt.find(dl, i0);
while idm != -1 and len(txt)-i0 > 1: # Tant qu'on en trouve:
# On vérifie que la séquence n'est pas précédée d'un backslash => Annule la séquence
if idm == 0 or txt[idm-1] != "\\":
# On cherche s'il y a bien une balise de fermeture pour la séquence de math
var ifm: int = txt.find(el, idm+len(dl));
var good: bool = false;
while ifm != -1 and txt[ifm-1] == "\\": # On en a trouvée une, mais elle est annulée par un '\'
ifm = txt.find(el, ifm+len(ifm)); # Donc on en cherche une plus loin
if ifm != -1:
# On ajoute d'abord le text avant :
decompose_text(txt.substr(i0, idm));
# On ajoute ensuite la partie de math
parties.append([txt.substr(idm+len(dl), ifm-idm-len(el)), 1]);
# On update le curseur dans notre texte
i0 = ifm+len(dl);
if not good:
# Il n'y a pas de parties de math
# On ajoute tout le texte restant
decompose_text(txt.substr(i0));
i0 = len(txt);
else:
i0 = idm + len(dl);
#
idm = txt.find(dl, i0);
#
# On ajoute le texte restant s'il y en a un
if len(txt)-i0 > 1:
decompose_text(txt.substr(i0));

func set_text(txt: String):
text = txt;
# On nettoie ce qu'il pourrait y avoit avant
Global.empty_childs(hflow);
# On décompose
parties = [];
decompose_parties_base(txt);
# Maintenant, pour chaque partie, on va l'afficher
for p in parties:
if p[1] == 0: # label
hflow.add_child(create_label(p[0]));
elif p[1] == 1: # Maths
hflow.add_child(create_math(p[0]));
elif p[1] == 2: # Ligne vide
hflow.add_child(create_empty_row());
#
changed = true;

func _process(delta):
if changed:
if count_changed < 10:
if count_changed == 5:
set_text(text);
count_changed += 1;
else:
count_changed = 0;
changed = false;
for c in hflow.get_children():
if c is Control and not (c is Label or c is Panel):
var sprt = c.get_node("Control/Sprite");
sprt.position = Vector2(0,0);
c.rect_min_size = sprt.texture.size * sprt.scale;
79 changes: 79 additions & 0 deletions pages/RenderTextOld.tscn

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions pages/home/settings/page_settings.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ func _ready():
$VBoxContainer/FontSize/FontSize.text = String(Global.settings["font_size"])+"%";
$VBoxContainer/Langue/OptionButton.selected = Global.settings["language"];
$VBoxContainer/DataLoc/DataPath.text = Global.settings["data_path"];
$VBoxContainer/DebLat/LineEdit.text = Global.settings["deb_lat"];
$VBoxContainer/EndLat/LineEdit.text = Global.settings["end_lat"];
#
Global.resize_all_fonts();

Expand All @@ -32,6 +34,8 @@ func _on_Bt_apply_pressed():
# TODO, sauvegarder les données et recharger la page
Global.settings["font_size"] = $VBoxContainer/FontSize/InpFontSize.value;
Global.settings["language"] = $VBoxContainer/Langue/OptionButton.selected;
Global.settings["deb_lat"] = $VBoxContainer/DebLat/LineEdit.text;
Global.settings["end_lat"] = $VBoxContainer/EndLat/LineEdit.text;
if $VBoxContainer/DataLoc/DataPath.text != Global.settings["data_path"]:
# On supprime l'ancien fichier de data
var dir: Directory = Directory.new();
Expand Down
Loading

0 comments on commit 5d86098

Please sign in to comment.