Win32 API 日本語リファレンス
ホームGraphics.Gdi › GetFontData

GetFontData

関数
フォントファイルのメトリックテーブルを取得する。
DLLGDI32.dll呼出規約winapi対応OSWindows 2000 以降

シグネチャ

// GDI32.dll
#include <windows.h>

DWORD GetFontData(
    HDC hdc,
    DWORD dwTable,
    DWORD dwOffset,
    void* pvBuffer,   // optional
    DWORD cjBuffer
);

パラメーター

名前方向説明
hdcHDCinデバイスコンテキストへのハンドルです。
dwTableDWORDinフォントデータを取得するフォントメトリックテーブルの名前です。このパラメーターには、Microsoft Corporation が発行する TrueType Font Files 仕様に記載されているメトリックテーブルのいずれかを指定できます。このパラメーターが 0 の場合、TrueType フォントファイルではファイルの先頭から、TrueType Collection ファイルでは現在選択されているフォントのデータの先頭から情報が取得されます。TrueType Collection ファイルでファイルの先頭からデータを取得するには、'ttcf' (0x66637474) を指定します。
dwOffsetDWORDinフォントメトリックテーブルの先頭から、関数が情報の取得を開始する位置までのオフセットです。このパラメーターが 0 の場合、情報は dwTable パラメーターで指定されたテーブルの先頭から取得されます。この値がテーブルのサイズ以上の場合、エラーが発生します。
pvBuffervoid*outoptionalフォント情報を受け取るバッファーへのポインターです。このパラメーターが NULL の場合、関数はフォントデータに必要なバッファーのサイズを返します。
cjBufferDWORDin取得する情報の長さ(バイト単位)です。このパラメーターが 0 の場合、GetFontDatadwTable パラメーターで指定されたデータのサイズを返します。

戻り値の型: DWORD

公式ドキュメント

GetFontData 関数は、TrueType フォントのフォントメトリックデータを取得します。

戻り値

関数が成功した場合、戻り値は返されたバイト数です。

関数が失敗した場合、戻り値は GDI_ERROR です。

解説(Remarks)

この関数は、フォント操作アプリケーションがフォントファイルから直接 TrueType フォント情報を取得するために使用することを目的としています。フォントの埋め込みについては、Font Embedding Reference を参照してください。

アプリケーションは、GetFontData 関数を使用して、TrueType フォントをドキュメントとともに保存できる場合があります。そのためには、アプリケーションは OUTLINETEXTMETRIC 構造体の otmfsType メンバーを確認して、フォントが埋め込み可能かどうかを判断します。otmfsType のビット 1 がセットされている場合、そのフォントの埋め込みは許可されません。ビット 1 がクリアされている場合、フォントは埋め込み可能です。ビット 2 がセットされている場合、埋め込みは読み取り専用です。埋め込みが許可されている場合、アプリケーションは dwTabledwOffsetcbData の各パラメーターに 0 を指定して、フォントファイル全体を取得できます。

アプリケーションがこの関数を使用して非 TrueType フォントの情報を取得しようとした場合、エラーが発生します。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// GDI32.dll
#include <windows.h>

