Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RTL Support #45

Draft
wants to merge 2 commits into
base: feature
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added src/bitmaps/button/direction_ltr_16.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 src/bitmaps/button/direction_ltr_24.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 src/bitmaps/button/direction_ltr_32.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 src/bitmaps/button/direction_ltr_48.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 src/bitmaps/button/direction_ltr_64.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 src/bitmaps/button/direction_rtl_16.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 src/bitmaps/button/direction_rtl_24.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 src/bitmaps/button/direction_rtl_32.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 src/bitmaps/button/direction_rtl_48.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 src/bitmaps/button/direction_rtl_64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions src/bitmaps/manifest.respack
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ button/button_color_two_24.png
button/button_color_two_32.png
button/button_color_two_48.png
button/button_color_two_64.png
button/direction_ltr_16.png
button/direction_ltr_24.png
button/direction_ltr_32.png
button/direction_ltr_48.png
button/direction_ltr_64.png
button/direction_rtl_16.png
button/direction_rtl_24.png
button/direction_rtl_32.png
button/direction_rtl_48.png
button/direction_rtl_64.png
button/button_fontname_16.png
button/button_fontname_24.png
button/button_fontname_32.png
Expand Down
37 changes: 37 additions & 0 deletions src/command/edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "../project.h"
#include "../selection_controller.h"
#include "../subs_controller.h"
#include "../subs_edit_ctrl.h"
#include "../text_selection_controller.h"
#include "../utils.h"
#include "../video_controller.h"
Expand Down Expand Up @@ -1265,6 +1266,40 @@ struct edit_insert_original final : public Command {
}
};

struct edit_text_ltr final : public Command {
CMD_NAME("edit/text_ltr")
CMD_ICON(direction_ltr)
STR_DISP("Set text to LTR")
STR_MENU("Set text to LTR")
STR_HELP("Change the direction of the text to LTR")

void operator()(agi::Context *c) override {
if (c->textSelectionController->GetControl()->GetLayoutDirection() != wxLayout_LeftToRight) {
c->textSelectionController->GetControl()->SetLayoutDirection (wxLayout_LeftToRight);
}
}
};

struct edit_text_rtl final : public Command {
CMD_NAME("edit/text_rtl")
CMD_ICON(direction_rtl)
STR_DISP("Set text to RTL")
STR_MENU("Set text to RTL")
STR_HELP("Change the direction of the text to RTL")

void operator()(agi::Context *c) override {
if (c->textSelectionController->GetControl()->GetLayoutDirection() == wxLayout_RightToLeft)
return;

c->textSelectionController->GetControl()->SetLayoutDirection (wxLayout_RightToLeft);
wxString data = c->textSelectionController->GetControl()->GetText();
if (data.StartsWith(RTL_MARK) != false) {
data.insert(0, RTL_MARK);
c->textSelectionController->GetControl()->SetTextRaw(data.c_str());
}
}
};

}

