Weird character imported in Qt QString from a system action in osx - c++

I have an app that at some point displays printer info.
On mac, when adding a printer, the default location is set as "me's iMac" unless user changes it. (me being the user name). But the apostrophe is not quite an apostrophe...
me’s iMac
This is how it shows in system printer dialog:
When I try to display the location in my app - using
QString(pCups->getValue("printer-location", i))
I get
How can I fix this substitution ? are they using a special font ? is there some way I can recognize it ?
I am thinking that if the user name contains other special characters, they may also be mapped to non-UTF-8 ones by the system add printer dialog. I don't have control over it... How can I find and fix any of the fancy symbols with standard ones ?
OSX 10.6-10.9
edit: for now I just did a replace with that specific character... But I think I may have other surprises if I don't do a more generic replace for all non-UTF-8 fancy characters
QString(pCups->getValue("printer-location", i)).replace("’", "'");
// this is what it looks like on Windows, though that particular item will only be used on unix (obviously... cups)
QString(pCups->getValue("printer-location", i)).replace("’", "'");

According to this page, QString interprets its C-style string argument as Latin-1 (ISO-8859-1), not UTF-8. The advice given is to always construct QStrings from C-style strings using QString::fromUtf8() instead.
There's an alternative noted on that page, but you are discouraged from using it because it's a process-global setting and therefore may interfere with libraries or the like. You can call QTextCodec::setCodecForCStrings() passing the UTF-8 text codec (QTextCodec::codecForName(“UTF-8”)) early in the startup of your app.

Related

How to skip choosing folder in microsoft pdf printer?

I use MFC and understand how to skip configuration menu (set pInfo->m_bDirect to false). But I want to set the folder and filename programmatically, without a special dialog. If it is impossible, can you advise me about a PDF printer with this functionality (may be changing configuration file for this goal)?
One question at a time, has limitations so here goes.
Q.) I want set folder and filename programmatically without special dialog.
A.) If you look at the output port of a recent windows installation of Microsoft Print To PDF
You may note it is set to PORTPROMPT: and that is exactly what causes the request for a filename.
You might note lower down, I have several ports set to a filename, and the fourth one down is called "My Print to PDF"
So very last century methodology; when I print with a *duplicate printer but give it a different name I can use different page ratios etc., without altering the built in standard one. The output for a file will naturally be built:-
Exactly in one repeatable location, that I can file monitor and rename it, based on the source calling the print sequence, such that if it is my current default printer I can right click files to print to a known \folder\file.pdf
Your next question will be how to change the target location and that is done behind the scenes when using apps such as WordPad Command Line printing where we can specify the port name on the command line.
Some users will use a Port Redirection Monitor (Do NOT use RedMon as unsupported in Win 10, consider Multi Port Redirector)
Other Users will interact with the duplicate named printer directly via PrintUI where you can alter output port, rotation or paper format etc.
And for your usage you could try one of these for hints, with your newly defined PDF printer:-
Microsoft Web Browser ActiveX control - printing to a specified (non default) printer
Programmatically set filename and path in Microsoft Print to PDF printer

Django - suing font awersome in unicode form

I want to use font awersome in my project in the way, that user can choose which icon he wants. I found django-fontawesome-5 but there is one problem - there is no access to icon's unicode and I need it for one javascript component (html doesn't work there, and unicode does). I was looking all over the internet, but I coundn't find anything that would allow me to add font-awersome icons, with their unicodes somhow stored. My question is do you know how to get this feature?
The unicode codes are stored in icons.json and in icons_semantic_ui.json
Since you've got the codes source you can define a custom templatetag or a model/mixin method or a function which just gets a code from one of those json files using icon name
You can see example in fontawesome_5/utils.py

Using GetWindowTextW to get Hebrew text from edit control

