[SOLVED] Changing Font.Name raises exception. lazarus / 31-LCL ---------------------------------------------------------------------- JCobb · 2018-04-08 08:36 When I try to change Canvas.Font.Name before sending text to it I get: Project proj_rawdisplay raised exception class 'External: SIGSEGV'... Actually I get this error whenever I change the Name of any font.  All the examples I've looked at online use the Font.Name property for changing the font.  Is there some reason this wouldn't work? The code is pretty long but here's the test code I've been using to work the problem: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function THexDisplay.DetectMonospace(fontStr: string) : boolean;var  w1,w2  : integer;  HFont  : TFont; begin    HFont.Create;    with HRawDisplay do     with Rimg do      begin         HFont.Assign(Canvas.Font);       HFont.Name:='Monospace';         Canvas.Font.Assign(HFont);           w1 := Canvas.TextWidth('M');             w2 := Canvas.TextWidth('i');             DetectMonoSpace := (w1 = w2);        end; end; I've tried with and without HFont.Create.  I've tried using Assign or just HFont := Canvas.Font, etc. HRawDisplay is a TGraphicControl class in another unit.  I've tried creating a local bitmap for this unit but it fails just as well. Essentially what I'm trying to do is make certain that a Monospace font is selected for the control just in case the default font is NOT monospace.  Using "IsMonospace" doesn't return useful results so I'm checking TextWidth which seems to work fine... At least until I try to change the Font.Name. In this case "Monospace" is a font that's available on my Linux system but it's not available on my Windows system.  So I want to find the next best usable monospace font and use that instead. Thanks in advance, Jeff ---------------------------------------------------------------------- taazz · 2018-04-08 08:50 I hope so otherwise it would be scary. In short hfont is an empty variable, what ever value it has, is invalid. you need to initialize it. Depending on your needs you can do Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function THexDisplay.DetectMonospace(fontStr: string) : boolean;var  w1,w2  : integer;  HFont  : TFont; begin  HFont := Canvas.Font;  .  .  .end; this will create an reference to the canvas' own font variable, any change to the HFont is made to the canvas.font directly. if you want to create a copy of the canvas.font then you do something along the lines of. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function THexDisplay.DetectMonospace(fontStr: string) : boolean;var  w1,w2  : integer;  HFont  : TFont; begin  HFont := TFont.create;  try  HFont.Assign(Canvas.Font);  .  .  .  finally    HFont.Free;  end;end; This creates a copy of what property values the canvas.font has. Any change to hfont is local only. Now when you assign hfont to canvas.font the property setter is smart enough to call the assign method eg Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   canvas.Font := HFont; is the same as Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   canvas.Font.Assign(HFont) ---------------------------------------------------------------------- Cyrax · 2018-04-08 08:51 You are creating instance of TFont class in wrong way. Change Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  HFont.Create;to Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  HFont := TFont.Create; Then there should be no exceptions anymore. ---------------------------------------------------------------------- jamie · 2018-04-08 14:05 My docs tells me that TFont.Pitch := fpFixed will force the font handler system to locate a font with a fixed pitch..  At less this works in Windows, not sure if that is also supported in other widgets?  In the case of windows, it would most likely use "Courier". ---------------------------------------------------------------------- wp · 2018-04-08 14:16 Quote from: jamie on April 08, 2018, 04:05:35 pmMy docs tells me that TFont.Pitch := fpFixed will force the font handler system to locate a font with a fixed pitch..  At less this works in Windows, not sure if that is also supported in other widgets?  In the case of windows, it would most likely use "Courier". How did you test this? If I add a TLabel to a form, set its Font.Pitch to fpFixed and run, nothing changes regarding the label's font. And this is the experience that I've always been having. ---------------------------------------------------------------------- dsiders · 2018-04-08 15:59 Quote from: wp on April 08, 2018, 04:16:23 pm Quote from: jamie on April 08, 2018, 04:05:35 pmMy docs tells me that TFont.Pitch := fpFixed will force the font handler system to locate a font with a fixed pitch..  At less this works in Windows, not sure if that is also supported in other widgets?  In the case of windows, it would most likely use "Courier". How did you test this? If I add a TLabel to a form, set its Font.Pitch to fpFixed and run, nothing changes regarding the label's font. And this is the experience that I've always been having. What I see on On Windows 8.1: It has no effect if a value has been assigned to TFont.Name (even the value Default). When TFont.Name is blank, changing TFont.Pitch selects the default monospace font. ---------------------------------------------------------------------- jamie · 2018-04-08 17:09 Yes, that is how I did the test, I just used a local TFont to test with.. I didn't know the NAME had to be cleared, in my case it was already cleared so I guess it work P.S.   I tested this on my old W2k PC, now on my Win10 and it did it with old D3... ---------------------------------------------------------------------- Bart · 2018-04-08 20:57 IIRC you can use EnumFontFamiliesEX to retreive mono-space fonts. I did once, but cannot find that code anymore (catastophic harddisk failure). Note: this was on Windows, not sure *nix worked as well. Bart ---------------------------------------------------------------------- JCobb · 2018-04-08 21:50 Turns out Assign versus := and knowing which did what was giving me one problem. Another stems from a resized dynamic array not being large enough.  But instead of getting this notice I was getting an exception.  Everything works now. Thanks for the help! Jeff