diff --git a/README.md b/README.md index 74007f07..31faaa17 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ In the Frame rows, a name that starts with `:` is a method of the widget that th | [Frame: Region](docs/API.md#regionsetpointpoint-one-argument-form) | `:SetPoint("point")` (one-arg form), `:SetSize` / `:GetSize`, `:GetRect`, `:IsMouseOver`, `:IsDragging`, `:IsProtected` / `:CanChangeProtectedState` | | [Frame: Frame](docs/API.md#createframeframetype--name-parent-template-id) | `CreateFrame` template lists and `id`, `GetMouseFoci`, `:SetShown`, `:SetClampRectInsets` / `:GetClampRectInsets`, `:SetResizeBounds`, `:GetBoundsRect`, `:GetEffectiveAlpha`, `:HookScript`, `:IsEventRegistered`, `:RegisterUnitEvent`, `:SetPropagateKeyboardInput` / `:GetPropagateKeyboardInput`, `:SetDontSavePosition` / `:GetDontSavePosition`, `:SetAttribute`, `:SetAttributeNoHandler`, `:ClearAttribute`, `:GetAttribute`, `:CanChangeAttribute`, `:AllowAttributeChanges` | | [Frame: Button](docs/API.md#buttonsetnormalfontobjectfont--buttongetnormalfontobject) | `:SetEnabled`, `:SetFormattedText`, `:SetNormalFontObject` / `:GetNormalFontObject`, `:SetMotionScriptsWhileDisabled` / `:GetMotionScriptsWhileDisabled`, `:RegisterForClicks("AnyUp"/"AnyDown")`, `:RegisterForMouse`, `GetClickFrame` | -| [Frame: FontString](docs/API.md#fontstringgetstringheight) | `:GetStringHeight`, `:GetUnboundedStringWidth`, `:GetWrappedWidth`, `:GetNumLines`, `:GetLineHeight`, `:GetFieldSize`, `:IsTruncated`, `:SetMaxLines` / `:GetMaxLines`, `:SetWordWrap` / `:CanWordWrap`, `:SetIndentedWordWrap` / `:GetIndentedWordWrap` (also on fonts, edit boxes, message frames, and SimpleHTML), `:SetFormattedText`, `:SetRotation` / `:GetRotation` | +| [Frame: FontString](docs/API.md#fontstringgetstringheight) | `:GetStringHeight`, `:GetUnboundedStringWidth`, `:GetWrappedWidth`, `:GetNumLines`, `:GetLineHeight`, `:GetFontHeight` / `:SetFontHeight` (also on fonts), `:GetFieldSize`, `:IsTruncated`, `:SetMaxLines` / `:GetMaxLines`, `:SetWordWrap` / `:CanWordWrap`, `:SetIndentedWordWrap` / `:GetIndentedWordWrap` (also on fonts, edit boxes, message frames, and SimpleHTML), `:SetFormattedText`, `:SetRotation` / `:GetRotation` | | [Frame: Texture](docs/API.md#texturesetrotationangle--cx-cy) | `:SetRotation` / `:GetRotation`, `:SetVertexOffset` / `:GetVertexOffset`, `:SetColorTexture`, `:SetAtlas` / `:GetAtlas`, `:ResetTexCoord`, `:SetSpriteSheetCell`, `:SetDesaturation` / `:GetDesaturation`, `IsDesaturateSupported`, `:SetMask`, `frame:CreateMaskTexture`, `:AddMaskTexture` / `:RemoveMaskTexture`, `:GetNumMaskTextures`, `:GetMaskTexture` | | [Frame: EditBox](docs/API.md#editboxsetcursorpositionposition) | `:SetCursorPosition` / `:GetCursorPosition`, `:GetUTF8CursorPosition`, `:ClearHighlightText`, `:SetHighlightColor` / `:GetHighlightColor`, `:HasFocus`, `GetCurrentKeyBoardFocus`, `:HasText`, `:ClearHistory`, `:Enable` / `:Disable` / `:IsEnabled`, `:SetCountInvisibleLetters` / `:IsCountInvisibleLetters`, `:IsInIMECompositionMode` | | [Frame: StatusBar](docs/API.md#statusbarsetreversefillisreversefill--statusbargetreversefill) | `:SetReverseFill` / `:GetReverseFill`, `:SetRotatesTexture` / `:GetRotatesTexture`, `:SetFillStyle` / `:GetFillStyle`, `:SetColorFill`, `:SetStatusBarDesaturated` / `:IsStatusBarDesaturated`, `:SetStatusBarDesaturation` / `:GetStatusBarDesaturation` | diff --git a/TODO.md b/TODO.md index 61045d11..c51b159a 100644 --- a/TODO.md +++ b/TODO.md @@ -4116,7 +4116,7 @@ Likely worth it (small, ported code uses them): `IsObjectLoaded`, `RotateTextures` (on `Texture::Transform`), `AbortDrag`. - Slider: `SetObeyStepOnDrag`, `SetStepsPerPage`, `IsDraggingThumb`, `SetEnabled` (also EditBox `SetEnabled`). -- FontString / Font: `SetFontHeight` / `GetFontHeight`, `SetTextScale` / +- FontString / Font: `SetTextScale` / `GetTextScale`, `ClearText`, `SetTextToFit`, `GetUnboundedStringWidthForText`, `FindCharacterIndexAtCoordinate`. - Texture: `GetTextureFilePath`, `ClearVertexOffsets`. diff --git a/docs/API.md b/docs/API.md index 51abd84e..0bd65d8a 100644 --- a/docs/API.md +++ b/docs/API.md @@ -273,6 +273,7 @@ build instructions. - [`fontstring:GetWrappedWidth()`](#fontstringgetwrappedwidth) - [`fontstring:GetNumLines()`](#fontstringgetnumlines) - [`fontstring:GetLineHeight()`](#fontstringgetlineheight) + - [`fontstring:GetFontHeight([calculated])` / `fontstring:SetFontHeight(height)`](#fontstringgetfontheightcalculated--fontstringsetfontheightheight) - [`fontstring:IsTruncated()`](#fontstringistruncated) - [`fontstring:SetMaxLines(maxLines)` / `fontstring:GetMaxLines()`](#fontstringsetmaxlinesmaxlines--fontstringgetmaxlines) - [`fontstring:SetIndentedWordWrap([indent])` / `fontstring:GetIndentedWordWrap()`](#fontstringsetindentedwordwrapindent--fontstringgetindentedwordwrap) @@ -6619,6 +6620,31 @@ Returns the height of one text line in UI pixels — the font height, without the `SetSpacing` value. Use it with `GetNumLines` and `GetSpacing` to reconstruct `GetStringHeight` per line. +### `fontstring:GetFontHeight([calculated])` / `fontstring:SetFontHeight(height)` + +`GetFontHeight` returns the height of the font, in the same units as the +second return value of `GetFont`. + +- `calculated` is `true` when you omit it. A string that draws at the pixel + size of its font then returns that pixel size. +- When `calculated` is `false`, the result is always equal to the second + return value of `GetFont`. + +`SetFontHeight` changes only the height. The font file and the flags +(`OUTLINE`, `THICKOUTLINE`, `MONOCHROME`) stay the same, as in a `SetFont` +call with the current file and flags. A height of `0` or less has no effect. +If the string has no font, `SetFontHeight` raises an error. + +Font objects have the same pair. On a Font object, `GetFontHeight` takes no +argument. The font strings that use the font change with it. + +```lua +local f = frame:CreateFontString(nil, "ARTWORK", "GameFontNormal") +f:SetFontHeight(18) -- same file and flags, new height +print(f:GetFontHeight(false) == select(2, f:GetFont())) -- true +GameFontNormal:SetFontHeight(14) -- strings that use GameFontNormal change too +``` + ### `fontstring:IsTruncated()` Returns `true` when the engine cut the text off with an ellipsis diff --git a/src/Offsets.h b/src/Offsets.h index fcb67dce..e901e68d 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -11569,6 +11569,26 @@ enum Offsets { OFF_SIMPLEFONT_ATTR_FLAGS = 0x38, OFF_SIMPLEFONT_STYLE_FLAGS = 0x54, VTBL_SIMPLEFONT_UPDATE_OBJECTS = 0x14, + // The font height, internal units; the shared Font/EditBox/... GetFont + // body 0x0079F3B0 pushes it as its second value (`mov eax,[esi+0x48]`), + // and the shared SetFont 0x0079F210 stores it at 0x0079F329. + OFF_SIMPLEFONT_HEIGHT = 0x48, + // The engine's own GetFont / SetFont Lua methods of FontString (table + // 0x0087C1D8) and Font (table 0x0087C7C8), `int __fastcall(L)`. Each + // resolves its own self; SetFont reads (file, height[, flags]) from stack + // slots 2-4, GetFont pushes (file, height, flagsText). + FUN_SCRIPT_FONTSTRING_GET_FONT = 0x0079D400, + FUN_SCRIPT_FONTSTRING_SET_FONT = 0x0079D4F0, + FUN_SCRIPT_FONT_GET_FONT = 0x007A0330, + FUN_SCRIPT_FONT_SET_FONT = 0x007A0270, + // The two engine calls of every font-height push (GetFont 0x0079D4A5 / + // 0x0079F3D1): `DivideByScreenScaleX(GetScaledScreenAspect() * 1024 * h)`. + // GetScaledScreenAspect: `float __cdecl()`, returns + // [VAR_COORD_ASPECT_COMPENSATION] in ST0. DivideByScreenScaleX: + // `float __stdcall(float)`, ST0 = arg / [VAR_COORD_DDC_WIDTH_SCALE], left + // at x87 precision (the caller stores it with `fstp qword`). + FUN_COORD_GET_SCALED_SCREEN_ASPECT = 0x0041AD70, + FUN_COORD_DIVIDE_BY_SCREEN_SCALE_X = 0x0041AE40, // Each class's font, as its SetJustifyH passes it to 0x0079FC20, and the // class's Lua type id and method registry. A Font object is its own font; // SimpleHTML has one per text type, `fonts[FUN_SIMPLEHTML_READ_TEXT_TYPE(L)]` diff --git a/src/api/Documentation.cpp b/src/api/Documentation.cpp index 163b2f98..a29f746d 100644 --- a/src/api/Documentation.cpp +++ b/src/api/Documentation.cpp @@ -162,6 +162,7 @@ const WidgetSystem kWidgetSystems[] = { {Offsets::VAR_FRAME_METHOD_REGISTRY, "SimpleFrameAPI"}, {Offsets::VAR_TEXTURE_METHOD_REGISTRY, "SimpleTextureAPI"}, {Offsets::VAR_FONTSTRING_METHOD_REGISTRY, "SimpleFontStringAPI"}, + {Offsets::VAR_FONT_METHOD_REGISTRY, "SimpleFontAPI"}, {Offsets::VAR_EDITBOX_METHOD_REGISTRY, "SimpleEditBoxAPI"}, {Offsets::VAR_MODEL_METHOD_REGISTRY, "SimpleModelAPI"}, }; diff --git a/src/fontstring/FontHeight.cpp b/src/fontstring/FontHeight.cpp new file mode 100644 index 00000000..947bc0d2 --- /dev/null +++ b/src/fontstring/FontHeight.cpp @@ -0,0 +1,156 @@ +// This file is part of ClassicAPI. +// +// ClassicAPI is free software: you can redistribute it and/or modify it under the terms +// of the GNU General Public License as published by the Free Software Foundation, either +// version 3 of the License, or (at your option) any later version. +// +// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// ClassicAPI. If not, see . + +// `GetFontHeight` and `SetFontHeight(height)` on FontString and Font — the two +// classes retail gives them (SimpleFontStringAPI, SimpleFontAPI). +// +// The getters push the height GetFont pushes as its second value, through +// the same two engine calls GetFont makes, so `GetFontHeight()` equals +// `select(2, GetFont())` bit for bit. FontString's optional `calculated` +// (default true) is the `scaled` argument of the engine's own height getter +// 0x007727B0 — 4.3.4's CSimpleFontString::GetFontHeight(recalculate): with it +// set, a string drawn at its font's pixel size (bit 0x200) reports that size. +// GetFont passes 0. +// +// The setters are the engine's own SetFont with the current file and flags: +// they rearrange the Lua stack into SetFont's (self, file, height, flags) +// from GetFont's results and call the class's SetFont method. That keeps its +// unit conversion, its unchanged-font skip, its rebuild and the push to the +// strings that follow a Font. The flags survive the trip as text: GetFont +// formats all three (0x006F1AD0) and SetFont parses all three (0x006F1A90). + +#include "Game.h" +#include "Offsets.h" + +#include + +namespace FontString::FontHeight { +namespace { + +// FUN_FONTSTRING_FONT_HEIGHT — __thiscall(fs, scaled-on-stack); dummy-EDX form. +using FsFontHeight_t = float(__fastcall *)(void *fs, void *edx, int scaled); +using GetScaledScreenAspect_t = float(__cdecl *)(); +// Declared double so the quotient is read from ST0 with `fstp qword`, as +// GetFont stores it, not narrowed to float. +using DivideByScreenScaleX_t = double(__stdcall *)(float); + +// GetFont's push (0x0079D4A5..0x0079D4C4). `aspect * 1024` is exact, so the +// float product rounds once, as GetFont's `fstp dword` does. +double ToUiHeight(float internal) { + const float aspect = + reinterpret_cast(Offsets::FUN_COORD_GET_SCALED_SCREEN_ASPECT)(); + const float scaled = aspect * static_cast(Offsets::UI_COORD_SCALE_UNIT) * internal; + return reinterpret_cast(Offsets::FUN_COORD_DIVIDE_BY_SCREEN_SCALE_X)(scaled); +} + +void *FontStringSelf(void *L) { return Game::Lua::ResolveFontString(L); } +void *FontSelf(void *L) { return Game::Lua::ResolveTypedObject(L, 1, Offsets::VAR_FONT_LUA_TYPE_ID); } + +int __fastcall Script_FontStringGetFontHeight(void *L) { + void *fs = FontStringSelf(L); + if (fs == nullptr) + return 0; + const bool calculated = Game::Lua::Type(L, 2) == Game::Lua::TYPE_NIL || Game::Lua::ToBoolean(L, 2) != 0; + const float h = reinterpret_cast(Offsets::FUN_FONTSTRING_FONT_HEIGHT)( + fs, nullptr, calculated ? 1 : 0); + Game::Lua::PushNumber(L, ToUiHeight(h)); + return 1; +} + +int __fastcall Script_FontGetFontHeight(void *L) { + void *font = FontSelf(L); + if (font == nullptr) + return 0; + Game::Lua::PushNumber(L, ToUiHeight(Game::Read(font, Offsets::OFF_SIMPLEFONT_HEIGHT))); + return 1; +} + +template +int __fastcall Script_SetFontHeight(void *L) { + void *object = Self(L); + if (object == nullptr) + return 0; + const char *name = Game::Lua::ObjectDisplayName(object); + if (!Game::Lua::IsNumber(L, 2)) { + Game::Lua::Error(L, "Usage: %s:SetFontHeight(height)", name); + return 0; + } + Game::Lua::SetTop(L, 2); // self, height + reinterpret_cast(GetFont)(L); // self, height, file, old, flags + if (Game::Lua::Type(L, 3) != Game::Lua::TYPE_STRING) { + Game::Lua::Error(L, "%s:SetFontHeight(): Font not set", name); + return 0; + } + Game::Lua::Remove(L, 4); // self, height, file, flags + Game::Lua::PushValue(L, 2); + Game::Lua::Remove(L, 2); // self, file, flags, height + Game::Lua::Insert(L, 3); // self, file, height, flags + reinterpret_cast(SetFont)(L); + return 0; +} + +const Game::Lua::FrameMethodEntry kFontStringMethods[] = { + {"GetFontHeight", &Script_FontStringGetFontHeight}, + {"SetFontHeight", &Script_SetFontHeight<&FontStringSelf, Offsets::FUN_SCRIPT_FONTSTRING_GET_FONT, + Offsets::FUN_SCRIPT_FONTSTRING_SET_FONT>}, +}; +const Game::Lua::FrameMethodEntry kFontMethods[] = { + {"GetFontHeight", &Script_FontGetFontHeight}, + {"SetFontHeight", + &Script_SetFontHeight<&FontSelf, Offsets::FUN_SCRIPT_FONT_GET_FONT, Offsets::FUN_SCRIPT_FONT_SET_FONT>}, +}; + +const Game::Doc::Field kFontStringGetArgs[] = { + Game::Doc::Opt("calculated", "bool", "true", + "When true, a string drawn at its font's own pixel size returns that size."), +}; +const Game::Doc::Field kFontStringGetRets[] = { + Game::Doc::Req("height", "uiUnit"), +}; +const Game::Doc::Field kFontStringSetArgs[] = { + Game::Doc::Req("height", "uiUnit"), +}; +const Game::Doc::Field kFontGetRets[] = { + Game::Doc::Req("height", "number"), +}; +const Game::Doc::Field kFontSetArgs[] = { + Game::Doc::Req("height", "number"), +}; +const Game::Doc::Function kFontStringGet{"Returns the height of the font.", kFontStringGetArgs, + kFontStringGetRets}; +const Game::Doc::Function kFontStringSet{"Sets the height of the font and keeps its file and flags.", + kFontStringSetArgs, {}}; +const Game::Doc::Function kFontGet{"Returns the height of the font.", {}, kFontGetRets}; +const Game::Doc::Function kFontSet{"Sets the height of the font and keeps its file and flags.", kFontSetArgs, + {}}; + +const Game::Doc::Method kFontStringDocs[] = { + {"GetFontHeight", &kFontStringGet}, + {"SetFontHeight", &kFontStringSet}, +}; +const Game::Doc::Method kFontDocs[] = { + {"GetFontHeight", &kFontGet}, + {"SetFontHeight", &kFontSet}, +}; + +void RegisterLuaFunctions() { + Game::Lua::RegisterFrameMethods(reinterpret_cast(Offsets::VAR_FONTSTRING_METHOD_REGISTRY), + kFontStringMethods, 2, kFontStringDocs, 2); + Game::Lua::RegisterFrameMethods(reinterpret_cast(Offsets::VAR_FONT_METHOD_REGISTRY), kFontMethods, + 2, kFontDocs, 2); +} + +const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; + +} // namespace +} // namespace FontString::FontHeight