I have a VisualStudio project set to "Use Multi-Byte Character Set. It's an older codebase and generally I've been able to get around ok using the "W" version of various functions. However I'm running into a problem trying to get a Hebrew string out of an edit control. No matter what I try I get question marks.
E.g.
int textLen = GetWindowTextLengthW(chatBoxHwnd);
wchar_t* buffw = new wchar_t[textLen + 1];
GetWindowTextW(chatBoxHwnd, buffw, textLen + 1);
But when I try to use buffw (such as displaying it with MessageBoxW) I still get question marks.
So the solution was to also use the Unicode version for creating the edit control itself ( CreateWindowExW vs CreateWindowEX) as well as the ancillary functions around that (SendMessageW, SetWindowLongPtrW, etc...)
After that I was able to successfully use the above code to pull Hebrew (and other languages) from the edit box.
Special thanks to this post How to create a unicode window in a non-unicode application

Strange symbol appears in GtkTreeView

I am writting log viewer with GTK for Windows. I use GtkTreeView widget to display log records. It contains 3 columns: date & time, event source, event text. For any reason, in event time column strange symbol appears:
I used debugger watch window to see string value, and it doesn't contain any extra characters that can result in this strange symbol appearence.
What are possible reasons of such tree view behavior?
All text used in Gtk+-2.0 widgets needs to be in UTF8 encoding.Text in plain ASCII is automatically valid UTF8, but as soon as you have special characters that do not exist in plain ASCII (usually characters that are not used in the English language alphabet), they need to be in UTF8 encoding.Otherwise you may get what you are getting now.
This is something weird but gtk use to have this problem if you do not use glib library functions. You need to handle all of the strings with this functions. This could be a rendering issue.
for paths you use g_path_get_basename(path);
for dirs you use g_path_get_dirname (path);
for date and time use some of this docs gtk date and time
so it can be displayed properly in the treeview

Get a font filename based on Font Name and Style (Bold/Italic)

This has been driving me crazy all day.
I need to get a font filename (eg. Arial.ttf) based on its name (Arial in this case) and whether it is bold, italic or both. Using those pieces of information, I need to find the font file so I can use it for rendering.
Some more examples:
Calibri, Bold would resolve to calibrib.ttf.
Calibri, Italic would resolve to calibrii.ttf.
Any ideas on how I could achieve this in C++ (Win32)
First, to my knowledge, there is no reliable way to do that.
The Windows API deals with font families and mappings, not with font files, which are dealt with at a lower level. Also note that even if you manage to get the file name of a font, no rendering function (that I know of) will accept it, so what will you do with it?
That said, you can look in the registry key HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Fonts in order to obtain the file name of a font from its logical name. An implementation of that solution can be found here.
Related to the earlier posts, this seems to be a reliable way:
1) Read the registered Windows font list from
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Fonts\
You will obtain file names and alternate file paths here.
The Font names are not useful as they can change with user's locale.
2) Load the TrueType files (.ttf, .ttc, .otf):
Use FreeType https://www.freetype.org/). Just initialize the freetype library and load face with FT_New_Face(library, path, 0, &face).
3) Obtain the font Family name using FreeType.
Use FT_Get_Sfnt_Name_Count() and FT_Get_Sfnt_Name() to obtain the string table.
You will need to check if the encoding is Ansi, UTF16 or other, as some strings will be in multiple different languages and encodings.
4) Obtain the OS2 TrueType properties.
Use (TT_OS2 *) FT_Get_Sfnt_Table (face, ft_sfnt_os2) to get the OS2 structure.
Interpret the structure using docs like https://www.microsoft.com/typography/otspec/os2.htm#fc
5) Now you have font file path, family name, style properties and other information. Build a list of these and function to search for a file based on font family and style.
This Code Project project does what you want. As-is it fails on Windows 7 because the GetWinVer function stops at XP. It is trivial to add the case for Windows 7.
You normally do this by calling CreateFontIndirect and then getting the system to render. Perhaps you could explain why you can't use this standard approach.
One solution would be to access the font files and extract the name from the name table to create your own lookup (an STL map would be a simple way of doing that). Details of the TTF file format can be found here.