unit Set_default_codepage;
{Originally setdefaultcodepages
http://wiki.freepascal.org/Lazarus_with_FPC3.0_without_UTF-8_mode}
{At cmd command line, can see active code page with "chcp";
can set active code page with "chcp nnn"}
{https://msdn.microsoft.com/en-us/library/windows/desktop/dd317752(v=vs.85).aspx
For both Windows code pages and OEM code pages, the code values 0x00 through 0x7F
correspond to the 7-bit ASCII character set. Code values 0x00 through 0x19 and 0x7F
always represent standardized control characters and 0x20 through 0x7E represent
standardized displayable characters.
Characters represented by the remaining codes, 0x80 through 0xff, vary among character sets.
Each character set includes different special characters, typically customized
for a language or group of languages.
Windows code page 1252 and OEM code page 437 are generally used in the United States.
All codepages - https://msdn.microsoft.com/en-us/library/windows/desktop/dd317756(v=vs.85).aspx
https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/
}
interface
procedure Set_console_fontsize(Fontsize: smallint {short});
{short - see https://msdn.microsoft.com/en-us/library/s3f49ktz.aspx
smallint - see https://www.freepascal.org/docs-html/ref/refsu4.html}
implementation
uses
Windows; // for types used in CONSOLE_FONT_INFOEX and function SetCurrentConsoleFontEx
const
LF_FACESIZE = 32;
type
CONSOLE_FONT_INFOEX = record
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms682069(v=vs.85).aspx
cbSize : ULONG;
nFont : DWORD; // https://msdn.microsoft.com/en-us/library/windows/desktop/ms683165(v=vs.85).aspx
dwFontSizeX : SHORT;
dwFontSizeY : SHORT;
FontFamily : UINT;
FontWeight : UINT; // range from 100 to 1000, in multiples of 100
FaceName : array [0..LF_FACESIZE-1] of WCHAR;
end;
{ Only supported in Vista and onwards!}
function SetCurrentConsoleFontEx(hConsoleOutput: HANDLE; bMaximumWindow: BOOL; var CONSOLE_FONT_INFOEX): BOOL;
stdcall; external 'kernel32.dll' name 'SetCurrentConsoleFontEx';
{***************************************************}
procedure Set_console_fontsize(Fontsize: smallint {short});
const // choose one
Codepage: smallint = 437; // OEM United States - see https://en.wikipedia.org/wiki/Code_page_437#Internationalization
{Font size & hi-chars are correct but only if not "uses crt"}
// Codepage: integer = CP_UTF8; // original (= 65001; see D:\Lazarus\fpc\3.0.0\source\rtl\win\wininc\redef.inc)
{Font size is correct but hi-char output is all boxes}
// Codepage: smallint = 1252; // ANSI Latin 1; Western European (Windows)
{Font size is correct but hi-char output is same as if "uses crt"}
var
New_CONSOLE_FONT_INFOEX : CONSOLE_FONT_INFOEX;
Rslt: boolean;
begin
SetConsoleOutputCP(Codepage);
SetTextCodepage(Output, Codepage);
FillChar(New_CONSOLE_FONT_INFOEX, SizeOf(CONSOLE_FONT_INFOEX), 0);
with New_CONSOLE_FONT_INFOEX do
begin
cbSize := SizeOf(CONSOLE_FONT_INFOEX);
nFont:= 0; // per http://stackoverflow.com/questions/33975912/how-to-set-console-font-to-raster-font-programmatically
{Values 0..100 don't seem to have any effect}
dwFontSizeX:= 0; {Values 0..100 don't seem to have any effect}
dwFontSizeY:= Fontsize;
FontFamily := FF_DONTCARE; // 5/5/2017 - http://stackoverflow.com/questions/33975912/how-to-set-console-font-to-raster-font-programmatically
{https://msdn.microsoft.com/en-us/library/cc250389.aspx
FF_DONTCARE = default font}
FaceName := 'Lucida Console';
{For the following FaceName, font size becomes default (12), regardless of Fontsize -
''
Consolas
Terminal}
FontWeight:= 400 {FW_NORMAL}; // http://stackoverflow.com/questions/33975912/how-to-set-console-font-to-raster-font-programmatically
{FW_NORMAL = 400}
{If <> 400 then Fontsize has no effect but hi-chars are displayed correctly
even if "uses crt"}
end;
Rslt:= SetCurrentConsoleFontEx(StdOutputHandle,false,New_CONSOLE_FONT_INFOEX);
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms686200%28v=vs.85%29.aspx
{2nd param doesn't seem to have any effect}
writeln('SetCurrentConsoleFontEx = ', Rslt);
{True regardless if "uses crt"}
end;
{#########################################}
end.