Implemented 2D Fonts

Selecting 2D fonts

In any class implementing mrpt::utils::CCanvas, the text font can be selected using CCanvas::selectTextFont. Next, text can be drawn with CCanvas::textOut. Note that one of the most commonly used implementations of CCanvas is CImage.

List of fonts

  • “6×13”
  • “6x13B” (bold)
  • “6x13O” (italic)
  • “9×15”
  • “9x15B” (bold)
  • “10×20”
  • “18x18ja” (Japanese, UNICODE character values)

The following image is taken from the example MRPT/samples/textFonts, refer to the source code for further details.

Fonts_render_engine

 

[spoiler title=”See C++ code” style=”2″]
#include <mrpt/utils.h>
#include <mrpt/gui.h>

using namespace mrpt;
using namespace mrpt::gui;
using namespace mrpt::utils;
using namespace std;

// ------------------------------------------------------
//				TestFonts
// ------------------------------------------------------
void TestFonts()
{
	CImage		img(400,300);

	img.filledRectangle(0,0,400,300, TColor(0x50,0x50,0x50) );

	int y = 10;
	img.selectTextFont("5x7");
	img.textOut(10,y,"Hello World! with font \"5x7\"", TColor::white); y+=20;

	img.selectTextFont("6x13");
	img.textOut(10,y,"Hello World! with font \"6x13\"", TColor::white); y+=20;

	img.selectTextFont("6x13B");
	img.textOut(10,y,"Hello World! with font \"6x13B\"", TColor::white); y+=20;

	img.selectTextFont("6x13O");
	img.textOut(10,y,"Hello World! with font \"6x13O\"", TColor::white); y+=20;

	img.selectTextFont("9x15");
	img.textOut(10,y,"Hello World! with font \"9x15\"", TColor::white); y+=20;

	img.selectTextFont("9x15B");
	img.textOut(10,y,"Hello World! with font \"9x15B\"", TColor::white); y+=20;

	img.selectTextFont("18x18ja");
	img.textOut(10,y,"MRPTのフォントは易しいです!", TColor::white); y+=20;

	img.selectTextFont("10x20");
	img.textOut(10,y,"Hello World! with font \"10x20\"", TColor::white); y+=20;


    CDisplayWindow		win1("MRPT - Demo of text fonts render");
    win1.setPos(10,10);
    win1.showImage( img );

    cout << "Push a key in the console or in the window to continue...";
    win1.waitForKey();
    cout << "Done" << endl;
}

// ------------------------------------------------------
//		MAIN
// ------------------------------------------------------
int main()
{
	try
	{
		TestFonts();
		return 0;
	} catch (std::exception &e)
	{
		std::cout << "MRPT exception caught: " << e.what() << std::endl;
		return -1;
	}
	catch (...)
	{
		printf("Untyped exception!!");
		return -1;
	}
}
[/spoiler]