簡體   English   中英

FreeType 是如何計算提前的?

[英]How is FreeType calculating advance?

我試圖弄清楚 FreeType 庫是如何計算FT_GlyphSlotRec_advance變量的。 根據該結構上方的注釋塊,該變量與此處進一步解釋的horiAdvance變量相同,后者包含在FT_Glyph_Metrics_結構中。

無論如何,以典型的 Arial 窄字體為例。 它可以在 C:\Windows\Fonts\ARIALN.TTF 中找到。 將其上傳到任何基於 web 的 TTF 文件編輯器(選項 1選項 2 ),或使用您自己的可以解釋 TTF 文件的程序(fontforge 等),您可以看到“。”的高級。 字符被明確定義為467。

我正在運行以下代碼,當我讀取FT_GlyphSlotRec_advance.x值時,我得到的值為 1472。顯然這與 467 不同。我是否誤解了它的含義? 還是我錯誤地使用了 FreeType 庫?

我正在重新輸入我用來從沒有互聯網的計算機上獲取 1472 的代碼,所以請原諒我任何平凡的語法錯誤。

#include <string>
#include <ft2build.h>
#include FT_FREETYPE_H

using namespace std;

int main() {
  FT_Uint font_height = 100;
  FT_Library ft;
  FT_Face face;
  char* filepathname; // Defined through code that was excluded for the sake of simplifying this example

  FT_Init_FreeType(&ft);
  FT_New_Face(ft, filepathname, 0, &face);
  FT_Set_Pixel_Sizes(face, 0, font_height);
  
  FT_Uint glyph_idx = FT_Get_Char_Index(face, 33);
  FT_Load_Glyph(face, glyph_index, FT_LOAD_RENDER);

  FT_Pos advance_x = face->glyph->advance.x;

  return 0;
}

main()結束時中斷會導致 advance_x 為 1472。

如您在問題中鏈接的教程第 2 步頁面所示,這些指標通常以 26.6 像素格式報告。 您正在通過加載字體的方式對其進行修改。 您可以使用FT_LOAD_NO_SCALE來“[不] 縮放加載的輪廓字形,但將其保留在字體單位中。”

您的代碼會在默認屏幕分辨率為 72 dpi 時以您請求的大小生成字形的 bitmap 版本。 您得到的 1472 是用於在 bitmap 中繪制字符的像素數 * 64。 因此,在這種情況下,1472 / 64 = 23 像素,如果您將多個字符繪制到同一個 bitmap 中,這將是您將繪制光標/筆點前進的距離。

467 是 1/2048 向量單位。 要使用 100 像素的 font_height 進行轉換,它是:467/2048 * 100 像素,即 22.8 像素。 舍入為 23 像素。 乘以 64 得到 1472 1/64 像素。

如果您想看到它與像素值對齊,請將您的 font_height 設置為 2048,然后將生成的 face->glyph->advance.x (29888) 除以 64,得到 467。

以下腳本將為您提供您正在尋找的值:

#include <string>
#include <ft2build.h>
#include <stdio.h>
#include FT_FREETYPE_H

using namespace std;

int main() {
  FT_Library ft;
  FT_Face face;
  string filepathname = "./arialn.ttf";

  FT_Init_FreeType(&ft);
  FT_New_Face(ft, filepathname.c_str(), 0, &face);
  
  FT_UInt glyph_idx = FT_Get_Char_Index(face, '!');
  FT_Load_Glyph(face, glyph_idx, FT_LOAD_NO_SCALE);

  printf("width=%ld height=%ld\nhoriBearingX=%ld horiBearingY=%ld horiAdvance=%ld\n vertBearingX=%ld vertBearingY=%ld vertAdvance=%ld\n",face->glyph->metrics.width,face->glyph->metrics.height,face->glyph->metrics.horiBearingX,face->glyph->metrics.horiBearingY,face->glyph->metrics.horiAdvance,face->glyph->metrics.vertBearingX,face->glyph->metrics.vertBearingY,face->glyph->metrics.vertAdvance);
  FT_Pos advance_x = face->glyph->advance.x;
  printf("advance_x = %ld \n",advance_x);

  return 0;
}

Output:

width=183 height=1466
horiBearingX=148 horiBearingY=1466 horiAdvance=467
vertBearingX=-85 vertBearingY=228 vertAdvance=1922
advance_x = 467 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM