Ignore:
Timestamp:
Feb 25, 2019, 8:34:42 PM (6 years ago)
Author:
ataylor
Message:

Experimental new logic to try and fix DBCS text wrapping.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Components/CanvasFontManager.pas

    r403 r418  
    5050    lMaxCharInc: LONG;
    5151    lMaxDescender: LONG;
     52    lEmInc: LONG;
    5253
    5354    destructor Destroy; override;
     
    103104    function AverageCharWidth: longint;
    104105    function MaximumCharWidth: longint;
    105 
     106    function CJKCharWidth: longint;         // ALT
     107    function CJKTextWidth(  const Length: longint; const S: PChar ): longint;
    106108    function IsFixed: boolean;
    107109
     
    391393      Font.lAveCharWidth := pfm^[ T ].lAveCharWidth;
    392394      Font.lMaxCharInc := pfm^[ T ].lMaxCharInc;
     395      Font.lEmInc := pfm^[ T ].lEmInc;
    393396
    394397      Font.ID := -1; // and always shall be so...
     
    825828  Result.lAveCharWidth := FontInfo.lAveCharWidth;
    826829  Result.lMaxCharInc := FontInfo.lMaxCharInc;
     830  Result.lEmInc := FontInfo.lEmInc;
    827831
    828832  // Set style flags
     
    10601064  FCurrentFont.lMaxCharInc := fm.lMaxCharInc;
    10611065  FCurrentFont.lMaxDescender := fm.lMaxDescender;
     1066  FCurrentFont.lEmInc := fm.lEmInc;
    10621067end;
    10631068
     
    11051110  Result := FCurrentFont.FixedWidth;
    11061111end;
     1112
     1113// ALT begins
     1114//
     1115// A 'default' average width for DBCS characters; probably not very accurate.
     1116// Should only be used as a fallback in case querying the actual string width
     1117// is impossible/fails.
     1118//
     1119function TCanvasFontManager.CJKCharWidth: longint;
     1120begin
     1121  EnsureMetricsLoaded;
     1122  if FCurrentFont.lMaxCharInc < FCurrentFont.lEmInc then
     1123    Result := FCurrentFont.lMaxCharInc * FontWidthPrecisionFactor
     1124  else
     1125    Result := FCurrentFont.lEmInc * FontWidthPrecisionFactor;
     1126end;
     1127
     1128// Get the render width of a CJK (Chinese/Japanese/Korean) character string.
     1129//
     1130function TCanvasFontManager.CJKTextWidth(  const Length: longint; const S: PChar ): longint;
     1131var
     1132  aptl: Array[ 0..TXTBOX_COUNT-1 ] Of PointL;
     1133begin
     1134  EnsureMetricsLoaded;
     1135  GpiQueryTextBox( FCanvas.Handle, Length, S^, TXTBOX_COUNT, aptl[0] );
     1136  // The pad of 2.1 per character was chosen through experimentation, since the
     1137  // actual returned value always seems too narrow. I have no idea why. (ALT)
     1138  Result := ( 2.1 + aptl[ TXTBOX_BOTTOMRIGHT ].x) * FontWidthPrecisionFactor;
     1139end;
     1140//
     1141// ALT ends
    11071142
    11081143procedure TCanvasFontManager.DrawString( Var Point: TPoint;
Note: See TracChangeset for help on using the changeset viewer.