Making fpSpreadsheetGrid RTL Compatible lazarus / 31-LCL ---------------------------------------------------------------------- shobits1 · 2016-01-22 03:00 This is a continuation discussion which originally started at How to make gap between lines in StringGrid. and since discussion shifted from topic this thread was created as proposed by member wp. any help would be appreciated  . ---------------------------------------------------------------------- shobits1 · 2016-01-22 03:03 Quote from: wp on January 21, 2016, 10:29:10 pmThanks for the patch, I'll have a look at it afterwards. You are approaching the real problems... Since in the WorksheetGrid every character can have a different font, the text is drawn character by character, instead of in one piece with the Canvas.TextOut. Could you test the attached little program (BiDi TextOut) whether any text that you enter in the edit control at the bottom in RTL mode (checkbox marked) is drawn correctly in the 2nd paintbox? The top paintbox draws the text in one piece, the lower one character by character, similarly like fpspreadsheet does. In my system the upper text is always drawn in left-to-right direction, the lower one in right-to-left, but I think this is because the canvas on my system is always LTR. I must understand this. In your system both texts should be the same. I am attaching another little project (fps-bidi) which compares a StringGrid with WorksheetGrid in LTR and RTL. At least in my system the screenshot shows that there is a LOT to be done... Ah, and BTW: I suppose this BiDi discussion will take some time. I would appreciate if you could put this into a separate thread because nobody will find it under its current header. As you can see in screenshots, the text didn't paint right. ---------------------------------------------------------------------- shobits1 · 2016-01-22 03:25 Quote from: wp on January 22, 2016, 12:05:34 amI looked at your patch, thanks again. I had to debug it, though, because there was a clipping rectangle in DrawAllCells which was not adapted to RTL. And the selection rectangle is not drawn correctly: drag the mouse across several cells horizontally and see that the selection does not follow the mouse correctly as it does vertically or in LTR mode - I did not yet find the bug in there. Anyway, I uploaded the new version to trunk. When you proceed, it will save time if you would always update your svn before beginning. Just in case you don't know... I think your previous post already answered the question above: the character-by-character painting routine is the reason why your Arabian characters are reversed. There is a single painting procedure for all text output, InternalDrawRichText in fpsvisualutils. Don't be scared, it is huge, but the main painting operation occurs in the local procedure DrawLine, and there are also some coordinate calculations below in the sections commented by "2a)..." and "2b)..." Be careful that RTL must work also with rotated text; you can create a cell with rotated text by 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 Grid.Cells[1,1] := 'Text';Grid.TextRotation[1,1] := rt90DegreeClockwiseRotation;  // or rt90DegreeCounterClockwiseRotation  // or: rtStacked  After that we should look at DrawRow, another "monster", sorry, but there is another clipping rectangle which prevents painting of the cells at the left in RTL mode. Your testing code should have ShowHeaders=true, otherwise you won't see this. It looks that seeking for that ColRowToOffset is a good idea for finding the points which have to be modified. I know there is still bugs, but I'm concentrated on how it looks first, since user will not edit or select cells rather drag and drop object on cells. there is also a bug which persist through all BiDi modes, when you double click  on merged cells to edit they display there own value - I don't know how to describe but lets say on 3x3 merged cells try and double click on 2x3 and you will see what I mean. anyway; I always updates to latest version.  I already traced InternalDrawRichText in fpsvisualutils. and honestly I don't know if Arabic text would printed correctly using character- by-character painting routine; you see Arabic character changes form  according to preceding and next character. maybe if someone knowledgeable is this would help. ---------------------------------------------------------------------- shobits1 · 2016-01-22 03:57 Quote from: wp on January 22, 2016, 12:05:34 amI looked at your patch, thanks again. I had to debug it, though, because there was a clipping rectangle in DrawAllCells which was not adapted to RTL. And the selection rectangle is not drawn correctly: drag the mouse across several cells horizontally and see that the selection does not follow the mouse correctly as it does vertically or in LTR mode - I did not yet find the bug in there. this patch should correct the selection rectangle bug. ---------------------------------------------------------------------- wp · 2016-01-22 06:37 Quote from: shobits1 on January 22, 2016, 04:25:31 amhonestly I don't know if Arabic text would printed correctly using character-by- character painting routine; you see Arabic character changes form  according to preceding and next character. maybe if someone knowledgeable is this would help. Characer-by-character painting came in as a simplification for "rich- text formatted cells". In these cells, the font changes after a specified character position. In this way, for example, the chemical formula of water, H2O, can be written with the subscript 2. Maybe it is necessary to keep blocks with the same font together. Example: "normal bold" is currently painted as (in LTR) "n", ..., "l", Switch font to bold, "b", ..., "d"; Instead, we should paint it as "normal", switch font, "bold". I think I can change this. Is this feature of any use in a purely Arabian text part (of course you still need it if you embed chemical formulas)? But the main question I am having: How about numbers? Are they inverted too? Your screenshot indicates that they are not. How about times, i.e. does "12:00" appear like that or like "00:21"? Telephone numbers? I need an exact definition of how such characters behave in RTL text. Quote from: shobits1 on January 22, 2016, 04:25:31 amthere is also a bug which persist through all BiDi modes, when you double click  on merged cells to edit they display there own value - I don't know how to describe but lets say on 3x3 merged cells try and double click on 2x3 and you will see what I mean.I don't understand what you mean by "3x3" and "2x3", but I see that if I double-click at any cell of the merged block of my demo the text appears in an edit box at the clicked location. This is certainly confusing because the unedited text is still visible. I think it is required to modify the cell editor in case of merged cells such that the edit control extends across the entire cell block. ---------------------------------------------------------------------- wp · 2016-01-22 09:34 Quote from: shobits1 on January 22, 2016, 04:57:51 amQuote from: wp on January 22, 2016, 12:05:34 am... the selection rectangle is not drawn correctly: drag the mouse across several cells horizontally and see that the selection does not follow the mouse correctly as it does vertically or in LTR mode - I did not yet find the bug in there. this patch should correct the selection rectangle bug. Applied with modifications: I added an overloaded CellText with all corners as parameters which takes care of the RTL mode, it avoids some of the "if IsRightToLeft" constructs. I'll focus on the InternalDrawRichText procedure today and try to avoid drawing the text as individual characters. To avoid merge conflicts you should not work at this procedure as well. Maybe go to the DrawRow procedure instead. ---------------------------------------------------------------------- shobits1 · 2016-01-22 11:06 Quote from: wp on January 22, 2016, 07:37:20 amBut the main question I am having: How about numbers? Are they inverted too? Your screenshot indicates that they are not. How about times, i.e. does "12:00" appear like that or like "00:21"? Telephone numbers? I need an exact definition of how such characters behave in RTL text. indeed everything is painted in reverse, time 12:00 -> 00:21 * date 12/11/2015 -> 5012/11/21 * numbers 013456789 -> 9876543210 (using BiDi TextOut project) in fps-bidi project things not the same; when entering time/date/numbers only they correctly display; but adding Arabic text position of words is wrong (e.g هذا 2015 عدد prints like ددع 2015 اذه word with same color are the same) ---------------------------------------------------------------------- wp · 2016-01-22 21:38 In the new revision the drawing code for "rich-text" has been refactored, the text is no longer painted character by character, but by character blocks having the same font; in the normal case where the entire cell has a single font the text is passed to the canvas at once. And since the canvas' TextOut can handle RTL properly I hope that the RTL painting issues should be gone now. (Of course, still a lot to do...) Please test. ---------------------------------------------------------------------- shobits1 · 2016-01-22 23:12 Arabic Text display correctly but there is problem with horizontal and vertical alignment (no problem observed with English text). The Alignment has this pattern: Text RotationHA.LeftHA.CenterHA.Right     VA.TopVA.CenterVA.BottomtrHorizontalAbout in Center     Shifted toward right     CorrectCorrectCorrectCorrectrt90DegreeCounterClockwis eRotationCorrectCorrectCorrectCorrectShifted toward top     About in Center     rt90DegreeClockwiseRotationCorrectCorrectCorrectAbout in Center     Shifted toward bottom     Correct I still trying to fix the clipping problem when header is displayed; so far has small success but merged cells are displayed over the header(far right column) when scrolling and also have some issue with Text Overflow. BTW: there is small bug concerning the focus rectangle and is only seen when headers are displayed and you try to select merged cells (the focus rectangle shifts by one cell); quick fix is to replace line 2302 (also fix a typo col/row order) 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 R := CellRect(r1, c1, r2, c2);  // wrong order of row/colwith 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 // using GetGridCol/GetGridRow to correct focus rectangle when Headers are displayedR := CellRect( GetGridCol(c1), GetGridRow(r1), GetGridCol(c2), GetGridRow(r2)); ---------------------------------------------------------------------- wp · 2016-01-22 23:29 Quote from: shobits1 on January 23, 2016, 12:12:34 amthere is small bug concerning the focus rectangleGrrrrrh, my fault... It is in trunk. No idea at the moment regarding the alignment issues. Stacked text currently is not working. Is this of any interest in your language? If yes, how would the text have to run, from top down or from bottom up? ---------------------------------------------------------------------- wp · 2016-01-23 11:20 The text renderer is using different ways to calculate the width of a string: in the preparation step (needed to find word breaks) the width is calculated as the sum of the individual character widths, in the drawing step the entire string is used. Can it be that in Arabian fonts these two methods lead to different results? Is there a chance that the rich-text feature will be used for Arabian texts. Suppose you want to highlight some part of the string by setting it bold. Example text (using "normal" characters), the word "bold" should be in bold characters: "This is bold". The fpspreadsheet notation would be "This is bold". The current implementation will draw the string "This is" in standard font, and "bold" in bold font. What about the point where both string meet? You are writing that Arabian characters depend on pre- or succeeding character. When the first string ("This is ") is drawn the last character (a space here, but it could be anything) does not know what will follow, and similarly, when the second string is drawn the first character ("b") does not know what was written before. Can this be an issue with an Arabian text? ---------------------------------------------------------------------- shobits1 · 2016-01-23 13:07 Sorry for delayed response, I'm not feeling well  . Quote from: wp on January 23, 2016, 12:20:19 pmThe text renderer is using different ways to calculate the width of a string: in the preparation step (needed to find word breaks) the width is calculated as the sum of the individual character widths, in the drawing step the entire string is used. Can it be that in Arabian fonts these two methods lead to different results? yes Arabic sum of individual characters width is different from the width of group of the same characters together; the attached patch works (the alignment is correct) but I don't knowif it'll break rich text functionality. Quote from: wp on January 23, 2016, 12:20:19 pmIs there a chance that the rich-text feature will be used for Arabian texts. Suppose you want to highlight some part of the string by setting it bold. Example text (using "normal" characters), the word "bold" should be in bold characters: "This is bold". The fpspreadsheet notation would be "This is bold". The current implementation will draw the string "This is" in standard font, and "bold" in bold font. What about the point where both string meet? You are writing that Arabian characters depend on pre- or succeeding character. When the first string ("This is ") is drawn the last character (a space here, but it could be anything) does not know what will follow, and similarly, when the second string is drawn the first character ("b") does not know what was written before. Can this be an issue with an Arabian text? for this application I'll use the very basics for rich text (like a whole word bold, color... but not for individual characters in the word); for others maybe they'll need per character highlighting. in Arabic you can think of any punctuation mark as words separator (characters changes form only to form individual words and word doesn't affect other word form) so if two words meet (no punctuation between them) the last char of the first string and the first char from second  string will change from - hope you understand what I mean; ---------------------------------------------------------------------- wp · 2016-01-23 13:49 New trunk version should work fine regarding RTL text alignment and orientation. Rich text is working (at least with LTR fonts), the OpenOffice test file loads fine like  before the major reconstruction. Sorry, I did not use your patch - my own code was already finished but needed testing. Stacked text still not working because the old code had drawn individual characters which is no longer true now. To be fixed soon. ---------------------------------------------------------------------- shobits1 · 2016-01-23 14:22 alignment works right; thought there is bug with rich text (tested it for the first time) painting resulting in removing spaces. in RTL: you have : "Long          English Text"   will display as   "English TextLong" you have : "Long     English     Text"   will display as   "TextEnglishLong" I'm not talking about words order (since in Arabic order is good) but spaces are completely gone. in LTR: you have : "Long          English Text"   will display as   "Long English Text" see other spaces are omitted. ---------------------------------------------------------------------- wp · 2016-01-23 16:12 Just looked into the DrawRow method and fixed text overflow and clipping errors. The big bugs are gone now. Looking for the dropped spaces afterwards. ---------------------------------------------------------------------- shobits1 · 2016-01-23 16:53 you beat me to it  ; though there is bug when hiding headers in RTL mode. this small patch will fix that and also correct appearance of cells with overflow text painted on them (fix grid lines not show). ---------------------------------------------------------------------- wp · 2016-01-23 17:25 Thanks. Its in trunk (although I don't see any effect of the "downto" in the patch") ---------------------------------------------------------------------- shobits1 · 2016-01-23 17:34 Thank you, the differences of using downto in screenshot - though it is minuscule I like it to be persistent between LTR & RTL. ---------------------------------------------------------------------- wp · 2016-01-23 18:17 Aaah - thanks. When testing this I saw that the my refactored code runs into an infinite loop when column A gets very narrow, to be precise, narrower than the width of a single character. Fixed in the recent trunk version. ---------------------------------------------------------------------- wp · 2016-01-23 20:04 Found the "space eater": it was not my refactored RTL code but the code which parses the HTML phrase for extracting the rich text parameters. Should be working now. ---------------------------------------------------------------------- shobits1 · 2016-01-23 20:28 just tested it; works in LTR but not in RTL. ---------------------------------------------------------------------- wp · 2016-01-23 20:50 Quote from: shobits1 on January 23, 2016, 09:28:56 pmjust tested it; works in LTR but not in RTL. No, it is, you just don't see where the text starts. Replace the 'Long          English Text' by 'Longxxx          English Text' and see the gap between the "xxx" and the "English Text". Note that for some reason which I don't know the characters in words using a Latin font are always drawn from left to right whatever the Bidi mode is, probably there is some flag in the font file which marks these character as LTR. Conversely, if I paste some Arabian text from the internet to my program it is always drawn from right to left even if my BiDi is LTR. Using the fpspreadsheet rich text feature separates the text into a segment for each contained font, the part "Long" uses bold font, the "xxx       English text" is standarad font. "Long" is drawn first, in RTL at the right edge of the cell. Then the "xxx    English text" is drawn immediately to its left. ---------------------------------------------------------------------- shobits1 · 2016-01-23 21:42 sorry but it looks like it partially working. the Arabic text is all the same but the place of bold word 1: is the simple form string (something like 'word1    word2     word3') 2: is the all word bold string (word1    word2     word3) works great. 3: the first word is bold but result are word1word2 word3 4: middle word is bold result are word1word2word3 the English text with bold word in middle also don't work (it is from Grid.Cells[6, 5] := 'Long    English    Text'; ) last cell is as you described in the post above. ---------------------------------------------------------------------- wp · 2016-01-23 22:13 Do you mean that the spaces are missing? At first sight the English text with bold in the middle seems to be "correct", i.e. in-line with the way the algorithm works: It consists of 3 parts "long    ", "English" (bold), "    Text". "Long   " is printed first, in RTL this is at the right of the cell - if you used default alignment or right alignment the text would not begin at the cell border because of the spaces. Immediately to the left of it, there is "English" because it has no spaces, and again, immediately to the left of "English there is "   Text", again no spaces because the word has them at the other side. But the Arabian text is drawn from right to left, therefore, I would not expect that the spaces are gone unless something is wrong in the scanning process. Could you upload the project, or at least, paste here the code you used to fill the cells (incl Arabian texts)? ---------------------------------------------------------------------- shobits1 · 2016-01-23 22:25 OK, project attached. ---------------------------------------------------------------------- wp · 2016-01-24 00:09 It's terrible, I don't know the order of the bytes, and if I press the arrow key to the left the cursor goes to the right and suddenly back to the left of the string. Could you try to construct the string by means of Pascal string append operations? This way I'd know that the string is really correct. I mean 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 const  ArabianStr1 = 'ABC';  // in Arabian of course  ArabianStr2 = 'EFG';  ArabianStr3 = 'HIJ';begin  Grid.Cells[1,1] := ArabianStr1 + ' ' + ArabianStr2 + ' ' + ArabianStr3;  // this should paint as "JIH GFE CBA"  Grid.Cells[1,2] := '' + ArabianStr1 + '' + ' ' + ArabianStr2 + ' ' + ArabianStr3; ---------------------------------------------------------------------- shobits1 · 2016-01-24 01:10 Quote from: wp on January 24, 2016, 01:09:47 amIt's terrible, I don't know the order of the bytes, and if I press the arrow key to the left the cursor goes to the right and suddenly back to the left of the string. Confusing hahh; well since about all the IDE I have worked with have the same problem I have learned and adapted myself to use it this way  . anyway here is the updated file ---------------------------------------------------------------------- wp · 2016-01-24 11:31 Seems to be working now, but I am not sure if this fix is generally valid. The problem is that spaces seem to have a life of their own in RTL, sometimes they jump to the front although they should be at the end. The current code strips leading and trailing spaces from the text and draws only the naked text at a position shifted by the leading space and signals to the drawing routine that the next text will be offset by the trailing space. Painting a string like s := '   ' + W + '        ' at location (x,y) is done as follows (W should not contain any leading/trailing spaces any more): - Count the leading and trailing spaces, multiply by the width of the space character to get the width of all leading and trailing spaces, wlead and wtrail, respectively - Goto (x,y) - In horizontal drawing reduce x by wlead (y accordingly in case of vertical drawing) - draw W in RTL way - reduce x by wtrail - this is where the next chunk of text will begin. I'd propose that you construct the html strings for rich-text cell texts always in this way because here it is clear which spaces are leading and which are trailing. The strings used in your first code, e.g., look different from the ones provided in the second post. It could be that the issue still exists with other punctuation - I did not check it, I was just happy to get this far. ---------------------------------------------------------------------- shobits1 · 2016-01-24 15:54 Great thanks to you  Seems to work great now, thank you for your time spend on help me and expending fpSpreadsheetGrid - I'll work with it and reports any new bugs/glitches. on side note (maybe off topic), how does MS-Excel handle rich text in cells, just tested it for curiosity and he handles per character Arabic rich text with no problem also he displays mixed Arabic and English on the same line correctly (not that I need any of those features but understand how he works may help fpSpreadsheet). anyway thanks again for the hard work. ---------------------------------------------------------------------- wp · 2016-01-24 17:02 Quote from: shobits1 on January 24, 2016, 04:54:13 pmon side note (maybe off topic), how does MS-Excel handle rich text in cells, just tested it for curiosity and he handles per character Arabic rich text with no problem also he displays mixed Arabic and English on the same line correctly (not that I need any of those features but understand how he works may help fpSpreadsheet). I don't know. I almost have the feeling that TCanvas of lazarus has an issue with mixed LTR and RTL characters. When you edit a mixed LTR/RTL text to Excel do you experience the same difficulties as with the IDE editor? Or what happens if you use other well-established editors, such as Notepad++? If not, this would be an indication of a TCanvas bug. Unfortunately, nobody of the core developers is RTL, so it is hard to fix. Your contribution would certainly be very welcome. Ah, one thing to note: Embedding HTML into strings is just the gui method of fpspreadsheet for rich-text. Internally, you pass the plain text and the rich-text parameter array to the method WriteText of the worksheet. It is no problem to call the direct method from the grid - this would avoid typing the LTR html tags into the RTL string. Maybe you could try this (not tested, maybe some typo here and there...): 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  richTextParams: TsRichTextParams;  normalFontIndex: Integer;  boldFontIndex: Integer;  fnt: TsFont;  gridCol, gridRow: Integer;  RTLString: string;begin  // String to be added ...  RTLString := 'هذا النص   بالعربية';    // ... to this cell of the grid  gridCol := 7;  gridRow := 5;   // We will use two fonts: the default font of the worksheet and the same one again, but in bold  fnt := Grid.Workbook.GetDefaultFont;  // the default font has index 0  normalFontIndex := 0;  boldFontIndex := Grid.Workbook.AddFont(fnt.FontName, fnt.Size, [fssBold], fnt.Color, fnt.Position);   // Set up the richt-text parameters  SetLength(richTextParams, 2);  richTextParams[0].FirstIndex := 1;   // I want to have the first word bold --> 1st bold character has index 1  richTextParams[0].FontIndex := boldFontIndex;  richTextParams[1].FirstIndex := 4;  // The first word is 3 characters long --> the font will change at index 4  richTextParams[1].FontIndex := normalFontIndex;   // Add the plain text (RTLString) to the worksheet behind the grid and use the specified rich-text parameters  // Note: col and row indexes are interchanged in grid and worksheet  Grid.Worksheet.WriteText(Grid.GetWorksheetRow(gridRow), Grid.GetWorksheetCol(gridCol), RTLString, richTextParams); ---------------------------------------------------------------------- shobits1 · 2016-01-24 22:51 Code works fine but need to pay attention to string length. I don't have Notepad++ at the moment but I do remember having an even terrible problem in Np++, the displayed text selection and the real selected text are different until switching to RTL mode. Sublime Text displays Arabic text as separated characters almost like the problem when you write character by character. Microsoft Visual Studio Code is an other editor and behave almost like Lazarus IDE (except when hitting home button the cursor goes to the 1st character instead of debut of line). If I remember right Delphi (maybe XE4) behaves like Lazarus IDE. As long as I remember every code editor have variety of problems with RTL language, maybe because programming language are natively LTR and code editors implementation don't bother with RTL. so in general I don't think its TCanvas bug. Quote from: wp on January 24, 2016, 06:02:08 pmUnfortunately, nobody of the core developers is RTL, so it is hard to fix. Your contribution would certainly be very welcome. Sure I'd like to help, even though I have no experience with the internals of LCL; I'll try and report any bugs/fixes when working with Lazarus. Quote from: shobits1 on January 24, 2016, 04:54:13 pmon side note (maybe off topic), how does MS-Excel handle rich text in cells, just tested it for curiosity and he handles per character Arabic rich text with no problem also he displays mixed Arabic and English on the same line correctly (not that I need any of those features but understand how he works may help fpSpreadsheet). ِNew finding, after playing with excel a bit I see that the whole grid has its own Bidi Mode and each cell has its own  (see screenshot) and Excel selects the preferred RTL/LTR mode according to the first character or you could force Right-to-Left or Left-to-Right; but still I don't know he does per character rich text even his sibling product MS-Word can't handle that  (screenshot has the text use in Excel) EDIT: BTW how do you force cell format: I'd like a cell to have this value 001234 but the grid keep changing it to 1234. also how to set Date format I'd like cell to have 11/12/15 (but grid displays 4/5). any information about forced cell type and masks would be helpful. ---------------------------------------------------------------------- engkin · 2016-01-24 23:04 Quote from: shobits1 on January 24, 2016, 11:51:21 pmQuote from: shobits1 on January 24, 2016, 04:54:13 pmon side note (maybe off topic), how does MS-Excel handle rich text in cells, just tested it for curiosity and he handles per character Arabic rich text with no problem also he displays mixed Arabic and English on the same line correctly (not that I need any of those features but understand how he works may help fpSpreadsheet). ِNew finding, after playing with excel a bit I see that the whole grid has its own Bidi Mode and each cell has its own  (see screenshot) and Excel selects the preferred RTL/LTR mode according to the first character or you could force Right-to-Left or Left-to-Right; but still I don't know he does per character rich text even his sibling product MS-Word can't handle that  (screenshot has the text use in Excel) I believe Excel uses Uniscribe. Previously it was (maybe still, and easier to make multi-platform) possible to get the glyphs (using GetCharacterPlacementW) and to draw them one by one (using ExtTextOutW), a quick example: 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 uses JwaWindows; {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);var  s: string;  us: UnicodeString;  flags: DWORD;  GCPRes: GCP_RESULTSW;  l, i, OrgMode: Integer;  pt: POINT;  OrgColor : COLORREF;  DC: LCLType.HDC;begin  DC := Canvas.Handle;   OrgMode := SetBkMode(DC, TRANSPARENT);  //OrgColor := GetTextColor(DC);   s := Edit1.Text;  us := UTF8Decode(s)+#0000;  l := Length(us);  Caption := IntToStr(l);   { Get glyphs and their offsets }  flags := GCP_GLYPHSHAPE or GCP_LIGATE or GCP_REORDER or GCP_USEKERNING;  ZeroMemory(@GCPRes, SizeOf(GCPRes));  GCPRes.lStructSize := SizeOf(GCPRes);  GCPRes.lpDx := GetMem(SizeOf(INT)*l);  GCPRes.lpGlyphs := GetMem(2*l);  GCPRes.nGlyphs:=l;  GetCharacterPlacementW(DC, PWideChar(@us[1]), Length(s), 0, GCPRes, flags);   { Draw glyphs }  pt.x := 20; pt.y := 10;  for i := 0 to GCPRes.nGlyphs-1 do  begin    { Color one glyph at a time }    SetTextColor(DC, Random(255) or (Random(255) shl 8) or (Random(255) shl 16));    ExtTextOutW(DC, pt.x, pt.y, ETO_GLYPH_INDEX or ETO_OPAQUE, nil, @GCPRes.lpGlyphs[i], 1, nil);    pt.x := pt.x + GCPRes.lpDx[i];  end;   //SetTextColor(DC, OrgColor);  SetBkMode(DC, OrgMode);   { Don't forget to free the memory }  FreeMem(GCPRes.lpDx);  FreeMem(GCPRes.lpGlyphs);end;  Used unit JwaWindows because unit Windows has a strange GetCharacterPlacementW Edit: Not sure the result is correct, but it seems so. ---------------------------------------------------------------------- shobits1 · 2016-01-24 23:35 Quote from: engkin on January 25, 2016, 12:04:39 amNot sure the result is correct, but it seems so. The Arabic word is printed correctly; maybe this is what we should used after all; maybe after I complete my project I'll try and implement this. ---------------------------------------------------------------------- wp · 2016-01-25 00:15 Quote from: shobits1 on January 24, 2016, 11:51:21 pmso in general I don't think its TCanvas bug.Good to know Quote from: shobits1 on January 24, 2016, 11:51:21 pmNew finding, after playing with excel a bit I see that the whole grid has its own Bidi Mode and each cell has its own  (see screenshot) and Excel selects the preferred RTL/LTR mode according to the first character or you could force Right-to-Left or Left-to-Right; but still I don't know he does per character rich text even his sibling product MS-Word can't handle that  (screenshot has the text use in Excel)A grid or worksheet BiDi mode should be no problem; I'd have to add a BiDi mode to TsWorksheet or TsWorkbook. Even a cell-based BiDi mode would be possible after adding a flag; TCell has a member "Flags" where it would fit in perfectly. And painting is already done, because the routine we were discussing all the time has a RightToLeft parameter. Per-character rich-text is done in Excel by selecting the characters in the edit line above the grid and picking the color or font from the toolbar or ribbon. In fpspreadsheet you can colorize individual characters by embedding the corresponding html tags (....), or assigning a color to the font used in the Rich-text parameters discussed above. Quote from: shobits1 on January 24, 2016, 11:51:21 pmBTW how do you force cell format: I'd like a cell to have this value 001234 but the grid keep changing it to 1234. also how to set Date format I'd like cell to have 11/12/15 (but grid displays 4/5). any information about forced cell type and masks would be helpful. The format is determined by Excel-type format strings. Assign the string to Grid.NumberFormat[col,row]. In your example with the leading zeros use the format string '000000' - each zero represents a digit; if the number has less digits than 6 (in this example) leading zero are added to complete the six. The number format for your date is 'mm"/"dd"/"yy' - 'mm' is the two-digit month, 'dd' the two digit day, 'yy' the two-digit year; leading zeros are added if the number has only 1 digit. The slash is quoted because otherwise it would be replaced by the system's date separator. In the wiki article (http://wiki.lazarus.freepascal.org/FPSpreadsheet# Number_format_strings), there is a summary of all the symbols. Example: 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 Grid.Cells[1,1] := now;Grid.NumberFormat[1,1] := 'mm"/"dd"/"yy';  // use 'mmmm' to get the month name as a string - it would be interesting to see if you get the strings in Arabian. ---------------------------------------------------------------------- wp · 2016-01-25 23:05 fpspreadsheet now supports a per-cell BiDiMode. Use it like 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 type  TsBiDiMode = (bdDefault, bdLTR, bdRTL);  // declared in fpstypesvar  bdm: TsBiDiMode; { *** WRITING *** }Grid.CellBiDiMode[gridCol, gridRow] := bdRTL;// orGrid.Worksheet.WriteBiDiMode(sheetRow, sheetCol, bdRTL); { *** READING *** }bdm := Grid.CellBiDiMode[gridCol, gridRow];// orcell := Grid.Worksheet.FindCell(sheetRow, sheetCol);bdm := Grid.Worksheet.ReadBiDiMode(cell);