win32 gui: center double-wide characters
This commit is contained in:
parent
921f094509
commit
5cc6724a07
2 changed files with 42 additions and 9 deletions
|
@ -87,7 +87,7 @@ pub fn render(
|
||||||
.left = 0,
|
.left = 0,
|
||||||
.top = 0,
|
.top = 0,
|
||||||
.right = if (double_width)
|
.right = if (double_width)
|
||||||
@as(f32, @floatFromInt(font.cell_size.x)) * @as(f32, @floatFromInt(font.cell_size.x))
|
@as(f32, @floatFromInt(font.cell_size.x)) * 2
|
||||||
else
|
else
|
||||||
@as(f32, @floatFromInt(font.cell_size.x)),
|
@as(f32, @floatFromInt(font.cell_size.x)),
|
||||||
.bottom = @floatFromInt(font.cell_size.y),
|
.bottom = @floatFromInt(font.cell_size.y),
|
||||||
|
@ -100,7 +100,7 @@ pub fn render(
|
||||||
self.render_target.DrawText(
|
self.render_target.DrawText(
|
||||||
@ptrCast(utf16.ptr),
|
@ptrCast(utf16.ptr),
|
||||||
@intCast(utf16.len),
|
@intCast(utf16.len),
|
||||||
font.text_format,
|
if (double_width) font.text_format_double else font.text_format_single,
|
||||||
&rect,
|
&rect,
|
||||||
&self.white_brush.ID2D1Brush,
|
&self.white_brush.ID2D1Brush,
|
||||||
.{},
|
.{},
|
||||||
|
|
|
@ -23,11 +23,13 @@ pub fn init() void {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const Font = struct {
|
pub const Font = struct {
|
||||||
text_format: *win32.IDWriteTextFormat,
|
text_format_single: *win32.IDWriteTextFormat,
|
||||||
|
text_format_double: *win32.IDWriteTextFormat,
|
||||||
cell_size: XY(u16),
|
cell_size: XY(u16),
|
||||||
|
|
||||||
pub fn init(dpi: u32, size: f32, face: *const FontFace) Font {
|
pub fn init(dpi: u32, size: f32, face: *const FontFace) Font {
|
||||||
var text_format: *win32.IDWriteTextFormat = undefined;
|
var text_format_single: *win32.IDWriteTextFormat = undefined;
|
||||||
|
|
||||||
{
|
{
|
||||||
const hr = global.dwrite_factory.CreateTextFormat(
|
const hr = global.dwrite_factory.CreateTextFormat(
|
||||||
face.ptr(),
|
face.ptr(),
|
||||||
|
@ -37,14 +39,43 @@ pub const Font = struct {
|
||||||
.NORMAL, // stretch
|
.NORMAL, // stretch
|
||||||
win32.scaleDpi(f32, size, dpi),
|
win32.scaleDpi(f32, size, dpi),
|
||||||
win32.L(""), // locale
|
win32.L(""), // locale
|
||||||
&text_format,
|
&text_format_single,
|
||||||
);
|
);
|
||||||
if (hr < 0) std.debug.panic(
|
if (hr < 0) std.debug.panic(
|
||||||
"CreateTextFormat '{}' height {d} failed, hresult=0x{x}",
|
"CreateTextFormat '{}' height {d} failed, hresult=0x{x}",
|
||||||
.{ std.unicode.fmtUtf16Le(face.slice()), size, @as(u32, @bitCast(hr)) },
|
.{ std.unicode.fmtUtf16Le(face.slice()), size, @as(u32, @bitCast(hr)) },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
errdefer _ = text_format.IUnknown.Release();
|
errdefer _ = text_format_single.IUnknown.Release();
|
||||||
|
|
||||||
|
var text_format_double: *win32.IDWriteTextFormat = undefined;
|
||||||
|
{
|
||||||
|
const hr = global.dwrite_factory.CreateTextFormat(
|
||||||
|
face.ptr(),
|
||||||
|
null,
|
||||||
|
.NORMAL, //weight
|
||||||
|
.NORMAL, // style
|
||||||
|
.NORMAL, // stretch
|
||||||
|
win32.scaleDpi(f32, size, dpi),
|
||||||
|
win32.L(""), // locale
|
||||||
|
&text_format_double,
|
||||||
|
);
|
||||||
|
if (hr < 0) std.debug.panic(
|
||||||
|
"CreateTextFormat '{}' height {d} failed, hresult=0x{x}",
|
||||||
|
.{ std.unicode.fmtUtf16Le(face.slice()), size, @as(u32, @bitCast(hr)) },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
errdefer _ = text_format_double.IUnknown.Release();
|
||||||
|
|
||||||
|
{
|
||||||
|
const hr = text_format_double.SetTextAlignment(win32.DWRITE_TEXT_ALIGNMENT_CENTER);
|
||||||
|
if (hr < 0) fatalHr("SetTextAlignment", hr);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const hr = text_format_double.SetParagraphAlignment(win32.DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
|
||||||
|
if (hr < 0) fatalHr("SetParagraphAlignment", hr);
|
||||||
|
}
|
||||||
|
|
||||||
const cell_size: XY(u16) = blk: {
|
const cell_size: XY(u16) = blk: {
|
||||||
var text_layout: *win32.IDWriteTextLayout = undefined;
|
var text_layout: *win32.IDWriteTextLayout = undefined;
|
||||||
|
@ -52,7 +83,7 @@ pub const Font = struct {
|
||||||
const hr = global.dwrite_factory.CreateTextLayout(
|
const hr = global.dwrite_factory.CreateTextLayout(
|
||||||
win32.L("█"),
|
win32.L("█"),
|
||||||
1,
|
1,
|
||||||
text_format,
|
text_format_single,
|
||||||
std.math.floatMax(f32),
|
std.math.floatMax(f32),
|
||||||
std.math.floatMax(f32),
|
std.math.floatMax(f32),
|
||||||
&text_layout,
|
&text_layout,
|
||||||
|
@ -73,13 +104,15 @@ pub const Font = struct {
|
||||||
};
|
};
|
||||||
|
|
||||||
return .{
|
return .{
|
||||||
.text_format = text_format,
|
.text_format_single = text_format_single,
|
||||||
|
.text_format_double = text_format_double,
|
||||||
.cell_size = cell_size,
|
.cell_size = cell_size,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *Font) void {
|
pub fn deinit(self: *Font) void {
|
||||||
_ = self.text_format.IUnknown.Release();
|
_ = self.text_format_single.IUnknown.Release();
|
||||||
|
_ = self.text_format_double.IUnknown.Release();
|
||||||
self.* = undefined;
|
self.* = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue