Hi, JUCE developers.
I found a bug in TextEditor that a part of the string is not drawn when a string containing double-byte spaces is set and highlighted.
This bug was confirmed in JUCE 6.1.2.
Here are sample codes.
<MainComponent.h>
#pragma once
#include <JuceHeader.h>
class MainComponent : public juce::Component
{
public:
MainComponent();
~MainComponent() override;
void paint (juce::Graphics&) override;
void resized() override;
private:
juce::TextEditor editor;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
};
<MainComponent.cpp>
#include "MainComponent.h"
using namespace juce;
// Note: japanese charactor and multi-byte space are repeated
// あ い う え お あ い う え お あ......
const auto japanese_text(CharPointer_UTF8("\xe3\x81\x82\xe3\x80\x80\xe3\x81\x84\xe3\x80\x80\xe3\x81\x86\xe3\x80\x80\xe3\x81\x88\xe3\x80\x80\xe3\x81\x8a\xe3\x80\x80\xe3\x81\x82\xe3\x80\x80\xe3\x81\x84\xe3\x80\x80\xe3\x81\x86\xe3\x80\x80\xe3\x81\x88\xe3\x80\x80\xe3\x81\x8a\xe3\x80\x80\xe3\x81\x82\xe3\x80\x80\xe3\x81\x84\xe3\x80\x80\xe3\x81\x86\xe3\x80\x80\xe3\x81\x88\xe3\x80\x80\xe3\x81\x8a\xe3\x80\x80\xe3\x81\x82\xe3\x80\x80\xe3\x81\x84\xe3\x80\x80\xe3\x81\x86\xe3\x80\x80\xe3\x81\x88\xe3\x80\x80\xe3\x81\x8a\xe3\x80\x80\xe3\x81\x82\xe3\x80\x80\xe3\x81\x84\xe3\x80\x80\xe3\x81\x86\xe3\x80\x80\xe3\x81\x88\xe3\x80\x80\xe3\x81\x8a\xe3\x80\x80\xe3\x81\x82\xe3\x80\x80\xe3\x81\x84\xe3\x80\x80\xe3\x81\x86\xe3\x80\x80\xe3\x81\x88\xe3\x80\x80\xe3\x81\x8a\xe3\x80\x80\xe3\x81\x82\xe3\x80\x80\xe3\x81\x84\xe3\x80\x80\xe3\x81\x86\xe3\x80\x80\xe3\x81\x88\xe3\x80\x80\xe3\x81\x8a\xe3\x80\x80\xe3\x81\x82\xe3\x80\x80\xe3\x81\x84\xe3\x80\x80\xe3\x81\x86\xe3\x80\x80\xe3\x81\x88\xe3\x80\x80\xe3\x81\x8a\xe3\x80\x80"));
MainComponent::MainComponent()
{
editor.setMultiLine(true);
#if JUCE_WINDOWS
const Font font("MS UI Gothic", 15.f, Font::plain);
#elif JUCE_MAC
const Font font("Arial Unicode MS", 15.f, Font::plain);
#else
const Font font("IPAGothic", 15.f, Font::plain);
#endif
editor.setFont(font);
editor.setText(String(japanese_text));
editor.selectAll();
addAndMakeVisible(&editor);
setSize(300, 400);
}
MainComponent::~MainComponent()
{
}
void MainComponent::paint (juce::Graphics& g)
{
}
void MainComponent::resized()
{
editor.setBounds(getLocalBounds());
}
When you run this code, you will see the following:
then unhighlighted,
Here is a sample project file.
TextEditorBugSample.zip (9.1 KB)
I investigated the cause of the problem and found the following things wrong.
In TextEditor::UniformTextSection::initialiseAtoms(),
...
// create a whitespace atom unless it starts with non-ws
if (text.isWhitespace() && *text != '\r' && *text != '\n')
{
do
{
++text;
++numChars;
}
while (text.isWhitespace() && *text != '\r' && *text != '\n');
}
...
This implemention is that only one-byte spaces are considered as spaces, and multi-byte spaces are not judged as spaces.
More detail, CharPointer_UTF8::isWhitespace() is false when first charactor is multi-byte space.
And, other implemention in TextEditor is called CharacterFunctions::isWhitespace().
That function is true when first charactor is multi-byte space.
So, this strange behavior has occured.
I hope that they will handle this bug.
Thanks.