🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.readers
Article #234135

--

#234135
From: _
Date: Thu, 02 Jul 2009 16:20
363 lines
16394 bytes
My grapheme-caching system makes any “script”(alphabet) monoSpaced.
screenShots:
  www.JeffRelf.F-M.FM/Bidi.PNG
  www.JeffRelf.F-M.FM/UTF-8.PNG
  
>From “ www.JeffRelf.F-M.FM/X.CPP ”:

  if ( WM == WM_NCACTIVATE ) {  OnStage = C, PaintReq = 1 ;
    ShowWindow( Win, OnStage ? SW_SHOW : SW_SHOWMINNOACTIVE );
    if ( !OnStage ) {
      //  The (maximized)window lost it's focus, minimize it.
      PaintReq = 0 ;  goto Done;  }
    ShowWindow( Win, SW_RESTORE ); goto reInit ;  }
          ――――――――――――――
  reInit:
  //  Window is being reActived, check if settings(in X.TXT) changed.
  gSettings();  
  RECT  cTB ; 
  if ( GetWindowRect( TaskBar, & cTB ), !memcmp( &cTB, &TB, SzTB ) )
    //  The size of the taskBar hasn't changed and
    //  the screen hasn't been reSized ― no need to reCache glyphs.
    goto Paint ;
  memmove( & TB, &cTB, SzTB ); 
  WinLeft = ScrLeft, WinRight = ScrRight, 
  WinBot = ScrBot, WinTop = ScrTop ;
  int  Left, Top, TBW = TB.right - TB.left
  , TBH = TB.bottom - TB.top
  //  Is the TaskBar tall or wide ?
  , Tall = TBH > TBW ;
  
  if ( Tall ) {
    //  The taskBar is on the left or right of our maximized window.
    if ( Left = TB.right < ScrW / 2 )  WinLeft = TB.right - 1 ;
    else  WinRight = TB.left + 1 ;  }
  //  The Taskbar is at the top or bottom of our maximized window. 
  else
     if ( Top = TB.bottom < ScrH / 2 ) 
       WinTop = TB.bottom - 1 ;  else  WinBot = TB.top + 1 ;
  WinW = WinRight - WinLeft, WinH = WinBot - WinTop ;
  WinRecG = _R( WinLeft, WinTop, WinW, WinH ), 
  WinRecL = _R( 0, 0, WinW, WinH );
  SetWindowPos( Win, HWND_TOP, WinLeft, WinTop, WinW, WinH, 0 );

  //  The nonInteger hight of a monoSpaced ASCII glyph is a function of
  //  the screen width and now many columns I want.
  iCharW = ceil( CharW = WinW / ( ColsWin = 123 ) );
  //  The nonInteger hight of an ASCII glyph is a function of its width
  //  and DejaVu's standard hight/width ratio: 366 / 189.
  //
  //  All graphemes, including Indic ligatures, are integer multiples
  //  of CharW and CharH... i.e. all graphemes are monoSpaced.
  iCharH = ceil( CharH = 366 * CharW / 189 ) ; //  21 / 11; 1.936
  RowsWin = ceil( WinH / CharH ), 
  Lines_Per_Pg = RowsWin - 1 - ToolBarLines ;
  //  Set the margins for smallCaps chars.
  ▼X = ( ( 1 - x∕X ) * CharW ) / 2, 
  ▼Y = ceil( ( 1 - x∕X ) * CharH ), wMultiCache = WinW ;

  PaintReq = 1, DeleteObject( caps ), caps = 0 ;  
  gFont( DejaVu•1, L"DejaVu Sans Mono", CharH );
  if ( !caps )  gFont( Consolas, L"Consolas", CharH );
  if ( !caps )  gFont( Courier, L"Courier New", CharH );
  gFont( Arial, L"Arial Unicode MS", CharH );
  gFont( fnMusical, L"Musical Symbols", 2 * CharH );

  CharW•3 = CharW * 3, CharH•3 = CharH * 3 ; 
  gFont( DejaVu•3, L"DejaVu Sans Mono", CharH•3 );
  gFont( fnIcons, L"MS Mincho", 2.33 * CharH );
  //  gFont( fnChess, L"MS Mincho", 5 * CharH );  

  //  Clear the dynamic array of graphemes
  { LoopXx( Gy, Gy )  free( P ), * PP = 0 ;  }
  
  //  Clear the dynamic array of grapheme caches(DirectDraw surfaces).
  { LoopXx( Ca, Ca )  Trash( P ), * PP = 0 ;  }
  aCache = 0, free( Ca.BB ), Zero( Ca ), free( Gy.BB ), 
  Zero( Gy ), Trash( DrawSur ), Trash( Cursor );
  
  
  { Loop( Highest_Ascii - Lowest_Ascii + 1 ) {
      //  preAllocate an indexable-list of ASCII chars.
      ✚✚( Gy ) = ( GyP ) Malloc ( sizeof( GlyT ) );  } }
  Cur_Pix = CharW * 2, CursorRec = _R( 0, 0, Cur_Pix, Cur_Pix );
  UseKey = 0, NewSur( DrawSur, WinW, WinH ), 
  NewSur( Cursor, Cur_Pix, Cur_Pix ), ✚✚Cache ;  
  { Graphics  Plus( DC );
    Plus.SetSmoothingMode( SmoothingModeAntiAlias );
    Pen  Ink( Color( 255, 255, 255, 255 ), 2 );
    //  Draw the cursor using smoothed lines.
    Plus.DrawLine( & Ink, 0, 0, Cur_Pix / 2, Cur_Pix );
    Plus.DrawLine( & Ink, 0, 0, Cur_Pix, Cur_Pix / 2 );  }
  
  Chk( Cursor->ReleaseDC( DC ) );  goto Done ;
      ―――――――――――――――――
