MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey / FindOrCreateFont

Method FindOrCreateFont

source/script_gui.cpp:7975–8152  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7973
7974
7975int GuiType::FindOrCreateFont(LPTSTR aOptions, LPTSTR aFontName, FontType *aFoundationFont, COLORREF *aColor)
7976// Returns the index of existing or new font within the sFont array (an index is returned so that
7977// caller can see that this is the default-gui-font whenever index 0 is returned). Returns -1
7978// on error, but still sets *aColor to be the color name, if any was specified in aOptions.
7979// To prevent a large number of font handles from being created (such as one for each control
7980// that uses something other than GUI_DEFAULT_FONT), it seems best to conserve system resources
7981// by creating new fonts only when called for. Therefore, this function will first check if
7982// the specified font already exists within the array of fonts. If not found, a new font will
7983// be added to the array.
7984{
7985 // Set default output parameter in case of early return:
7986 if (aColor) // Caller wanted color returned in an output parameter.
7987 *aColor = CLR_NONE; // Because we want CLR_DEFAULT to indicate a real color.
7988
7989 if (!*aOptions && !*aFontName)
7990 {
7991 // Relies on the fact that first item in the font array is always the default font.
7992 // If there are fonts, the default font should be the first one (index 0).
7993 // If not, we create it here:
7994 if (!sFontCount)
7995 {
7996 // sFont is created upon first use to conserve ~20 KB memory in non-GUI scripts.
7997 if ( !(sFont || (sFont = (FontType *)malloc(sizeof(FontType) * MAX_GUI_FONTS))) )
7998 // The allocation is small enough that failure would indicate it's unlikely the
7999 // program could continue for long, so it doesn't seem worth the added complication
8000 // to support recovering from this error:
8001 g_script.CriticalError(ERR_OUTOFMEM);
8002 // For simplifying other code sections, create an entry in the array for the default font
8003 // (GUI constructor relies on at least one font existing in the array).
8004 // Doesn't seem likely that DEFAULT_GUI_FONT face/size will change while a script is running,
8005 // or even while the system is running for that matter. I think it's always an 8 or 9 point
8006 // font regardless of desktop's appearance/theme settings.
8007 // MSDN: "It is not necessary (but it is not harmful) to delete stock objects by calling DeleteObject."
8008 sFont[sFontCount].hfont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
8009 // Get attributes of DEFAULT_GUI_FONT (name, size, etc.)
8010 FontGetAttributes(sFont[sFontCount]);
8011 ++sFontCount;
8012 }
8013 // Tell caller to return to default color, since this is documented behavior when
8014 // returning to default font:
8015 if (aColor) // Caller wanted color returned in an output parameter.
8016 *aColor = CLR_DEFAULT;
8017 return 0; // Always returns 0 since that is always the index of the default font.
8018 }
8019
8020 // Otherwise, a non-default name/size, etc. is being requested. Find or create a font to match it.
8021 if (!aFoundationFont) // Caller didn't specify a font whose attributes should be used as default.
8022 {
8023 if (sFontCount > 0)
8024 aFoundationFont = &sFont[0]; // Use default if it exists.
8025 else
8026 return -1; // No error displayed since shouldn't happen if things are designed right.
8027 }
8028
8029 // Copy the current default font's attributes into our local font structure.
8030 // The caller must ensure that mCurrentFontIndex array element exists:
8031 FontType font = *aFoundationFont;
8032 if (*aFontName)

Callers

nothing calls this directly

Calls 11

omit_leading_whitespaceFunction · 0.85
StrChrAnyFunction · 0.85
ctoupperFunction · 0.85
IsNumericFunction · 0.85
ColorNameToBGRFunction · 0.85
rgb_to_bgrFunction · 0.85
ATOIFunction · 0.85
FontExistFunction · 0.85
ValueErrorFunction · 0.85
CriticalErrorMethod · 0.80
ScriptErrorMethod · 0.80

Tested by

no test coverage detected