Make a textstring look like in MS Word or Libreoffice lazarus / 61-Beginners ---------------------------------------------------------------------- ChristianundCo · 2018-10-09 16:34 Hello there, sorry for my English. I'm programming a tool to edit subtitles. (simple textlines) For that I need a function that writes a textstring to canvas. Then I want to measure the width of the written text. Because I need the textwidth in pixel for later. Now I saw that a text looks in every app different. For example I have these two textlines Code: [Select]Eins zwei drei vier fünf sechs sieben acht neun ...................................................................... ..... I want them in "Tahoma" and BOLD. Now my problem. In my font (Tahoma, bold) they must have same length AT EVERY CHOOSEN SIZE. In MSWord (not Wordpad) and Libreoffice and some other apps they are of equal length. (ok, there is a pixel difference at Size 50 but that doesn't matter) It looks like this in every fontsize: https://s1.imagebanana.com/file/181009/51gakppp.jpg That's what I want! But in Windows Notepad and Freepascal MemoBox it looks something like this: https://s1.imagebanana.com/file/181009/SPFcJmY7.jpg The dots in the second line are of different length to the first line. Sometime more, sometimes less depending on the fontsize. Then I tried draw it on free pascal default canvas with the codeline 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.Quality:=  fqAntialiased; This is better but not good enough. Then I tried the bgrabitmap-9.9-package and the CanvasBGRA with a TBGRAVectorizedFontRenderer The result ist this: https://s1.imagebanana.com/file/181009/huqIPNji.jpg My function is 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 var    bmp: TBGRABitmap;    begin    a:=26; //FOntsize      bmp := TBGRABitmap.Create(ClientWidth,ClientHeight,ColorToRGB(clBtnFace));   bmp.FontRenderer := TBGRAVectorizedFontRenderer.Create;         bmp.CanvasBGRA.Font.Name := 'Tahoma';    bmp.CanvasBGRA.Font.Quality :=fqFineClearTypeBGR;    bmp.CanvasBGRA.Font.Style:=[fsBold];    bmp.CanvasBGRA.Font.Height:= round(-a* Font.PixelsPerInch/72);       bmp.CanvasBGRA.Brush.Color:=BGRA(255,0,0,255);    bmp.CanvasBGRA.TextOut(0,0,zeile1);    bmp.CanvasBGRA.TextOut(0,80,zeile2);     bmp.Draw(Canvas,0,0);      Form1.Edit1.Text:=IntToStr(bmp.CanvasBGRA.TextWidth(zeile1));     bmp.Free;end; Here are the 2 line nearly of equal length in every fontsize (a=12 ... a=45). But not so exact like in MS Word or other apps. But I want them equal. Is there a better way to get this 2 lines look like in word?  Note that I need a solution together with a GetTextwidth-function because later I need the pixelwidth. ---------------------------------------------------------------------- wp · 2018-10-09 17:41 Using a proportional font like Tahoma the two strings 'Eins zwei drei vier fünf sechs sieben acht neun' and '............................... ............................................' will almost never have the same length because every character has its own width, and the ratios of character widths will change with font size, at least due to round-off error. You can only "cheat" by drawing the first string word by word and add a pixel here and there such that the length difference is balanced. See attached project in which the widths of both strings should be equal within 1 pixel (even this could be avoided by adding the remaining pixel randomly to one of the spaces). Note that this is just a proof of concept, it does not mean that there may not be bug here and there. ---------------------------------------------------------------------- Blaazen · 2018-10-09 17:48 IMO it requires some special feature for variable space between words or, better, between characters. Lazarus doesn't have any, don't know about BGRA. ---------------------------------------------------------------------- ChristianundCo · 2018-10-09 17:56 Thank you for your answers and your code. But why does Word etc. it right? Nearly no difference between the two lines whether size is "12" or size is "45" ---------------------------------------------------------------------- wp · 2018-10-09 18:10 Quote from: ChristianundCo on October 09, 2018, 07:56:27 pmBut why does Word etc. it right? Nearly no difference between the two lines whether size is "12" or size is "45" By chance. Word probably draws the text word by word and uses a different width for the space character than is defined by the font itself (otherwise all programs should show the same text width). What is the line with the dots good for? You write in the first post that you want to measure the width of text. Why don't you use the method TextWidth(text) of the Canvas? ---------------------------------------------------------------------- ChristianundCo · 2018-10-09 18:20 Quote from: wp on October 09, 2018, 08:10:12 pmWhat is the line with the dots good for? That was a simple example to test if my pascal-tool works like MSWord or my subtitle editor (aegisub) Quote from: wp on October 09, 2018, 08:10:12 pm Why don't you use the method TextWidth(text) of the Canvas? In line 23 i use it. ---------------------------------------------------------------------- Handoko · 2018-10-09 18:35 Quote from: ChristianundCo on October 09, 2018, 07:56:27 pmBut why does Word etc. it right? Nearly no difference between the two lines whether size is "12" or size is "45" Not only Word and LibreOffice. I tested Inkscape and Krita Linux version, the dots and text are equally long if I resize the font. But not so on GIMP. I think the issue is depended on the text rendering engine they use. ---------------------------------------------------------------------- ChristianundCo · 2018-10-09 18:50 Yes! That I think, too. Therefore I made the code with the vectorfontrenderer. Because this was the only solution that comes nearly to MSWord and a few other tools. I forgot to say: If I use c# there is the line Code: C#  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; and the two lines are exactly of same length. without -> not But I don't want to translate my tool to c#