namespace cmd {
Expand All @@ -1273,6 +1308,8 @@ namespace cmd {
reg(agi::make_unique<edit_color_secondary>());
reg(agi::make_unique<edit_color_outline>());
reg(agi::make_unique<edit_color_shadow>());
reg(agi::make_unique<edit_text_ltr>());
reg(agi::make_unique<edit_text_rtl>());
reg(agi::make_unique<edit_font>());
reg(agi::make_unique<edit_find_replace>());
reg(agi::make_unique<edit_line_copy>());
Expand Down
3 changes: 3 additions & 0 deletions src/subs_edit_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ SubsEditBox::SubsEditBox(wxWindow *parent, agi::Context *context)
MakeButton("edit/color/shadow");
middle_right_sizer->AddSpacer(5);
MakeButton("grid/line/next/create");
middle_right_sizer->AddSpacer(5);
MakeButton("edit/text_ltr");
MakeButton("edit/text_rtl");
middle_right_sizer->AddSpacer(10);

by_time = MakeRadio(_("T&ime"), true, _("Time by h:mm:ss.cs"));
Expand Down
23 changes: 22 additions & 1 deletion src/subs_edit_ctrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ SubsTextEditCtrl::SubsTextEditCtrl(wxWindow* parent, wxSize wsize, long style, a
using std::bind;

Bind(wxEVT_CHAR_HOOK, &SubsTextEditCtrl::OnKeyDown, this);
Bind(wxEVT_CHAR, &SubsTextEditCtrl::OnChar, this);

Bind(wxEVT_MENU, bind(&SubsTextEditCtrl::Cut, this), EDIT_MENU_CUT);
Bind(wxEVT_MENU, bind(&SubsTextEditCtrl::Copy, this), EDIT_MENU_COPY);
Expand Down Expand Up @@ -191,9 +192,29 @@ void SubsTextEditCtrl::OnLoseFocus(wxFocusEvent &event) {
event.Skip();
}

void SubsTextEditCtrl::OnKeyDown(wxKeyEvent &event) {
void SubsTextEditCtrl::OnChar(wxKeyEvent &event) {
event.Skip();

// TODO upgrade system to support both RTL and LTR by fixing data
if (GetTextRaw().length() == 0) {
if (IsCharRTL(event.GetUnicodeKey()))
{
SetTextTo(RTL_MARK);
SetLayoutDirection(wxLayout_RightToLeft);
SetSelection(GetSelectionStart() + 5, GetSelectionStart() + 5);
}
else
{
SetLayoutDirection(wxLayout_LeftToRight);
}
}


}

void SubsTextEditCtrl::OnKeyDown(wxKeyEvent &event) {
event.Skip();

// Workaround for wxSTC eating tabs.
if (event.GetKeyCode() == WXK_TAB)
Navigate(event.ShiftDown() ? wxNavigationKeyEvent::IsBackward : wxNavigationKeyEvent::IsForward);
Expand Down
1 change: 1 addition & 0 deletions src/subs_edit_ctrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class SubsTextEditCtrl final : public wxStyledTextCtrl {
void OnSetThesLanguage(wxCommandEvent &event);
void OnLoseFocus(wxFocusEvent &event);
void OnKeyDown(wxKeyEvent &event);
void OnChar(wxKeyEvent &event);

void SetSyntaxStyle(int id, wxFont &font, std::string const& name, wxColor const& default_background);
void Subscribe(std::string const& name);
Expand Down
1 change: 1 addition & 0 deletions src/text_selection_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class TextSelectionController {
int GetStagedInsertionPoint() const { return has_staged_selection ? staged_selection_end : insertion_point; }

void SetControl(wxStyledTextCtrl *ctrl);
wxStyledTextCtrl* GetControl() const {return ctrl;}
~TextSelectionController();

DEFINE_SIGNAL_ADDERS(AnnounceSelectionChanged, AddSelectionListener)
Expand Down
5 changes: 5 additions & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ int SmallestPowerOf2(int x) {
return x;
}

bool IsCharRTL(wxChar c) {
// only hebrew for now
return (((wxChar)0x5d0) <= c) && (c <= ((wxChar)0x5ea));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't tested, but this should be better and support all the RTL character.

Suggested change
return (((wxChar)0x5d0) <= c) && (c <= ((wxChar)0x5ea));
bool IsCharRTL(wxChar character) {
hb_unicode_funcs_t* unicode_funcs = hb_unicode_funcs_get_default();
hb_script_t script = hb_unicode_script(unicode_funcs, character);
return hb_script_get_horizontal_direction(script) == HB_DIRECTION_RTL;
}

}

#ifndef __WXMAC__
void RestartAegisub() {
config::opt->Flush();
Expand Down
5 changes: 5 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class wxMouseEvent;
class wxStyledTextCtrl;
class wxWindow;

const std::string LRT_MARK = "\u202A";
const std::string RTL_MARK = "\u202B";

bool IsCharRTL(wxChar c);

wxString PrettySize(int bytes);

std::string float_to_string(double val, int precision = 3);
Expand Down