Post Reply 
Custom Text Formatting (Previously: Getting long results in a program)
04-12-2024, 10:48 PM (This post was last modified: 04-12-2024 11:10 PM by komame.)
Post: #65
RE: Getting long results in Program
Hi Tyann,

(04-12-2024 05:21 AM)Tyann Wrote:  I'm attaching the code so that you can check, correct and improve it.
All that's left is to deal with the transparency, I've done a hard test in the code and it works very well.

I've analyzed your code. It looks pretty good, although a few things need refinement. I've made some optimizations to the code both for simplification and performance improvement.

  1. The shortcoming is the too small height of the area copied from the source to the buffer, causing letters for the largest font size (7) with descenders at the bottom, such as "g," to have their last line of pixels cut off.
    To fix this, I changed

    BLIT_P("buffer",10,0,g,x,y,x+l,y+h);
    to
    BLIT_P("buffer",10,0,g,x,y,x+l,y+h+1);

    But this still needs improvement for italic text.
    ---
  2. I moved
    TEXTOUT_P(s,"buffer",10,0,p,c,l);
    inside the REPEAT...UNTIL loop, placing it under the condition i==16 at the beginning of the loop, allowing all formatting cases to be placed into a single common "CASE," which organized the code somewhat.
    ---
  3. For italic text, I simplified the code for shifting left. Instead of

    h:=h-3;n:=CEILING(h/3);j:=10;
    FOR ii FROM 1 TO n DO
    BLIT_P("buffer",j-1,h-d,"buffer",10,h-d,10+l,h);
    j:=j-1;h:=h-3;d:=MIN(h,3);
    END;

    I inserted a shorter and faster version, without unnecessary variables and preliminary calculations:

    j:=9;
    FOR ii:=h-6 DOWNTO -2 STEP 3 DO
    BLIT_P("buffer",j,ii,"buffer",10,ii,10+l,ii+3);
    j:=j-1;
    END;

    This also allowed removing the variables "n", "d" and "hh" from the program.
    ---
  4. Apparently, there is a situation when displaying formatted text in italics on Gx. I replaced:

    h:=hh;j:=10;d:=3;
    n:=CEILING(h/3);y:=y+h;
    FOR ii FROM 1 TO n DO
    BLIT_P(g,x,y-d,"buffer",j,h-d,j+l,h);
    j:=j-1;y:=y-3;h:=h-3;d:=MIN(h,3);
    END;

    with a faster and simpler version:

    h:=hh;j:=10;
    FOR ii:=h-3 DOWNTO -2 STEP 3 DO
    BLIT_P(g,x,y+ii,"buffer",j,ii,j+l,ii+3);
    j:=j-1;
    END;

    ---
  5. The code doesn't handle displaying letters with accents at the top (such as "Ó" or "É") in the case of very small fonts. The problem arises from the inconsistency of TEXTOUT_P because for larger fonts, the point (x, y) is applied to the upper part of the characters (taking accents into account). However, for small fonts, the point (x, y) is applied to the upper part of uppercase letters (below accents). This causes accents to be positioned higher and invisible if you insert the text at point (x,0). To solve this problem, you need to insert text into the buffer 3 pixels lower and adjust all parts of the program that add formatting accordingly. This is quite a significant change and may also mean enlarging the buffer by a few pixels because applying one common rule to all font sizes will also affect the largest fonts, which after being shifted 3 pixels lower may no longer fit in the buffer.
    ---
  6. Among smaller adjustments, I also adjusted the position of the strikethrough and underline because they were incorrectly positioned for small fonts. For example, the underline overlapped the lower part of letters in very small fonts, making the text unreadable

(04-12-2024 05:21 AM)Tyann Wrote:  BLIT_P takes this argument last and therefore forces us to send a colour for pixels that are not visible, so we need to set a default value that is different from the 2 colours that we can supply to ATEXTOUT_P (I was thinking of a local list of 3 colours: one of which must be different or perhaps a calculation).

Regarding BLIT_P and the requirement to define the [c] parameter when using transparency, I mentioned this issue in post #59 and in my opinion, this bug cannot be bypassed. The last and penultimate parameters ([a] and [alpha]) should have their order reversed. Otherwise, there is always a risk that one of the colors will not be copied (because one of them may always appear in the source image). So what you proposed may not work because when displaying text, the edges of the letters are smoothed (antialiasing is applied), creating several intermediate colors. Therefore, even if you know the text color and the background color, you never know which color to choose to avoid hitting one of those that will result from the overlap of the text edges with the background, especially when the text is embedded on a texture (where quite a few different colors may occur). Of course, if a background color (solid background) is provided, then the chances of the text being correctly copied are quite high.

(04-12-2024 05:21 AM)Tyann Wrote:  For the arguments, I've stuck with a numerical value, but we can add the option of setting a string (I was then thinking of converting the string into a numerical equivalent so as to have just one type of test).

