How do I make this code cross platform? lazarus / 21-General ---------------------------------------------------------------------- Joanna · 2021-09-08 06:15 Today i tried to compile my program using Lazarus client installed on Fedora Linux without success. This code unfortunately relies upon the windows unit. Does anyone know how to make it cross platform ? The current code was written  using win 7 os and Lazarus 1.6.4 r54278 FPC 3.0.2 x86_64-win64-win32/win64 if that matters. Here is the code. 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 STRING_SIZE( CONST MY_STRING: STRING; FONT: TFONT; CONST MEASURING_WIDTH: BOOLEAN) : WORD;         VAR          TMPCANVAS: TCANVAS;          DC: HDC;BEGINIF MY_STRING = '' THEN EXIT (0);DC := GETDC(0);  TMPCANVAS := TCANVAS.CREATE;  TMPCANVAS.HANDLE := DC;  TMPCANVAS.FONT := FONT;  TMPCANVAS.FONT.PIXELSPERINCH := FONT.PIXELSPERINCH;  IF MEASURING_WIDTH     THEN RESULT := TMPCANVAS.TEXTWIDTH(MY_STRING)     ELSE RESULT := TMPCANVAS.TextHeight(MY_STRING) ;  RELEASEDC(0, TMPCANVAS.HANDLE);  FREEANDNIL(TMPCANVAS);END; ---------------------------------------------------------------------- winni · 2021-09-08 09:04 Hi! Use an existing canvas . At least the main form owns a canvas: 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 STRING_SIZE(C: TCanvas;CONST MY_STRING: STRING; FONT: TFONT; CONST MEASURING_WIDTH: BOOLEAN) : WORD;         VAR          TmpFont : TFont;BEGINIF MY_STRING = '' THEN EXIT;  TmpFont := C.Font;  C.FONT := FONT;  IF MEASURING_WIDTH     THEN RESULT := C.TEXTWIDTH(MY_STRING)   ELSE RESULT := C.TextHeight(MY_STRING) ;  C.Font := TmpFont;END; Winni ---------------------------------------------------------------------- balazsszekely · 2021-09-08 09:12 @Joanna QuoteToday i tried to compile my program using Lazarus client installed on Fedora Linux without success. This code unfortunately relies upon the windows unit. Does anyone know how to make it cross platform ? The current code was written  using win 7 os and Lazarus 1.6.4 r54278 FPC 3.0.2 x86_64-win64-win32/win64 if that matters. Here is the code. Replace unit "Windows" with units "LCLType, LCLIntf". ---------------------------------------------------------------------- Joanna · 2021-09-09 04:36 Thank you for your answers will try to follow the suggestions. ---------------------------------------------------------------------- Warfley · 2021-09-09 13:52 Quote from: winni on September 08, 2021, 11:04:09 amHi! Use an existing canvas . At least the main form owns a canvas: 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 STRING_SIZE(C: TCanvas;CONST MY_STRING: STRING; FONT: TFONT; CONST MEASURING_WIDTH: BOOLEAN) : WORD;         VAR          TmpFont : TFont;BEGINIF MY_STRING = '' THEN EXIT;  TmpFont := C.Font;  C.FONT := FONT;  IF MEASURING_WIDTH     THEN RESULT := C.TEXTWIDTH(MY_STRING)   ELSE RESULT := C.TextHeight(MY_STRING) ;  C.Font := TmpFont;END; Winni Your code is missing the pixels per inch line: 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 TMPCANVAS.FONT.PIXELSPERINCH := FONT.PIXELSPERINCH;And this line is important, because when assigning the font, the pixel per inch will be computed according to  the DPI configuration. So if you have two forms on two different monitors with different DPI configurations, then the result will be different depending which canvas you use, if it's the high DPI forms canvas or the low DPI forms canvas. Also your code will not restore the original font, because you just fetch a reference in tmpfont and the setter for C.Font will call assign on the exact same object ---------------------------------------------------------------------- Joanna · 2021-09-10 01:08 i did this. 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 STRING_SIZE( CONST MY_STRING: STRING; MY_FONT: TFONT; MY_CANVAS: TCANVAS; CONST MEASURING_WIDTH: BOOLEAN): WORD;BEGINIF MY_STRING = '' THEN EXIT (0);  MY_CANVAS.FONT := MY_FONT;  MY_CANVAS.FONT.PIXELSPERINCH := MY_FONT.PIXELSPERINCH;  IF MEASURING_WIDTH     THEN RESULT := MY_CANVAS.TEXTWIDTH(MY_STRING)     ELSE RESULT := MY_CANVAS.TextHeight(MY_STRING) ;END; [/code] Another thing I dont understand is the original code used a canvas but also HDC from windows unit. Is it not possible to use a canvas by itself without this ... TMPCANVAS.HANDLE := DC; ? I don't really understand handles very well ---------------------------------------------------------------------- skalogryz · 2021-09-10 05:18 here's my version of cross-platform 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 STRING_SIZE( CONST MY_STRING: STRING; FONT: TFONT; CONST MEASURING_WIDTH: BOOLEAN) : WORD;var  sz: TSize;  b : TBitmap;begin  b := TBitmap.Create;  try    b.SetSize(1,1);    b.Canvas.Font := Font;  sz := b.Canvas.TextExtent(My_String);    if MEASURING_WIDTH then Result := sz.Width    else Result := sz.Height;  finally    b.Free;  end;end;  Or something even more-old school like: 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 var  b :TBitmap = nil; function GetInterlanCanvas: TCanvas;begin  if not Assigned(b) then begin     b := TBitmap.Create;    b.SetSize(1,1);  end;  Result := b.Canvas;end; function STRING_SIZE( CONST MY_STRING: STRING; FONT: TFONT; CONST MEASURING_WIDTH: BOOLEAN) : WORD;var  sz: TSize;  cnv : Tcanvas;begin  c := GetInternalCanvas;  cnv.Font := Font;  sz := cnv.TextExtent(My_String);  if MEASURING_WIDTH then Result := sz.Width  else Result := sz.Height;end;  ...initialization finalization  b.Free;  Quote from: Joanna on September 10, 2021, 03:08:02 amAnother thing I dont understand is the original code used a canvas but also HDC from windows unit. Is it not possible to use a canvas by itself without this ... TMPCANVAS.HANDLE := DC; ? I don't really understand handles very well The purpose of the original code is to do less allocations as possible. In order to TCanvas class to operate it need a system handle. (all "text measurement" calls would be done via system API. system API depends on the Handle of the "graphics context" available). There are several ways to obtain the graphics context 1) create yourself (i.e. for drawing on a bitmap) 2) get the context during paint event for a window / control 3) do what the example does and get a global handle of the screen GetDC(0) Assumption is made that the 3d approach requires less memory reallocation. As soon as the handle is received it's given to TCanvas class. 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 TMPCANVAS.HANDLE := DC; and only after that TCanvas methods can be used in order to measure the text. ---------------------------------------------------------------------- Warfley · 2021-09-10 13:32 Quote from: Joanna on September 10, 2021, 03:08:02 ami did this. 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 STRING_SIZE( CONST MY_STRING: STRING; MY_FONT: TFONT; MY_CANVAS: TCANVAS; CONST MEASURING_WIDTH: BOOLEAN): WORD;BEGINIF MY_STRING = '' THEN EXIT (0);  MY_CANVAS.FONT := MY_FONT;  MY_CANVAS.FONT.PIXELSPERINCH := MY_FONT.PIXELSPERINCH;  IF MEASURING_WIDTH     THEN RESULT := MY_CANVAS.TEXTWIDTH(MY_STRING)     ELSE RESULT := MY_CANVAS.TextHeight(MY_STRING) ;END; [/code] Another thing I dont understand is the original code used a canvas but also HDC from windows unit. Is it not possible to use a canvas by itself without this ... TMPCANVAS.HANDLE := DC; ? I don't really understand handles very well But be careful, this code permanently changes the font of the canvas used.