How does GDI function GetFontData know which font? - winapi

I have some old code that I want to port to metro. The old code uses the GDI function GetFontData to get font data from a table whose tag is being passed to it. I plan to replace it with IDWriteFontFace::TryGetFontTable.To do so I have to create a IDWriteFontFace object which requires path to the font file corresponding to the font it represents. But what I don't understand is where does GetFontData figure out the font files from whose tables it is supposed to fetch the data from? Does it do so from the device context that is passed to it?

The font is the one currently selected in the Device Context. You can retrieve it by using GetCurrentObject with object type OBJ_FONT. You can then safely cast the returned HGDIOBJ to a HFONT.
As for retrieving the font file name, that's not easy. See that SO Question

There always some font selected in Device Context (I say always because there is default font). So GetFontData returns that font depending on HDC hdc parameter.
As manuell mentioned GetFontData is similar to (HFONT)(GetCurrentObject(hdc, OBJ_FONT))

Related

Could I add a caption over an existing image using Go?

If I have font files (for different fonts), is it possible to create a caption over image using Go?
You can use my gofont library for that. See the documentation for the minimal interface.
You basically call gofont.LoadFromFile("font.ttf") and on the returned Font object, you can set the PixelHeight and R/G/B values to control the draw output.
Call font.Write() on the object to render text into your draw.Image. It is even possible to center text, using the returned x and y values returned by the function.
The library is pure Go and should work on all platforms.

use ghost script to extract single spot color

I am trying to use ghostscript to extract the image for a single spot color (from a PDF), REGARDLESS of whether it would be visible when printed.
I tried using the tiffsep device, but the problem is that any of the spot color that is hidden by objects above does not get sent out.
Is there any device, or settings that would allow simply any objects regardless of visibility to be extracted to a bitmap file.
If an object overlies another in PostScript then it is absolutely correct that it causes the underlying object not to render in that position (modulo overprint) because PostScript has an opaque imaging model.
So no, you can't prevent this, its supposed to work like that.

How to check the success of CreateFont() [duplicate]

In Windows, the CreateFontIndirect() call can silently substitute compatible fonts if the requested font is not requested. The GetObject() call does not reflect this substitution; it returns the same LOGFONT passed in. How can I find what font was actually created? Alternately, how can I force Windows to only return the exact font requested?
In Windows, the CreateFontIndirect() call can silently substitute compatible fonts if the requested font is not requested. The GetObject() call does not reflect this substitution; it returns the same LOGFONT passed in.
It's not CreateFontIndirect that's doing the substitution. The substitution happens when the font is selected into the DC. CreateFontIndirect just gives you a handle to an internal copy of the LOGFONT. That's why GetObject gives you the same LOGFONT back.
How can I find what font was actually created?
If you select the HFONT into the target DC, you can then ask the DC for the information about the font that was actually chosen as the best match to the LOGFONT.
The face name is available with GetTextFace.
You can get metrics with GetTextMetrics.
If the selected font is TrueType or OpenType, you can get additional metrics with GetOutlineTextMetrics.
That essentially tells you what font was actually created.
Aside:
When doing something like print preview, you can start with a LOGFONT, select it into the printer DC (or IC), grab the details of the actual font (printers often substitute fonts), and then create a new LOGFONT that's more representative of the actual font. Select that into the screen DC, and--with appropriate size conversions--do a pretty good match of what the user will actually get.
To get the appropriate font on different language versions of the OS,
call EnumFontFamiliesEx with the desired font characteristics in the
LOGFONT structure, then retrieve the appropriate typeface name and
create the font using CreateFont or CreateFontIndirect.
While it's not a universal way to get the actual font name from an HFONT, you can check beforehand what CreateFontIndirect would (most likely) return.
Judging from how MSDN suggest this as a good solution for getting a font family from the attributes it seems like the way Windows internally performs the substitution.

Getting font handle for already installed font

Can anyone tell me how I get the HFONT handle to send a WM_SETFONT message? The font is calibri and should already be installed on Windows, all I can see is the add/create functions here
http://msdn.microsoft.com/en-us/library/windows/desktop/dd144821%28v=vs.85%29.aspx
the CreateFont function states it returns a handle but Im wondering why I would need to create something thats already there.
The only thing that's "there" is a set of font files in the c:\windows\fonts directory. They contain the outline of the font. You really do have to call CreateFont(). At which point Windows actually accesses the file and creates the specific font you ask for. With the requested height, weight, etc.

How do I tell if a font is a symbol font?

Given an HFONT, how do I tell if it's a symbol font? A pdf library I'm using needs to treat symbol fonts differently, so I need a way to programatically tell if any given font is a symbol font or not.
Use GetObject to get the font's properties to a LOGFONT structure. Check the lfCharSet member; if it's SYMBOL_CHARSET, you have a symbol font.
Mark Ransom's answer is going to work 99.999% of the time, but there's a theoretical possibility that it could give the wrong answer.
To avoid this possibility, you should use GetTextMetrics to get the TEXTMETRICS of the actual font and check if the tmCharSet is SYMBOL_CHARSET.
What's the difference between checking lfCharSet and tmCharSet?
When you create an HFONT, Windows makes an internal copy of the LOGFONT. It describes the font you want, which could be different than the font you get.
When you select the HFONT into a device (or information) context, the font mapper finds the actual font that best matches the LOGFONT associated with that HFONT. The best match, however, might not be an exact match. So when you need to find out something about the actual font, you should take care to query the HDC rather than the HFONT.
If you query the HFONT with GetObject, you just get the original LOGFONT back. GetObject doesn't tell you anything about the actual font because it doesn't know what actual font the font mapper chose (or will choose).
APIs that ask about the font selected into a particular DC, like GetTextMetrics, GetTextFace, etc., will give you information about the actual font.
For this problem, Mark's answer (using GetObject) is probably always going to work, because the odds of the font mapper choosing a symbol font when you want a textual font (or vice versa) are minuscule. In general, though, when you want to know something about the actual font, find a way to ask the HDC.

Resources