From my point of view, there's no point in introducing two alternative types of arguments regarding text formatting. One is completely sufficient.

Below is the complete code with my corrections (after pasting the code, two spaces (in different places) may appear in the definition of ICON - this is an error related to copying from hpmuseum. Remove these two spaces, and then the program will compile):
Code:
ICON buffer 89504E470D0A1A0A0000000D494844520000014A0000001C01000000002FF2CBF6000000027​4524E5300007693CD3800000018494441547801EDC10109000000C3A0F54FFD1C073580230304B40​001D69772FE0000000049454E44AE426082;
EXPORT ATEXTSIZE(s,p,a)
BEGIN
 LOCAL h,l,f;
 {'l','h'}:=TEXTSIZE(s,p);
 IF BITAND(a,8) THEN
  l:=l+CEILING(h/3);
 END;
 IF BITAND(a,16) THEN
  l:=l+1;f:=1;
 END;
 IF BITAND(a,32) THEN
  l:=l+1-f;h:=h+1;
 END;
 {l,h}; 
END;

EXPORT ATEXTOUT_P(...l)
BEGIN
 LOCAL d:=SIZE(l),t:=TYPE(l(2)),i,f;
 CASE
  IF 2<d<6 OR (d==6 AND t==8) THEN
   f:=STRING(l);
   f:="TEXTOUT_P("+SUPPRESS(f,{1,DIM(f)});
   EXPR(f);
  END;
  IF 5<d<9 THEN  
   IF t≠8 THEN 
    l:=INSERT(l,2,G0);
    d:=d+1;
   END;
   IF d<8 THEN
    l(0):=GET(ATEXTSIZE(l(1),l(5),l(7)),1);
   END;
   atextout(l); 
  END;
  IF d==9 AND t==8 then
   atextout(l);  
  END;
  DEFAULT
    Err(0);
 END;
END;

atextout(par)
BEGIN
 LOCAL s,g,x,y,p,c,a,l;
 LOCAL f,i,j:=10,k,fi,fr,ii;
 {'s','g','x','y','p','c','a','l'}:=par({1,8});
 LOCAL h;
 IF 63<a OR a<0 THEN
  Err(2);
 END;
 {'k','h'}:=ATEXTSIZE(s,p,a);
 l:=MIN(l,k);
 IF SIZE(par)==9 THEN
  f:=par(9);fr:=1;
 END;
 IF fr THEN
  RECT_P("buffer",f);
 ELSE
  BLIT_P("buffer",10,0,g,x,y,x+l,y+h+3);
 END;
 i:=1;
 REPEAT 
  IF i==16 THEN
    TEXTOUT_P(s,"buffer",10,0,p,c,l); 
  END;
  IF BITAND(a,i) THEN
   CASE
    IF i==1 THEN //barré
     j:=IP(h-p-5);
     LINE_P("buffer",10,j,10+l,j,c);
    end;
    IF i==2 THEN //sous ligné
      LINE_P("buffer",10,h-2,10+l,h-2,c);  
    END;
    IF i==4 THEN //encadré
     RECT_P("buffer",10,0,10+l-1,h-1,c,{0,255}); 
    END; 
    IF i==8 THEN //italique 
     j:=9;
     FOR ii:=h-6 DOWNTO -2 STEP 3 DO
      BLIT_P("buffer",j,ii,"buffer",10,ii,10+l,ii+3);
      j:=j-1;
     END;
     fi:=1;
    END;
    IF i==16 THEN //gras
      TEXTOUT_P(s,"buffer",11,0,p,c,l);
    END;
    IF i==32 THEN //relief
      TEXTOUT_P(s,"buffer",11,1,p,c,l);
    END;
   END;
  END;
  i:=i*2;
 UNTIL i==64;
 IF fi THEN
  j:=10;
  FOR ii:=h-3 DOWNTO -2 STEP 3 DO
    BLIT_P(g,x,y+ii,"buffer",j,ii,j+l,ii+3);
    j:=j-1;
  END;
 ELSE
  BLIT_P(g,x,y,"buffer",10,0,l+10,h+1);
 END;
END;

Err(n)
BEGIN
 LOCAL l:={};
 CASE
  if n==0 then EXPR(":"); end;
  if n==1 then UPPER(1); end;
  if n==2 then l(1); end;
  if n==3 then l+{1,2}; end;
  if n==4 then CROSS([1],[1,1]);end;
 END;
END;

I had something else to write, but now I don't remember what... Oh well, maybe I'll remember tomorrow.
Good night.

Piotr Kowalewski
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Getting long results in Program - komame - 04-12-2024 10:48 PM
RE: Custom Text Formatting - Tyann - 04-15-2024, 06:55 PM
RE: Custom Text Formatting - komame - 04-15-2024, 07:42 PM
RE: Custom Text Formatting - Tyann - 04-16-2024, 04:38 AM
RE: Custom Text Formatting - komame - 04-16-2024, 06:02 AM



User(s) browsing this thread: 1 Guest(s)