<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
Dear VTK Users,<br>
<br>
An now the final piece of the puzzle: how to specify the Windows
system font file for FreeType to use.<br>
<br>
Keep in mind that this is C++/CLI .NET 4 code, but something similar
is possible in plain C++.<br>
<title>Snippet</title>
<pre style="font-family:Consolas;font-size:13;color:black;background:white;"> <span style="color:green;">// Must store the m_font_filepath because the VTK text property only stores a const char * to it. So it must be persistent.</span>
<span style="color:green;">// Have a few to ensure that we always find one. Otherwise, the default "Arial" is used that doesn't support Unicode.</span>
<title>Snippet</title> <span style="color:blue;">array</span><<span style="color:blue;">String</span> ^> ^<span style="color:navy;">font_filenames</span>= { <span style="color:#a31515;">"tahoma.ttf"</span>, <span style="color:#a31515;">"arial.ttf"</span>, <span style="color:#a31515;">"times.ttf"</span>, <span style="color:#a31515;">"cour.ttf"</span> };
<span style="color:blue;">String</span> ^<span style="color:navy;">font_folder</span>= <span style="color:blue;">Environment</span>::<span style="color:#880000;">GetFolderPath</span>(<span style="color:blue;">Environment</span>::<span style="color:blue;">SpecialFolder</span>::<span style="color:#a000a0;">Fonts</span>);
<span style="color:blue;">for</span> <span style="color:blue;">each</span> (<span style="color:blue;">String</span> ^<span style="color:navy;">font_filename</span> <span style="color:blue;">in</span> <span style="color:navy;">font_filenames</span>)
{
<span style="color:blue;">String</span> ^<span style="color:navy;">font_filepath</span>= <span style="color:blue;">IO</span>::<span style="color:blue;">Path</span>::<span style="color:#880000;">Combine</span>(<span style="color:navy;">font_folder</span>, <span style="color:navy;">font_filename</span>);
<span style="color:blue;">if</span> (<span style="color:blue;">IO</span>::<span style="color:blue;">File</span>::<span style="color:#880000;">Exists</span>(<span style="color:navy;">font_filepath</span>))
{
<span style="color:navy;">m_font_filepath</span>= <span style="color:#880000;">marshal_as</span><<span style="color:blue;">std</span>::<span style="color:blue;">string</span>>(<span style="color:navy;">font_filepath</span>);
<span style="color:blue;">break</span>;
}
}
<span style="color:blue;">Diagnostics</span>::<span style="color:blue;">Debug</span>::<span style="color:#880000;">Assert</span>(<span style="color:navy;">m_font_filepath</span> != <span style="color:#a000a0;">NULL</span>);
<span style="color:blue;">if</span> (<span style="color:navy;">!m_font_filepath</span>.empty()<span style="color:blue;"></span>)
{
<span style="color:navy;">m_VTKScalarBarActor</span>-><span style="color:#880000;">GetTitleTextProperty</span>()-><span style="color:#880000;">SetFontFamily</span>(<span style="color:#a000a0;">VTK_FONT_FILE</span>);
<span style="color:navy;">m_VTKScalarBarActor</span>-><span style="color:#880000;">GetTitleTextProperty</span>()-><span style="color:#880000;">SetFontFile</span>(<span style="color:navy;">m_font_filepath</span>.<span style="color:#880000;">c_str</span>());
}
</pre>
The only catch here is that this bypasses the FreeType controls for
bold/italic/etc. since these are stored in different font files and
so you must manage that yourself (if necessary). I tested the 4
fonts listed above on my Windows 7 Professional PC and they all
properly handle Unicode characters. In my experience, the font
mapping for Tahoma works well on other languages of Windows such as
Japanese and Chinese, so it's a good default choice for a sans serif
font.<br>
<br>
I hope that this helps others in the future.<br>
<br>
<div class="moz-cite-prefix">On 1/21/2015 3:26 PM, Serge Lalonde
wrote:<br>
</div>
<blockquote cite="mid:54C00B66.2080807@infolytica.com" type="cite">
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
Dear VTK Users,<br>
<br>
So I finally got it working. Many thanks to all who replied,
particularly David Gobbi.<br>
Here's the summary so that anyone trying to do this in the future
will find it easier.<br>
<br>
To pass a Unicode C++/CLI .NET string from a String^ to a VTK 6.1
UTF-8 encoded string for display:<br>
<title>Snippet</title>
<pre style="font-family:Consolas;font-size:13;color:black;background:white;"> <span style="color:navy;">m_VTKScalarBarActor</span>-><span style="color:#880000;">SetTitle</span>(<b><span style="color:blue;">vtkUnicodeString</span></b><b>::</b><b><span style="color:#880000;">from_utf16</span></b><b>((</b><b><span style="color:blue;">const</span></b><b> </b><b><span style="color:blue;">vtkTypeUInt16</span></b><b> *) </b><b><span style="color:#880000;">marshal_as</span></b><b><</b><b><span style="color:blue;">std</span></b><b>::</b><b><span style="color:blue;">wstring</span></b><b>>(</b><b><span style="color:navy;">colormap_title</span></b><b>).</b><b><span style="color:#880000;">c_str</span></b><b>()).</b><b><span style="color:#880000;">utf8_str</span></b><b>()</b>);
</pre>
Basically, marshal the .NET String^ to a std::wstring to get it in
UCS-2 Unicode for C++, cast its contents to vtkUInt16* (small
cheat here, but VTK or the UTF-8 lib should accept wchar_t*
instead) and then pass that to vtkUnicodeString which will convert
it to UTF-8. Phew!<br>
<br>
Note that this also requires that the vtkTextProperty of the text
being drawn (in this case the Title of the vtkScalarBarActor) also
needs to have it's font family set to VTK_FONT_FILE and the font
file must point to a valid font:<br>
<title>Snippet</title>
<pre style="font-family:Consolas;font-size:13;color:black;background:white;"> <span style="color:navy;">m_VTKScalarBarActor</span>-><span style="color:#880000;">GetTitleTextProperty</span>()-><span style="color:#880000;">SetFontFamily</span>(<span style="color:#a000a0;">VTK_FONT_FILE</span>);
<span style="color:navy;">m_VTKScalarBarActor</span>-><span style="color:#880000;">GetTitleTextProperty</span>()-><span style="color:#880000;">SetFontFile</span>(<span style="color:#a31515;">"C:\\Windows\\winsxs\\amd64_microsoft-windows-font-truetype-arial_31bf3856ad364e35_6.1.7601.18528_none_d0a29012c3ff391b\\arial.ttf"</span>);
</pre>
You can use system TTF files (like I did here) or free ones, like
DejaVu fonts. I'm still looking at how to get the font file
automatically, since this was used as a test, I just searched my
C:\Windows folder to find an Arial font to try out. I'll post
again once I get this working. Note that I did actually test the
DejaVu fonts and they worked.<br>
<br>
If, on the other hand, you're happy with Latin1 encoded
characters, you can do this instead:<br>
<title>Snippet</title>
<pre style="font-family:Consolas;font-size:13;color:black;background:white;"> <span style="color:navy;">m_VTKScalarBarActor</span>-><span style="color:#880000;">SetTitle</span>(<span style="color:#880000;">marshal_as</span><<span style="color:blue;">std</span>::<span style="color:blue;">string</span>>(<span style="color:blue;">System</span>::<span style="color:blue;">Text</span>::<span style="color:blue;">Encoding</span>::<span style="color:#880000;">GetEncoding</span>(<span style="color:#a31515;">"ISO-8859-1"</span>)-><span style="color:#880000;">GetString</span>(<span style="color:blue;">System</span>::<span style="color:blue;">Text</span>::<span style="color:blue;">Encoding</span>::<span style="color:navy;">UTF8</span>-><span style="color:#880000;">GetBytes</span>(<span style="color:navy;">colormap_title</span>))).<span style="color:#880000;">c_str</span>());
</pre>
This can display fewer characters, but doesn't require that the
font family and font file be set.<br>
<br>
The same must be done to handle file names coming from a user in
case it also contains Unicode characters:<br>
<title>Snippet</title>
<pre style="font-family:Consolas;font-size:13;color:black;background:white;"> <span style="color:navy;">pWriter</span>-><span style="color:#880000;">SetFileName</span>(<span style="color:blue;">vtkUnicodeString</span>::<span style="color:#880000;">from_utf16</span>((<span style="color:blue;">const</span> <span style="color:blue;">vtkTypeUInt16</span> *) <span style="color:#880000;">marshal_as</span><<span style="color:blue;">std</span>::<span style="color:blue;">wstring</span>>(<span style="color:navy;">file_path</span>).<span style="color:#880000;">c_str</span>()).<span style="color:#880000;">utf8_str</span>());
</pre>
Unfortunately, the writers (and probably the readers) still use
fopen() in the internal code which can't handle UTF-8 encoded
filenames.<br>
So a filename like "C:\Users\serge\Desktop\Test Ύ Δ δ Ϡ.png"
becomes "C:\Users\serge\Desktop\Test ÎŽ Δ δ Ï .png". Not great,
but if you ignore the conversion, you get errors and the file
isn't written for writers or (presumably) read for readers (you
get Debug messages in the error console). So it's still better to
do the conversion and it will be ready when this is improved in
VTK. BTW, is this on the roadmap to be fixed?<br>
<br>
I hope that this will help others in the future.<br>
<div class="moz-signature">-- <br>
<meta http-equiv="content-type" content="text/html;
charset=UTF-8">
<title>Signature</title>
<a moz-do-not-send="true" href="http:://www.infolytica.com">www.infolytica.com
</a><br>
300 Leo Pariseau, Suite 2222, Montreal, QC, Canada, H2X 4B3<br>
(514) 849-8752 x236, Fax: (514) 849-4239 </div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
Powered by <a class="moz-txt-link-abbreviated" href="http://www.kitware.com">www.kitware.com</a>
Visit other Kitware open-source projects at <a class="moz-txt-link-freetext" href="http://www.kitware.com/opensource/opensource.html">http://www.kitware.com/opensource/opensource.html</a>
Please keep messages on-topic and check the VTK FAQ at: <a class="moz-txt-link-freetext" href="http://www.vtk.org/Wiki/VTK_FAQ">http://www.vtk.org/Wiki/VTK_FAQ</a>
Search the list archives at: <a class="moz-txt-link-freetext" href="http://markmail.org/search/?q=vtkusers">http://markmail.org/search/?q=vtkusers</a>
Follow this link to subscribe/unsubscribe:
<a class="moz-txt-link-freetext" href="http://public.kitware.com/mailman/listinfo/vtkusers">http://public.kitware.com/mailman/listinfo/vtkusers</a>
</pre>
</blockquote>
<br>
<div class="moz-signature">-- <br>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Signature</title>
<a href="http:://www.infolytica.com">www.infolytica.com </a><br>
300 Leo Pariseau, Suite 2222, Montreal, QC, Canada, H2X 4B3<br>
(514) 849-8752 x236, Fax: (514) 849-4239
</div>
</body>
</html>