#include <mil.h>
#include "../../DmrUtil/C++/FontUtil.h"
#include "../../DmrUtil/C++/CharacterCreator.h"
#include "../../DmrUtil/C++/VisualizeFont.h"
#if M_MIL_USE_WINDOWS
#include <tchar.h>
#endif
static const MIL_INT STRING_LENGTH_MAX = 255;
void PrintHeader()
{
MosPrintf(MIL_TEXT("[EXAMPLE NAME]\n")
MIL_TEXT("DmrEditFontFile\n\n")
MIL_TEXT("[SYNOPSIS]\n")
MIL_TEXT("This program helps edit a Dot Matrix Reader (SureDotOCR) font (.mdmrf).\n")
MIL_TEXT("The user can modify a character or add a new character to\n")
MIL_TEXT("the font file interactively.\n\n"));
MosPrintf(MIL_TEXT("Note that predefined font files can be found in\n")
MIL_TEXT("your \\Matrox Imaging\\Contexts installation directory.\n\n"));
}
int MosMain()
{
PrintHeader();
MIL_ID MilApplication,
MilSystem;
MIL_TEXT_CHAR UserCharName[STRING_LENGTH_MAX] = MIL_TEXT("");
MappAlloc(M_NULL, M_DEFAULT, &MilApplication);
MsysAlloc(M_DEFAULT, M_SYSTEM_HOST, M_DEFAULT, M_DEFAULT, &MilSystem);
MIL_ID MilDmrContext = MdmrAlloc(MilSystem, M_DOT_MATRIX, M_DEFAULT, M_NULL);
MIL_TEXT_CHAR MyChar = ' ';
while(MyChar != 'e' || MyChar != 'E')
{
MosPrintf(MIL_TEXT("Press 'E' to exit or press any other key to edit a font.\n\n"));
MyChar = (MIL_TEXT_CHAR)MosGetch();
if (MyChar == 'e' || MyChar == 'E')
break;
MappControl(M_ERROR, M_PRINT_DISABLE);
MdmrImportFont(M_INTERACTIVE, M_DMR_FONT_FILE, MilDmrContext, M_NEW_LABEL,
M_IMPORT_ALL_CHARS, M_DEFAULT);
MappControl(M_ERROR, M_PRINT_ENABLE);
if (MdmrInquire(MilDmrContext, M_NUMBER_OF_FONTS, M_NULL) == 0)
{
MosPrintf(MIL_TEXT("File opening failed.\n\n"));
continue;
}
MIL_INT FontSizeX = MdmrInquireFont(MilDmrContext, M_FONT_INDEX(0), M_DEFAULT,
M_NULL, M_FONT_SIZE_COLUMNS, M_NULL);
MIL_INT FontSizeY = MdmrInquireFont(MilDmrContext, M_FONT_INDEX(0), M_DEFAULT,
M_NULL, M_FONT_SIZE_ROWS, M_NULL);
CCharCreator* pCharCreator = new CCharCreator(MilSystem, FontSizeX, FontSizeY);
bool AddCharacter = true;
while(AddCharacter)
{
CVisualizeFont DisplayFont(MilSystem, MilDmrContext);
DisplayFont.VisualizeFont();
MosPrintf(MIL_TEXT("Press 'D' to edit a character or press any ")
MIL_TEXT("other key to continue.\n\n"));
MyChar = (MIL_TEXT_CHAR)MosGetch();
if (MyChar != MIL_TEXT('d') && MyChar != MIL_TEXT('D'))
{
AddCharacter = false;
break;
}
MosPrintf(MIL_TEXT("Type a character value to edit then press <Enter>: "));
#if M_MIL_USE_LINUX
fgets(UserCharName, STRING_LENGTH_MAX, stdin);
UserCharName[strcspn(UserCharName, MIL_TEXT("\n"))] = MIL_TEXT('\0');
#else
_fgetts(UserCharName, _countof(UserCharName), stdin);
UserCharName[_tcscspn(UserCharName, MIL_TEXT("\n"))] = MIL_TEXT('\0');
#endif
MosPrintf(MIL_TEXT("\n"));
MappControl(M_ERROR, M_PRINT_DISABLE);
MIL_INT CharIndex = MdmrInquireFont(MilDmrContext, M_FONT_INDEX(0), M_DEFAULT,
UserCharName, M_CHAR_INDEX_VALUE, M_NULL);
MIL_INT InquireFontError = MappGetError(M_DEFAULT, M_CURRENT, M_NULL);
MappControl(M_ERROR, M_PRINT_ENABLE);
if (InquireFontError == M_NULL_ERROR)
{
bool NewChar = CharIndex == M_INVALID;
vector<MIL_UINT8> DotChar(FontSizeX * FontSizeY, 0xFF);
if (NewChar)
MdmrControlFont(MilDmrContext, M_FONT_INDEX(0), M_DEFAULT, UserCharName,
M_CHAR_ADD, M_DEFAULT, &DotChar[0]);
else
{
MdmrInquireFont(MilDmrContext, M_FONT_INDEX(0), M_DEFAULT, UserCharName,
M_CHAR_TEMPLATE + M_TYPE_MIL_UINT8, &DotChar[0]);
pCharCreator->LoadCharacter(DotChar);
}
mstring CharName;
FontUtil::GetCharName(MilDmrContext, UserCharName, M_NULL, &CharName);
mstring CharHexName;
FontUtil::GetCharName(MilDmrContext, UserCharName, M_HEX_UTF16_FOR_ALL, &CharHexName);
if (pCharCreator->CreateCharacterInteractive(CharName.c_str(), CharHexName.c_str(), NewChar))
{
MdmrControlFont(MilDmrContext, M_FONT_INDEX(0), M_DEFAULT, UserCharName,
M_CHAR_TEMPLATE, M_DEFAULT, &(pCharCreator->DotCharMatrix())[0]);
}
else if (NewChar)
{
MdmrControlFont(MilDmrContext, M_FONT_INDEX(0), M_DEFAULT, CharName.c_str(),
M_CHAR_DELETE, M_DEFAULT, M_NULL);
MosPrintf(MIL_TEXT("Unable to add new character because it was empty.\n\n"));
}
else
MosPrintf(MIL_TEXT("Unable to replace character because the new character was empty.\n\n"));
}
else
MosPrintf(MIL_TEXT("Unable to edit the supplied character. The character may ")
MIL_TEXT("be invalid.\n\n"));
}
delete pCharCreator;
FontUtil::ExportFont(MilDmrContext, 0);
MdmrControl(MilDmrContext, M_FONT_DELETE, M_FONT_INDEX(0));
}
MdmrFree(MilDmrContext);
MsysFree(MilSystem);
MappFree(MilApplication);
return 0;
}