DWORD GetFontData(
    HDC hdc,
    DWORD dwTable,
    DWORD dwOffset,
    void* pvBuffer,   // optional
    DWORD cjBuffer
);
[DllImport("GDI32.dll", ExactSpelling = true)]
static extern uint GetFontData(
    IntPtr hdc,   // HDC
    uint dwTable,   // DWORD
    uint dwOffset,   // DWORD
    IntPtr pvBuffer,   // void* optional, out
    uint cjBuffer   // DWORD
);
<DllImport("GDI32.dll", ExactSpelling:=True)>
Public Shared Function GetFontData(
    hdc As IntPtr,   ' HDC
    dwTable As UInteger,   ' DWORD
    dwOffset As UInteger,   ' DWORD
    pvBuffer As IntPtr,   ' void* optional, out
    cjBuffer As UInteger   ' DWORD
) As UInteger
End Function
' hdc : HDC
' dwTable : DWORD
' dwOffset : DWORD
' pvBuffer : void* optional, out
' cjBuffer : DWORD
Declare PtrSafe Function GetFontData Lib "gdi32" ( _
    ByVal hdc As LongPtr, _
    ByVal dwTable As Long, _
    ByVal dwOffset As Long, _
    ByVal pvBuffer As LongPtr, _
    ByVal cjBuffer As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetFontData = ctypes.windll.gdi32.GetFontData
GetFontData.restype = wintypes.DWORD
GetFontData.argtypes = [
    wintypes.HANDLE,  # hdc : HDC
    wintypes.DWORD,  # dwTable : DWORD
    wintypes.DWORD,  # dwOffset : DWORD
    ctypes.POINTER(None),  # pvBuffer : void* optional, out
    wintypes.DWORD,  # cjBuffer : DWORD
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('GDI32.dll')
GetFontData = Fiddle::Function.new(
  lib['GetFontData'],
  [
    Fiddle::TYPE_VOIDP,  # hdc : HDC
    -Fiddle::TYPE_INT,  # dwTable : DWORD
    -Fiddle::TYPE_INT,  # dwOffset : DWORD
    Fiddle::TYPE_VOIDP,  # pvBuffer : void* optional, out
    -Fiddle::TYPE_INT,  # cjBuffer : DWORD
  ],
  -Fiddle::TYPE_INT)
#[link(name = "gdi32")]
extern "system" {
    fn GetFontData(
        hdc: *mut core::ffi::c_void,  // HDC
        dwTable: u32,  // DWORD
        dwOffset: u32,  // DWORD
        pvBuffer: *mut (),  // void* optional, out
        cjBuffer: u32  // DWORD
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("GDI32.dll")]
public static extern uint GetFontData(IntPtr hdc, uint dwTable, uint dwOffset, IntPtr pvBuffer, uint cjBuffer);
"@
$api = Add-Type -MemberDefinition $sig -Name 'GDI32_GetFontData' -Namespace Win32 -PassThru
# $api::GetFontData(hdc, dwTable, dwOffset, pvBuffer, cjBuffer)
#uselib "GDI32.dll"
#func global GetFontData "GetFontData" sptr, sptr, sptr, sptr, sptr
; GetFontData hdc, dwTable, dwOffset, pvBuffer, cjBuffer   ; 戻り値は stat
; hdc : HDC -> "sptr"
; dwTable : DWORD -> "sptr"
; dwOffset : DWORD -> "sptr"
; pvBuffer : void* optional, out -> "sptr"
; cjBuffer : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "GDI32.dll"
#cfunc global GetFontData "GetFontData" sptr, int, int, sptr, int
; res = GetFontData(hdc, dwTable, dwOffset, pvBuffer, cjBuffer)
; hdc : HDC -> "sptr"
; dwTable : DWORD -> "int"
; dwOffset : DWORD -> "int"
; pvBuffer : void* optional, out -> "sptr"
; cjBuffer : DWORD -> "int"
; DWORD GetFontData(HDC hdc, DWORD dwTable, DWORD dwOffset, void* pvBuffer, DWORD cjBuffer)
#uselib "GDI32.dll"
#cfunc global GetFontData "GetFontData" intptr, int, int, intptr, int
; res = GetFontData(hdc, dwTable, dwOffset, pvBuffer, cjBuffer)
; hdc : HDC -> "intptr"
; dwTable : DWORD -> "int"
; dwOffset : DWORD -> "int"
; pvBuffer : void* optional, out -> "intptr"
; cjBuffer : DWORD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	gdi32 = windows.NewLazySystemDLL("GDI32.dll")
	procGetFontData = gdi32.NewProc("GetFontData")
)

// hdc (HDC), dwTable (DWORD), dwOffset (DWORD), pvBuffer (void* optional, out), cjBuffer (DWORD)
r1, _, err := procGetFontData.Call(
	uintptr(hdc),
	uintptr(dwTable),
	uintptr(dwOffset),
	uintptr(pvBuffer),
	uintptr(cjBuffer),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function GetFontData(
  hdc: THandle;   // HDC
  dwTable: DWORD;   // DWORD
  dwOffset: DWORD;   // DWORD
  pvBuffer: Pointer;   // void* optional, out
  cjBuffer: DWORD   // DWORD
): DWORD; stdcall;
  external 'GDI32.dll' name 'GetFontData';
result := DllCall("GDI32\GetFontData"
    , "Ptr", hdc   ; HDC
    , "UInt", dwTable   ; DWORD
    , "UInt", dwOffset   ; DWORD
    , "Ptr", pvBuffer   ; void* optional, out
    , "UInt", cjBuffer   ; DWORD
    , "UInt")   ; return: DWORD
●GetFontData(hdc, dwTable, dwOffset, pvBuffer, cjBuffer) = DLL("GDI32.dll", "dword GetFontData(void*, dword, dword, void*, dword)")
# 呼び出し: GetFontData(hdc, dwTable, dwOffset, pvBuffer, cjBuffer)
# hdc : HDC -> "void*"
# dwTable : DWORD -> "dword"
# dwOffset : DWORD -> "dword"
# pvBuffer : void* optional, out -> "void*"
# cjBuffer : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "gdi32" fn GetFontData(
    hdc: ?*anyopaque, // HDC
    dwTable: u32, // DWORD
    dwOffset: u32, // DWORD
    pvBuffer: ?*anyopaque, // void* optional, out
    cjBuffer: u32 // DWORD
) callconv(std.os.windows.WINAPI) u32;
proc GetFontData(
    hdc: pointer,  # HDC
    dwTable: uint32,  # DWORD
    dwOffset: uint32,  # DWORD
    pvBuffer: pointer,  # void* optional, out
    cjBuffer: uint32  # DWORD
): uint32 {.importc: "GetFontData", stdcall, dynlib: "GDI32.dll".}
pragma(lib, "gdi32");
extern(Windows)
uint GetFontData(
    void* hdc,   // HDC
    uint dwTable,   // DWORD
    uint dwOffset,   // DWORD
    void* pvBuffer,   // void* optional, out
    uint cjBuffer   // DWORD
);
ccall((:GetFontData, "GDI32.dll"), stdcall, UInt32,
      (Ptr{Cvoid}, UInt32, UInt32, Ptr{Cvoid}, UInt32),
      hdc, dwTable, dwOffset, pvBuffer, cjBuffer)
# hdc : HDC -> Ptr{Cvoid}
# dwTable : DWORD -> UInt32
# dwOffset : DWORD -> UInt32
# pvBuffer : void* optional, out -> Ptr{Cvoid}
# cjBuffer : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t GetFontData(
    void* hdc,
    uint32_t dwTable,
    uint32_t dwOffset,
    void* pvBuffer,
    uint32_t cjBuffer);
]]
local gdi32 = ffi.load("gdi32")
-- gdi32.GetFontData(hdc, dwTable, dwOffset, pvBuffer, cjBuffer)
-- hdc : HDC
-- dwTable : DWORD
-- dwOffset : DWORD
-- pvBuffer : void* optional, out
-- cjBuffer : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('GDI32.dll');
const GetFontData = lib.func('__stdcall', 'GetFontData', 'uint32_t', ['void *', 'uint32_t', 'uint32_t', 'void *', 'uint32_t']);
// GetFontData(hdc, dwTable, dwOffset, pvBuffer, cjBuffer)
// hdc : HDC -> 'void *'
// dwTable : DWORD -> 'uint32_t'
// dwOffset : DWORD -> 'uint32_t'
// pvBuffer : void* optional, out -> 'void *'
// cjBuffer : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("GDI32.dll", {
  GetFontData: { parameters: ["pointer", "u32", "u32", "pointer", "u32"], result: "u32" },
});
// lib.symbols.GetFontData(hdc, dwTable, dwOffset, pvBuffer, cjBuffer)
// hdc : HDC -> "pointer"
// dwTable : DWORD -> "u32"
// dwOffset : DWORD -> "u32"
// pvBuffer : void* optional, out -> "pointer"
// cjBuffer : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t GetFontData(
    void* hdc,
    uint32_t dwTable,
    uint32_t dwOffset,
    void* pvBuffer,
    uint32_t cjBuffer);