//  Below is PrnChar().      
//  PrnChar() returns and/or prints/caches graphemes.

  HFONT  fnIcons, fnChess, DejaVu•3, caps, DejaVu•1
  //  Arial Unicode is my lower-quality, catch-all font.
  , Consolas, Courier, Arial, fnMusical ;
  float CharW, CharH, CharW•3, CharH•3 ;
  inline int Round( float X ) {  return floor( X + .5 );  }

  const struct {
    ulong 
      NormColor, NumColor, BarColor
    , Sel_Color,     Grey, NewColor
    , OddColor,    Yellow, Red ;  } Hues = {
        // This “struct” of colors doubles as a static array.
      RGB( 144, 111, 111 ), RGB(   0, 144, 144 ), RGB( 155, 111,   0 )
    , RGB(  99, 222,  99 ), RGB( 155, 155, 155 ), RGB( 133, 188, 133 )
    , RGB( 177,  122,  0 ), RGB( 255,  255,  0 ), RGB( 255,   0,   0 )
  };

  const int Colors = sizeof Hues / sizeof ulong ;

  #define  Is_Color (  \
       Ch == ChNorm || Ch == ChNormNoNum || Ch == ChNew  \
    || Ch == ChOdd || Ch == ChYell || Ch == ChRed  \
    || Ch == ChGrey || Ch == ChImg )

  const  wchar_t
    ChNorm = 0xD801, ChNormNoNum = 0xD802, ChNew = 0xD803
  , ChOdd = 0xD804, ChYell = 0xD805
  , ChRed = 0xD806, ChGrey = 0xD807, ChImg = 0xD808 ;
      
  struct CharSurT { int X ;  _SurT  Cache ;  };

    const int mxCH = 4 ;
  struct GlyT {  HFONT Font ;
    int  W, H, Blank, Rows, Cols, Known, szGraph ; wchar_t  CH[ mxCH ];
    CharSurT  CharSur[ Colors ];  };  
  typedef  GlyT  * GyP ;  
  typedef  GyP  * GyA ;  struct  GyT {  GyA  BB, PP, EE ; };

  typedef  _SurT  CaP ;  typedef  _SurT  * CaA ;  
  struct  CaT {  CaA  BB, PP, EE ; };

  GyT  Gy ;  CaT  Ca ;
  GyP & ✚✚( GyT & Gy ) {  return ✚✚< GyT, GyA, GyP> ( Gy, -1 ) ;  }
  CaP & ✚✚( CaT & Ca ) {  return ✚✚< CaT, CaA, CaP> ( Ca, -1 ) ;  }

  #define  Try( F )  \
    if ( F ) {  \
      SetPtr( DC, F ),  \
      GetGlyphIndices( DC, CH, 1, Good, GGI_MARK_NONEXISTING_GLYPHS); \
      if ( short( *Good ) != -1 ) {  \
        /* The font has the glyph */  \
        G.Font = F ;  goto  conFont ;  } }
      
  wchar_t  ChIconB = L'☀', ChIconE = L'✧' ;
  float iCharW, iCharH ; int xCache, wMultiCache ; float ▼X, ▼Y ; 
  const int iHueSel = pInt( & Hues.Sel_Color ) - pInt( & Hues );

  //  ✚✚Cache() returns a large array of normal-hight glyphs,
  //  so fewer (limited)DirectDraw surfaces have to be used.
  #define ✚✚Cache  (  \
    xCache = 0,  \
    NewSur( ✚✚( Ca ), wMultiCache, iCharH *1.1 ), aCache = * Ca.PP )
  
  int Col, Row ;   const int  Lowest_Ascii = 32, Highest_Ascii = 126 ;
  
