GFX - gfxGetFont?

HI,

AFL Function Reference - GFXSELECTFONT

Initializes a font with the specified characteristics. Then selects the the as current for subsequent drawing operations.

Is there such thing as GfxGetFont so that

  • I can temporarily store current font,
  • GfxSelectFont, perform some tasks
  • Then GfxSelectFont back to original font setting.

There is no such function and no plan for it. Just change the font to what you need and/or keep the track of what you selected yourself in a text variable.

Try something like that @mmqp :

/*
Set/Restore Font face,size,weight
*/
procedure GfxSetFont(facename, size, weight) {
	local old, var;
	var = "font"+GetChartID();
	old = StaticVarGetText(var);
	StaticVarSetText(var, (facename+","+size+","+weight)+":"+old); 
	GfxSelectFont(facename, size, weight);
}

function GfxGetFont() {
	local current, var;
	var = "font"+GetChartID();
	current = StaticVarGetText(var);
	if (current == "") font = "";
	else font = StrExtract(current, 0, ':');
	
	return font;
}

procedure GfxRestoreFont() {
	local stack, var, font, size, weight, start, len;
	var = "font"+GetChartID();
	stack = StaticVarGetText(var);
	if (stack != "") {
		// remove current
		start = StrFind(stack,":");
		len = StrLen(stack);
		stack = StrRight(stack, len-start);
		StaticVarSetText(var, stack);
		// restore font
		if (stack != "") {
			font = StrExtract(stack, 0);
			size = StrToNum(StrExtract(stack, 1));
			weight = StrToNum(StrExtract(stack, 2));
			GfxSelectFont(font, size, weight); 
		}
	}
}

// Tests
GfxSetFont("Tahoma", 15, 500); 
GfxDrawText("Cool !", 20, 20, 200, 60);
font = GfxGetFont();
if (font != ("Tahoma,15,500")) {
	PopupWindow("Gfx Font Error 1", "");
}

GfxSetFont("Arial", 12, 300);
GfxDrawText("Cool !", 20, 70, 200, 130);
font = GfxGetFont();
if (font != ("Arial,12,300")) {
	PopupWindow("Gfx Font Error 2", "");
}

GfxRestoreFont();
GfxDrawText("Cool !", 20, 140, 200, 200);
font = GfxGetFont();
if (font != ("Tahoma,15,500")) {
	PopupWindow("Gfx Font Error 3", "");
}

Regards

Clever codes thanks @alligator

NO NO NO! Don't do that this way.

It does NOT make ANY sense to store this in static variable. Font is NOT kept between runs, so storing it in static variable is not only waste of resources but also would give you incorrect results.

If you really want to keep it a simple GLOBAL variable is enough and short code like this works:

g_fontFace = "Arial"; g_fontSize = 12; g_fontWeight = 400;

function GfxSetFont( facename, size, weight )
{
  global g_fontFace, g_fontSize, g_fontWeight;
  
  g_fontFace = facename;
  g_fontSize = size;
  g_fontWeight = weight;

  GfxSelectFont( facename, size, weight );
}

function GfxGetFontFace() { global g_fontFace; return g_fontFace; }
function GfxGetFontSize() { global g_fontSize; return g_fontSize; }
function GfxGetFontWeight() { global g_fontWeight; return g_fontWeight; }

BTW: As a general observation: static variables get overused seriously. I have seen dozens of codes that use static variables for no reason at all and what is worse (like the code posted in this thread) incorrectly. Don't do that. Only use static variables when you must keep the STATE. All graphics is restarted with defaults with each formula run so there is no state that should/need to be kept at all

4 Likes

@Tomasz You're surely right for the general use of static variables.

But in that case, it really depends on what you want to achieve. In mine, the user can select & restore size so it's necessary to remember the selected font (& all previous ones) and for every chart (actually for a zone in the chart in my case). I guess you were thinking about some kind of graphical toolbox, each tool doing their own thing without messing with other. Of course static are unnecessary in the latter case.