WYSIWIG与Unicode

Mit*_*tch 2 windows delphi unicode wysiwyg

我在Delphi中编写了一个Windows程序,它使用GetCharWidth和Em-Square非常精确地将文本放置到屏幕和打印机上.这适用于ANSI文本,您只需要检索和计算255个字符的宽度,但是当您使用65535个字符转到Unicode时,它太慢了.由于必须创建2个宽度数组,一个用于普通数组,一个用于粗体数,因此问题变得更严重.

 //Setup a reference canvas for measuring purposes
  RefDC := CreateCompatibleDC ( FCanvas.Handle ) ;
  DPI := GetDeviceCaps ( RefDC , LOGPIXELSY ) ;

  //find EmSquare
  GetOutlineTextMetrics ( RefDC , sizeof(otm) , @otm[0] ) ;
  EmSq := otm[0].otmEmSquare ;

  //calc NORMAL char sizes
  GetObject ( FCanvas.Font.Handle , SizeOf ( lf ) , @lf ) ;

  lf.lfHeight := -EmSq ;
  lf.lfWidth  := 0 ;
  lf.lfWeight   := FW_NORMAL ;

  hf := CreateFontIndirect ( lf ) ;
  hold := SelectObject ( RefDC , hf ) ;

  GetCharWidth ( RefDC , 0 , $FFFF , nCharWidth ) ;
  for a := 0 to $FFFF do
    fCharWidth[a] := nCharWidth[a]* PixelSize / EmSq ;

  SelectObject ( RefDC , hold ) ;
  DeleteObject ( hf ) ;

  //calculate line height
  PixelSize := abs ( fCanvas.Font.Size * DPI / 72 ) ;
  GetOutlineTextMetrics ( RefDC , sizeof(otm) , @otm[0] ) ;
  LineHt := round ( ( otm[0].otmTextMetrics.tmHeight +
                      otm[0].otmTextMetrics.tmExternalLeading ) *
                      PixelSize / EmSq ) ;

  //calculate Bold char sizes
  lf.lfWeight := FW_BOLD ;
  hf := CreateFontIndirect ( lf ) ;
  hold := SelectObject ( RefDC , hf ) ;

  GetCharWidth ( RefDC , 0 , $FFFF , nCharWidth ) ;
  for a := 0 to $FFFF do
    fBoldWidth[a] := nCharWidth[a] * PixelSize / EmSq ;

  SelectObject ( RefDC , hold ) ;
  DeleteObject ( hf ) ;

  DeleteDC ( RefDC ) ;`
Run Code Online (Sandbox Code Playgroud)

lke*_*ler 8

计算单个字符宽度并以Unicode格式添加它们不仅非常慢,而且它是错误的并且无法正常工作.Unicode将字符标记组合在一起,有时以复杂的方式.

使用Unicode,正确的方法是将整个字符串传递给Windows API函数GetTextExtentExPoint以及行的宽度,它将为您计算出适合该行的字符数.

这里有一个例子.