//  Return and/or print/cache a grapheme,
//  including complex, multi-glyph ligatures.
GlyT  &PrnChar( LnP &CH, const ulong *Hue ) {
  GyP  P ;  wchar_t  Ch = *CH, Ch2 ;  
  int  unEq = 0, surrogatePair = ( Ch & 0xD800 ) == 0xD800 ;
  int szGraph = 1 + surrogatePair ;
  if ( Ch >= Lowest_Ascii && Ch <= Highest_Ascii ) {
    //  ASCII chars are an index into a “static” array.
    P = Gy.BB[ Ch - Lowest_Ascii ];  goto conP ;  }
  
  //  I haven't found an acceptable way to discover
  //  which graphemes are complex, multi-glyph ligatures;
  //  so I just hard-coded some here, as a proof of concept.
  static wchar_t  Ligs[] = L"்ி்ேூཱུྃྨེིོཾि्े"; 
  static LnP eLigs = Ligs + sizeof( Ligs ) / szChr ;
  if ( !surrogatePair )
    LOOP{  Ch2 = CH[ szGraph ];  
      if ( Ch2 <= Highest_Ascii )
        //  An ASCII char can't be a sub-ligature,
        //  no need to check.
        break ;
      LnP P = Ligs - 1 ;  
      while( ++ P < eLigs && Ch2 != *P );
      if ( P < eLigs )
         //  A complex grapheme(ligature) might contain 4 glyphs.
         //  The size must be known, othewise
         //  the binary search(below) wouldn't work.
         szGraph++ ; else break ;  }

  GyA  BB = Gy.BB + ( Highest_Ascii - Lowest_Ascii + 1 )
  , EE = Gy.PP + 1, PP = BB ;
  LOOP {  PP = BB + ( EE - BB ) / 2 ;  
    if ( PP == Gy.PP + 1 ) {  unEq = 0 ; break ; } 
    P = * PP ;  int unEq1 = szGraph - P->szGraph ;  
    unEq = unEq1 ? unEq1 : wmemcmp( CH, P->CH, P->szGraph );
    if ( ! unEq )
      //  Binary Search found the grapeme's “struct”.
      goto conP ;
    if ( unEq > 0 ) {
      if ( PP >= EE )
        //  The grapheme is not on record,
        //  insert it into the sorted/dynamic list.
        break ;
      BB = PP + 1 ; }
    else {  if ( PP <= BB )  break ;  EE = PP - 1 ; } }
  int  _PP =  PP - Gy.BB ;  ✚✚( Gy );
  EE = Gy.PP + 1, PP = Gy.BB + _PP ;  int  After = EE - PP - 1 ;
  if ( unEq > 0 )  PP ++, -- After ;
  if ( After > 0 )
    //  Make room to insert into the sorted list.
    memmove( PP + 1, PP, After * szPtr );
  * PP = P = ( GyP ) Malloc ( sizeof *P );
  
  conP:
  //  Now, we have Grapheme “struct”.
  GlyT  &G = *P ;
  if ( G.Blank || G.Known && !Hue )
    //  The details of the grapheme are known;
    //  no Hue was specified, so return(instead of drawing).
    return  G ;
  int iHue = !Hue ? 0 : Hue - ( ulong * ) & Hues ;
  CharSurT  &C = G.CharSur[ iHue ];
  if ( Hue && C.Cache )
    //  The grapheme's hue is cached, just print it.
    goto conHue ;

  int  Icon = Ch >= ChIconB && Ch <= ChIconE, Zero = Ch == '0'
  , capsCh = Ch >= 'a' && Ch <= 'z';
  HDC  DC ;  ushort  Good[ 3 ];   Chk( aCache->GetDC( & DC ) );  
  if ( G.Font ) {  SetPtr( DC, G.Font ); goto  conFont ;  }
  if ( surrogatePair && fnMusical ) {
    //  The Musical font is borked
    //  ( or surrogate pairs aren't supported), “Try()” doesn't work.
    SetPtr( DC, G.Font = fnMusical );  goto  conFont ;  }
  if ( Icon ) {  Try( fnIcons )  Try( DejaVu•3 )  }
  if ( capsCh && caps ) {  //  The smallCaps font, ASCII.
    SetPtr( DC, G.Font = caps ); goto  conFont ;  }
  Try( DejaVu•1 ) Try( Consolas ) Try( Courier ) Try( Arial )
  G.Known = G.Blank = 1 ;  Chk( aCache->ReleaseDC( DC ) );
  //  No font for this glyph, dump it.
  return G ;

  conFont:
  //  Now, we have a font.
  if ( !G.Known ) {
    //  Get the (monoSpaced)dimensions of the grapheme.
    SIZE  xy ; wmemmove( G.CH, CH, G.szGraph = szGraph );
    GetTextExtentPoint32( DC, CH, G.szGraph, &xy );
      
    G.W = xy.cx * 1.1, G.H = xy.cy * 1.1 ;
    if ( G.W && G.W < CharW )  G.W = CharW ;
    G.Cols = surrogatePair 
    ? 2 : Round( G.W / CharW ), G.Rows = Round( G.H / CharH );
    
    //  The monoSpacing isn't exact, allow some tollerance.
    //  G.W is the width of the grapheme's cache, G.H the hight.
    G.W = G.Cols * CharW * 1.1, G.H = G.Rows * CharH * 1.1 ;
    G.Known = 1, G.Blank = !G.Cols ;  
    
    if ( !Hue || G.Blank ) {
      //  Now, the details of the grapheme are known;
      //  no Hue was specified, so return(instead of drawing).
      Chk( aCache->ReleaseDC( DC ) );  return  G ;  } }
  
  if ( G.Cols > 1 )
    Chk( aCache->ReleaseDC( DC ) ), C.X = 0, 
    //  Allocate a DirectDraw surface to cache a tall grapheme.
    NewSur( C.Cache, G.W, G.H ), Chk( C.Cache->GetDC( & DC ) );
  else {
    if ( xCache + G.W >= wMultiCache )
      //  Find room in the array of normal-hight graphemes.
      Chk( aCache->ReleaseDC( DC ) ), 
      ✚✚Cache, Chk( aCache->GetDC( & DC ) );
    C.X = xCache, C.Cache = aCache, xCache += G.W ;  }

  SetPtr( DC, G.Font ), 
  SetBkColor( DC, iHue != iHueSel ? 0 : RGB( 77, 0, 77 ) ), 
  SetTextColor( DC, (( ulong *) &Hues)[ iHue ] );
  { int ▼ = Zero || capsCh ;  RECT  Fill = _R( C.X, 0, G.W, G.H );
    ExtTextOut( DC, C.X + ▼ * ▼X, ▼ * ▼Y
    , ETO_CLIPPED | ETO_OPAQUE, &Fill
    , G.szGraph > 1  //  make Zero a tiny circle.
      ? G.CH : ( Ch = Zero ? L'°' : !capsCh ? Ch : Ch - 32, &Ch ), 
    G.szGraph, 0 );  }
  Chk( C.Cache->ReleaseDC( DC ) );
  
  conHue:
  //  Now, the cache is known, paint it.
  Fast_Paint(  //  Paint the grapheme onto the back-buffer.
    C.Cache, _R( C.X, 0, G.W, G.H )
  , _R( Round( Col * CharW ), Round( Row * CharH ), G.W, G.H ) );
  Col += G.Cols ;  
  if ( G.szGraph > 1 )
    //  advance the pointer(string) more than usual.
    CH += G.szGraph - 1 ;   return  *P ;  }

    struct { int ii, i ; } sAnk, sB, sE ;
    int  C, C2 ;  
    ushort * P_C = ( ushort * ) & C, * P_C2 = ( ushort * ) & C2 ;
  //  Returns the index to a string, from a click on the screeen.
  int Col_to_Index( int ii ) {  int Ch, Ch2 ;  
    if ( Ln.BB + ii > Ln.PP || !Ln.BB[ ii ] )
      return 0 ;
    LnP B = Ln.BB[ ii ], P = B, E = P + int( *P_C2 / CharW );
    While( Ch ) {
      //  Traversing the line from left to right.
      if ( Is_Color )
        //  A formatting character(for a hue) increases line length.
        E ++ ;
      else {  GlyT &G = PrnChar( P, 0 );
        if ( G.Cols > 1 )
          //  A double, tripple, or quadruple-width grapheme means
          //  the string has  Fewer  chars than originally thought.
          E -= G.Cols - 1 ;
        if ( G.szGraph > 1 ) {  int Sk = G.szGraph - 1 ;
          //  A double, tripple, or quadruple-glyph ligature means
          //  the string has  More  chars than originally thought.
          E += Sk ; 
          if ( P + Sk >= E )
            //  Return the start of the complex grapheme.
            return  P - B ; P += Sk ;  } }
      if ( P >= E )  break ;  }
    return  P - B ;  }

    int szToClip ;
  //  Size the (multi-line)selected text.
  //  If “D” is nonZero, put the selected text into it.
  SzClip( LnP D ) {
    szToClip = 9 ; int Szing = ! D -- ;  LnA  PP = Ln.BB + sB.ii - 1 ;
    int Ch, Ch2, virEE = Ln.BB + sE.ii > Ln.PP ;
    //  virEE is true when selecting past the end of
    //  the (dynamic)array of lines, to get the entire line(above it).
    Loop( 1 + sE.ii - sB.ii - virEE ) {
      int Last = J == LLL - 1 ;  LnP  oD = D, B = * ++ PP, P = B, E ;
      if ( !B ) continue ;  E = B + strSz( B );
      if ( ! J )  P = B + sB.i ;  
      if ( !virEE && Last ) {  E = B + sE.i ;
         if ( *E ) {  GlyT &G = PrnChar( E, 0 ); E += G.szGraph ;  } }
      if ( Szing ) {
        szToClip += E - B + 2 ;
        if ( Last ) {
          szToClip *= szChr ; return ;  }  continue ;  }
      While( Ch && P < E )  if ( ! Is_Color )  * ++ D = Ch ;
      if ( !virEE && Last ) {
        //  When selecting multiple lines,
        //  selecting just one column on the last line
        //  means you want the entire line(above it), not that column.
        D[ !( LLL > 1 && D - oD == 1 ) ] = 0 ;  return ;  }
      * ++ D = 13, * ++ D = 10 ;  if ( virEE && Last )  * ++ D = 0 ;  } }

    const float  x∕X = .8 ;
  gFont( HFONT &F, LnP  N, float H ) {  FontFamily Nym( N ); 
    if ( ! Nym.IsAvailable() )
      //  The font doesn't exist.
      return ;
    DeleteObject( F ), 
    F = CreateFont( ceil( H ), 0,0,0,0,0,0,0,0,0,0,0, FIXED_PITCH, N );
    if ( !caps )
      //  The smallCaps font.
      caps = CreateFont(
        x∕X * H, 0,0,0,0,0,0,0,0,0,0,0, FIXED_PITCH, N );  }