C, "GDI32.dll");
// $ffi->GetFontData(hdc, dwTable, dwOffset, pvBuffer, cjBuffer);
// hdc : HDC
// dwTable : DWORD
// dwOffset : DWORD
// pvBuffer : void* optional, out
// cjBuffer : DWORD
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public interface Gdi32 extends StdCallLibrary {
    Gdi32 INSTANCE = Native.load("gdi32", Gdi32.class);
    int GetFontData(
        Pointer hdc,   // HDC
        int dwTable,   // DWORD
        int dwOffset,   // DWORD
        Pointer pvBuffer,   // void* optional, out
        int cjBuffer   // DWORD
    );
}
@[Link("gdi32")]
lib LibGDI32
  fun GetFontData = GetFontData(
    hdc : Void*,   # HDC
    dwTable : UInt32,   # DWORD
    dwOffset : UInt32,   # DWORD
    pvBuffer : Void*,   # void* optional, out
    cjBuffer : UInt32   # DWORD
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef GetFontDataNative = Uint32 Function(Pointer<Void>, Uint32, Uint32, Pointer<Void>, Uint32);
typedef GetFontDataDart = int Function(Pointer<Void>, int, int, Pointer<Void>, int);
final GetFontData = DynamicLibrary.open('GDI32.dll')
    .lookupFunction<GetFontDataNative, GetFontDataDart>('GetFontData');
// hdc : HDC -> Pointer<Void>
// dwTable : DWORD -> Uint32
// dwOffset : DWORD -> Uint32
// pvBuffer : void* optional, out -> Pointer<Void>
// cjBuffer : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetFontData(
  hdc: THandle;   // HDC
  dwTable: DWORD;   // DWORD
  dwOffset: DWORD;   // DWORD
  pvBuffer: Pointer;   // void* optional, out
  cjBuffer: DWORD   // DWORD
): DWORD; stdcall;
  external 'GDI32.dll' name 'GetFontData';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetFontData"
  c_GetFontData :: Ptr () -> Word32 -> Word32 -> Ptr () -> Word32 -> IO Word32
