日本語の呈示に関するデモとして、DrawHighQualityUnicodeTextDemo というのがあります。
Linux以外では問題なく動作しました。
Linuxでは、デモプログラムを起動したあと、なにも呈示されない画面になりますので、そこでキーボードの「q」を一度押す必要があります。(Octaveではそうでした。Matlabでは不明です)
基本的にはこのデモの通りにすれば日本語を呈示できますが、余計な機能もついているので、日本語の呈示のみに機能をしぼったプログラムを公開しておきます。
前知識として、Screen('DrawText') と DrawFormattedText に目を通してください。
また、日本語の呈示についてはこちらでも議論されていますので、参照してください。
指定したフォントがインストールされていない場合は、フォントの変更が行われませんので注意してください。
% Windowsではコントロールパネルからフォントを確認できます。% Macではアプリケーション → Font Bookから、フォントを確認できます。function JapaneseTest2AssertOpenGL;ListenChar(2);%myKeyCheck;try screenNumber=max(Screen('Screens')); [windowPtr, rect]=Screen('OpenWindow', screenNumber);
% 実験環境によって文字列をdoubleでくくる必要がある場合とない場合があります。 % 私の確認したところ、次のような結果でした。 % strFlag = 0; % doubleあり:Matlab(Mac), Matlab(Win) % strFlag = 1; % doubleなし:Matlab(Windows), Octave(Linux), Octave(Mac), Octave(Win)
strFlag = 0; if strFlag == 1 myText = 'TEST:日本語のテスト'; else myText = double('TEST:日本語のテスト'); end; % OSによってフォントの設定方法が異なります。 if IsOSX % Mac allFonts = FontInfo('Fonts'); foundfont = 0; for idx = 1:length(allFonts) if strcmpi(allFonts(idx).name, 'Hiragino Kaku Gothic Pro W3') foundfont = 1; break; end end if ~foundfont error('Could not find wanted japanese font on OS/X !'); end Screen('TextFont', windowPtr, allFonts(idx).number); end; if IsLinux % Linux Screen('TextFont', windowPtr, '-:lang=ja'); end; if IsWin % Windows Screen('TextFont', windowPtr, 'Courier New'); end; % フォントサイズの設定 Screen('TextSize', windowPtr, 30); % DrawTextを使って (x, y) = (100, 100) に描画 Screen('DrawText', windowPtr, myText, 100, 100); % 多機能なDrawFormattedTextを使って、画面の中央に描画 DrawFormattedText(windowPtr, myText, 'center', 'center', [0 0 0]); Screen('Flip', windowPtr); %キー入力を待つ KbWait; sca; ListenChar(0);catch sca; ListenChar(0); psychrethrow(psychlasterror);end余談ですが、Mac OS X では次のような方法でインストールされているフォントを確認することができます。
これでフォント名を調べて、フォントを指定することができます。
function SearchFontsallFonts = FontInfo('Fonts');for idx = 1:length(allFonts) fprintf('Font num: %d, Font name: %s \n', allFonts(idx).number,allFonts(idx).name);
end