Technical Details
Message-ID: <_@Jeff_Relf.Seattle.2009_Jul2.9.19am.7n>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.readers.20140922.mbox.gz!border1.nntp.ams.giganews.com!nntp.giganews.com!newsfeed.freenet.de!newsfeed.kpn.net!pfeed08.wxs.nl!news.glorb.com!news-spur2.glorb.com!homer.glorb.com!not-for-mail
References: <slrnh3ofr4.kn7.pjr@pjr.gotdns.org> <Xns9C2FDE91E1A12duckrulestheuniverse@nomail.afraid.org> <slrnh4ff0o.7va.pjr@pjr.gotdns.org> <_@Jeff_Relf.Seattle.2009_Jun29.0.23am.CA> <1tenawf6ve0oh.fe4vxxhwx3y0.dlg@40tude.net> <49sj451bssmbv70q0un17p2hfj3snr738q@4ax.com> <_@Jeff_Relf.Seattle.2009_Jul1.0.59pm.Py>

Show Full Headers
Complete Article Headers
Message-ID: <_@Jeff_Relf.Seattle.2009_Jul2.9.19am.7n>
Subject: --
Date: Thu, 02 Jul 2009 16:20:08 +0000
From: _@Jeff_Relf.Seattle.inValid
References: <slrnh3ofr4.kn7.pjr@pjr.gotdns.org> <Xns9C2FDE91E1A12duckrulestheuniverse@nomail.afraid.org> <slrnh4ff0o.7va.pjr@pjr.gotdns.org> <_@Jeff_Relf.Seattle.2009_Jun29.0.23am.CA> <1tenawf6ve0oh.fe4vxxhwx3y0.dlg@40tude.net> <49sj451bssmbv70q0un17p2hfj3snr738q@4ax.com> <_@Jeff_Relf.Seattle.2009_Jul1.0.59pm.Py>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.readers.20140922.mbox.gz!border1.nntp.ams.giganews.com!nntp.giganews.com!newsfeed.freenet.de!newsfeed.kpn.net!pfeed08.wxs.nl!news.glorb.com!news-spur2.glorb.com!homer.glorb.com!not-for-mail
Newsgroups: news.software.readers
Organization: Glorb Internet Services, http://www.glorb.com
Lines: 363
NNTP-Posting-Host: kmuqT6ZLoK/LNzYeScqfsg.user.posting2.glorb.com
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Complaints-To: abuse@glorb.com
NNTP-Posting-Date: Thu, 2 Jul 2009 16:20:08 +0000 (UTC)
X-Notice: Scanned by Mr. Bill
User-Agent: JeffRelf.F-M.FM/X.ZIP