-- hdc : HDC -> Ptr ()
-- dwTable : DWORD -> Word32
-- dwOffset : DWORD -> Word32
-- pvBuffer : void* optional, out -> Ptr ()
-- cjBuffer : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getfontdata =
  foreign "GetFontData"
    ((ptr void) @-> uint32_t @-> uint32_t @-> (ptr void) @-> uint32_t @-> returning uint32_t)
(* hdc : HDC -> (ptr void) *)
(* dwTable : DWORD -> uint32_t *)
(* dwOffset : DWORD -> uint32_t *)
(* pvBuffer : void* optional, out -> (ptr void) *)
(* cjBuffer : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library gdi32 (t "GDI32.dll"))
(cffi:use-foreign-library gdi32)

(cffi:defcfun ("GetFontData" get-font-data :convention :stdcall) :uint32
  (hdc :pointer)   ; HDC
  (dw-table :uint32)   ; DWORD
  (dw-offset :uint32)   ; DWORD
  (pv-buffer :pointer)   ; void* optional, out
  (cj-buffer :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetFontData = Win32::API::More->new('GDI32',
    'DWORD GetFontData(HANDLE hdc, DWORD dwTable, DWORD dwOffset, LPVOID pvBuffer, DWORD cjBuffer)');
# my $ret = $GetFontData->Call($hdc, $dwTable, $dwOffset, $pvBuffer, $cjBuffer);
# hdc : HDC -> HANDLE
# dwTable : DWORD -> DWORD
# dwOffset : DWORD -> DWORD
# pvBuffer : void* optional, out -> LPVOID
# cjBuffer : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目