The 'TextFont' always return 'Arial' in my Octave 7.3

Hi,

I try to set the textfont into Simple Chinese ‘KaiTi’, which worked in matlab 2023b, but not worked in Octave 7.3. My code is:

clear all;
[wptr,wrect]=Screen('OpenWindow',0,0,[0,650,640,1080]);
b=Screen('Preference','TextEncodingLocale','UTF8');
c=Screen('TextFont',wptr,char('KaiTi'));
Screen('Drawtext',wptr,double('简体中文'),[0],[0],255);
Screen('Flip',wptr);
WaitSecs(2);
sca;

The question is the varible ‘c’ always return the ‘Arial’ font.

Before that I type ‘FontDemo’ on the command window, and the default textfont is ‘Arial’. Is that might be the reason?

I re-setup the PTB, but the problem persist.

How could I fix it?

Thanks,
Huizhong

As per Screen(‘TextFont?’)

[oldFontName,oldFontNumber,oldTextStyle]=Screen(‘TextFont’, windowPtr [,fontNameOrNumber][,textStyle]);

So you get the previous font, not the current.

I run this code in matlab, and the varible ‘c’ also return the ‘Arial’ value. So, this may be not the key problem. The simple-Chinese font ‘KaiTi’ can be showed in matlab, but not in Octave.

Is the Octave using diffrent font file from the matlab? But these Chinese characters canbe show correctly in the compile window.

Thanks, that goes straight to the point.

Does double(‘简体中文’) give you the same value in Octave as in Matlab, when run from your script? Maybe Octave has some unicode issue (I recall vaguely running into this)

Nop. Your words hit me.

It return a 1 x 12 matrix with 3-digit number in Octave, but a 1 x 4 matrix with 5-digit number in Matlab.

Is there any possible to get same value?

You can try this:

...
str = '简体中文';
if IsOctave
    str = double(typecast(unicode2native(str,'UCS-4-INTERNAL'), 'uint32'));
else
    str = double(str);
end
Screen('Drawtext',wptr,str,0,0,255);
...

Yes!
It worked.
Thanks sooooooooooooo much ~

Neat trick to work around Octave’s current lack of proper Unicode handling :slight_smile:

One slight improvement for compatibility with Octave on Linux (and presumably macOS) is to use the UCS-4LE specifier instead of UCS-4-INTERNAL, as the former is supported on Linux/macOS Octave, but the latter isn’t at least on Ubuntu 20.04-24.04 LTS or macOS. On macOS there’s the additional trouble that unicode2native returns 19 Bytes instead of 16 Bytes ie. multiple of 4, so one needs to cut the vector down to multiples of 4 - go figure…

Another thing to mention is that Screen(‘TextFont’) will only return the actual chosen font after a call to Screen(‘TextBounds’), ‘DrawText’, DrawFormattedText etc., iow.

  1. oldfont = Screen(‘TextFont’, w, newfont); % oldont is previously selected font.
  2. nowfont = Screen(‘TextFont’, w); will return newfont as the requested font for next text draw, not neccessarily the font actually used for drawing the next text string.
  3. Screen(‘DrawText’, …) followed by
  4. actualfont = Screen(‘TextFont’, w);

will now contain the name of the truly used font in actualfont. This is because the system may not have the desired ‘newfont’ you wanted, and the fontmapper will then choose an available font that approximates the properties of newfont + selected text style (bold, recursive, underlined etc.) as closely as possible.

FontDemo.m shows this. Also ‘help DrawTextEncodingLocaleSettings’.

Thanks, my font-type is not listed in the FontDemo.m.

I also post this question in the Octave community, and the programmer give me another solution to convert characters into 16 Bytes.

https://octave.discourse.group/t/the-double-return-different-values-in-octave-from-matlab/5683/2

Both of them worked in Windows 11, but the followed problem is when I called the length function to calculated the rect of text, all of these code return a double times length value compared with the corresponding value in matlab. And then these characters only show half of the screen. See there:

https://octave.discourse.group/t/the-double-return-different-values-in-octave-from-matlab/5683/6

Ah, thanks for the info. Will keep it in mind.

Jeez, what a mess!

I don’t know what you are trying to achieve, but wrt. text formatting if that’s it, have a look at DrawFormattedTextDemo.m and DrawFormattedText2Demo.m for how to use the DrawFormattedText() / DrawFormattedText2() functions for more advanced text formatting. Maybe they already have what you need, at least they are our best shot at text formatting / wrapping etc. and trying to handle Unicode reasonably also on Octave. May or may not work decently, text formatting / typesetting is hard, and even more so given the differences between Matlab and Octave and Octave’s current limitations.

Thanks a lot for the advice.