Unable to embed font using libHaru on Windows - c++

I have following code that perfectly works on Linux:
auto pdf_doc = HPDF_New(nullptr, nullptr);
if (!pdf_doc) { /* error handling */ }
HPDF_SetCompressionMode(pdf_doc, HPDF_COMP_ALL);
HPDF_UseUTFEncodings(pdf_doc);
HPDF_SetCurrentEncoder(pdf_doc, "UTF-8");
const char* font_bold_name = HPDF_LoadTTFontFromFile(pdf_doc, "HelveticaBd.ttf", HPDF_TRUE);
auto font_bold = HPDF_GetFont(pdf_doc, font_bold_name, "UTF-8");
const char* font_regular_name = HPDF_LoadTTFontFromFile(pdf_doc, "Helvetica.ttf", HPDF_TRUE);
auto font_regular = HPDF_GetFont(pdf_doc, font_regular_name, "UTF-8");
But on Windows, call to HPDF_LoadTTFontFromFile() returns "" and error 105D is set in pdf_doc.
This error according to the documentation, means "Font cannot be embedded. (license restriction)".
But the same TTF font files are working on Linux. I also tried the font provided in demo but in that case it sets error 0x1017 in pdf_doc which means "Cannot open a file. (Detailed code is set.)"
Also the call succeeds when I pass HPDF_FALSE(meaning "do not embed the font") and it works. But I need to embed the font :(
Has anybody faced similar issue?
Am I missing something?
Why would license be an issue on Windows when Linux allows it?
Is it related to any font related settings/config on Windows?
Edit:
Error is occurring for Helvetica-bold font. The regular version of Helvetica doesn't cause any error but it is not getting embedded.
Does it have to do anything with the Base-14 fonts?

Related

Wia API - Error on changing transfer file format

I tried to control my Wifi scanner using the Windows Wia API.
I followed the tutorial on:
https://learn.microsoft.com/en-us/windows/win32/wia/-wia-wia-tutorial
And I looked at the Examples:
https://github.com/pauldotknopf/WindowsSDK7-Samples/tree/master/multimedia/wia
I basically copied the code from the datatransfer project:
https://github.com/pauldotknopf/WindowsSDK7-Samples/tree/master/multimedia/wia/datatransfer
And everthing works fine. I can communicate with my scanner and scan a document to a ".BMP" file.
Now I'd like to play around with some settings.I startet with trying to change the file format.
in DataTransfer.cpp:
https://github.com/pauldotknopf/WindowsSDK7-Samples/blob/master/multimedia/wia/datatransfer/DataTransfer.cpp
The scan happens in HRESULT DownloadItem(IWiaItem2* pWiaItem2 , BOOL bTransferFlag).
I thought the file format is specified there:
HRESULT DownloadItem(IWiaItem2* pWiaItem2 , BOOL bTransferFlag
{
...
GUID itemCategory = GUID_NULL;
ReadPropertyGuid(pWiaItem2,WIA_IPA_ITEM_CATEGORY,&itemCategory );
if( (!IsEqualIID(itemCategory,WIA_CATEGORY_FINISHED_FILE)) || (!IsEqualIID(itemCategory,WIA_CATEGORY_FOLDER)) )
{
/* WiaImgFmt_BMP sets the format to ".BMP" */
hr = WritePropertyGuid(pWiaPropertyStorage,WIA_IPA_FORMAT,WiaImgFmt_BMP);
}
if(FAILED(hr))
{
ReportError(TEXT("WritePropertyGuid() failed in DownloadItem().Format couldn't be set to BMP"),hr);
}
...
}
So i tried to change WiaImgFmt_BMP to other formats like WiaImgFmt_JPEG or WiaImgFmt_PNG
but they return the Error: HRESLUT: 0x80070057.
How can I make this work ?
I thought that maybe the scanner doesn't support the other formats, but I don't know how to check that.
When I researched this, I only found reference to the Wia Mini Driver. But I don't quite understand the difference between the API and the driver. If somebody could explain this to me I'd appreciate it.
Windows fax and scan does provide the PNG, JPEG format and I think they also use Wia, so I'd be surprised if the formats aren't supported.
I think it only support BMP and JPEG, you can use IWiaTransfer::EnumWIA_FORMAT_INFO method to know the format it support.

Working with UTF-8 std::string objects in C++

I'm using Visual Studio and C++ on Windows to work with small caps text like ʜᴇʟʟᴏ ꜱᴛᴀᴄᴋᴏᴠᴇʀꜰʟᴏᴡ using e.g. this website. Whenever I read this text from a file or put this text directly into my source code using std::string, the text visualizer in Visual Studio shows it in the wrong encoding, presumably the visualizer uses Windows (ANSI). How can I force Visual Studio to let me work with UTF-8 strings properly?
std::string message_or_file_path = "...";
auto message = message_or_file_path;
// If the file path is valid, read from that file
if (GetFileAttributes(message_or_file_path.c_str()) != INVALID_FILE_ATTRIBUTES
&& GetLastError() != ERROR_FILE_NOT_FOUND)
{
std::ifstream file_stream(message_or_file_path);
std::string text_file_contents((std::istreambuf_iterator<char>(file_stream)),
std::istreambuf_iterator<char>());
message = text_file_contents; // Displayed in wrong encoding
message = "ʜᴇʟʟᴏ ꜱᴛᴀᴄᴋᴏᴠᴇʀꜰʟᴏᴡ"; // Displayed in wrong encoding
std::wstring wide_message = L"ʜᴇʟʟᴏ ꜱᴛᴀᴄᴋᴏᴠᴇʀꜰʟᴏᴡ"; // Displayed in correct encoding
}
I tried the additional command line option /utf-8 for compiling and setting the locale:
std::locale::global(std::locale(""));
std::cout.imbue(std::locale());
Neither of those fixed the encoding issue.
From What’s Wrong with My UTF-8 Strings in Visual Studio?, there are a couple of ways to see the contents of a std::string with UTF-8 encoding.
Let's say you have a variable with the following initialization:
std::string s2 = "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9f\x8d\x8c";
Use a Watch window.
Add the variable to Watch.
In the Watch window, add ,s8 to the variable name to display its contents as UTF-8.
Here's what I see in Visual Studio 2015.
Use the Command Window.
In the Command Window, use ? &s2[0],s8 to display the text as UTF-8.
Here's what I see in Visual Studio 2015.
A working solution was simply rewriting all std::strings as std::wstrings and adjusting the code logic properly to work with std::wstrings, as indicated in the question as well. Now everything works as expected.

Trying to understand process mitigation policies that can be set by SetProcessMitigationPolicy function

Sorry, if it's too broad of a question. I'm trying to see what exactly SetProcessMitigationPolicy function does in Windows 10, but I can't find much about it online (besides my previous forays into this subject.) I'm testing its PROCESS_MITIGATION_POLICY options one-by-one, and I have some questions about these:
ProcessSystemCallDisablePolicy states that it "Disables the ability to use NTUser/GDI functions at the lowest layer.". So I'm testing it as such:
PROCESS_MITIGATION_SYSTEM_CALL_DISABLE_POLICY pmscdp = {0};
pmscdp.DisallowWin32kSystemCalls = 1;
BOOL bR = ::SetProcessMitigationPolicy(ProcessSystemCallDisablePolicy, &pmscdp, sizeof(pmscdp));
int err = ::GetLastError();
::GdiFlush(); //Try to trip it here
But it always fails with error code 19, or ERROR_WRITE_PROTECT.
So what exactly is it supposed to do and how do I set it?
ProcessExtensionPointDisablePolicy states that it "... prevents legacy extension point DLLs from being loaded into the process."
PROCESS_MITIGATION_EXTENSION_POINT_DISABLE_POLICY pmepdp = {0};
pmepdp.DisableExtensionPoints = 1;
BOOL bR = ::SetProcessMitigationPolicy(ProcessExtensionPointDisablePolicy, &pmepdp, sizeof(pmepdp));
int err = ::GetLastError();
Sorry for my naivete, but what is the extension point DLL? And how can I test one?
ProcessSignaturePolicy states that it can "restrict image loading to those images that are either signed by Microsoft, by the Windows Store, or by Microsoft, the Windows Store and the Windows Hardware Quality Labs (WHQL)".
First off, it seems to have no effect on CreateProcess and only works with LoadLibrary-type functions. So if I do this:
PROCESS_MITIGATION_BINARY_SIGNATURE_POLICY pmbsp = {0};
pmbsp.MicrosoftSignedOnly = 1;
//pmbsp.StoreSignedOnly = 1; //always seems to fail with this flag
//pmbsp.MitigationOptIn = 1; //Doesn't seem to have any effect
BOOL bR = ::SetProcessMitigationPolicy(ProcessSignaturePolicy, &pmbsp, sizeof(pmbsp));
BOOL err = ::GetLastError();
And then try to load some of my test DLLs:
HMODULE hModDll = ::LoadLibrary(L".\\Dll1.dll");
The LoadLibrary function fails with the MessageBox that reads:
Bad Image
Dll-Name is either not designed to run on Windows or it
contains an error. Try installing the program again using the original
installation media or contact your system administrator or the
software vendor for support. Error status 0xc0000428.
Interestingly, if I call it on some System32 DLL that is not signed:
HMODULE hModDll = ::LoadLibrary(L"iologmsg.dll");
it seems to work fine. But if I place a copy of my test Dll1.dll into System32 folder and load it this way:
HMODULE hModDll = ::LoadLibrary(L"Dll1_.dll");
it still fails with the same message box:
This is interesting. How can it tell the difference between iologmsg.dll and Dll1_.dll? Both files aren't signed.
PS. And that modal message box can throw in a really nasty wrench into the mix if the app (or the service) does not expect any UI to be shown there.
ProcessFontDisablePolicy lastly, I'm totally lost about this one. It states that it "turns off the ability of the process to load non-system fonts."
So after I enable it in my MFC GUI app:
PROCESS_MITIGATION_FONT_DISABLE_POLICY pmfdp = {0};
pmfdp.DisableNonSystemFonts = 1;
BOOL bR = ::SetProcessMitigationPolicy(ProcessFontDisablePolicy, &pmfdp, sizeof(pmfdp));
int err = ::GetLastError();
the app has a Richedit control that I can load a custom font in. So I went online and downloaded a totally random font. Then installed it in Windows Explorer and tried to use it from the app after that policy has been enabled:
//Set format for the text window
CHARFORMAT cf = { 0 };
cf.cbSize = sizeof(cf);
cf.dwMask = CFM_FACE | CFM_SIZE;
cf.yHeight = 18 * 20;
VERIFY(SUCCEEDED(::StringCchCopy(cf.szFaceName, _countof(cf.szFaceName), L"Action Man")));
VERIFY(SetDefaultCharFormat(cf));
The app was able to display and use that (clearly non-system) font without any issues:
So what am I missing here in that policy?
This is guessing, but since many links in the function's documentation are 404s, I believe that the following would be valid:
1.Probably not implemented, yet.
2.Only a guess (since the link is also 404), but it might refer to DLLs used in obsolete situtations (like the XP and below login DLL, replaced in Vista with Credential Providers).
3.Windows DLLs are treated as signed (without actually having a digital signature attached), not only because they reside in System32, but because Windows keeps internally a map for them. For your DLLs, it won't work. Also, this has no point in CreateProcess() because the new process cannot interact with yours (without your knowledge) and, therefore, cannot hijack it, where a DLL loaded with LoadLibrary can do anything to ruin your process.
4.It probably refers to fonts not installed by Explorer, but fonts added with AddFontResource.

Error loading cr2 with edsdk

I am trying to read cr2 images using canon sdk (canon_edsdk-2.12).
I seem to be loading the dll correctly, but when I try to get the actual image, I get an error.
I tried to run the sample program to see how that is different than mine, but the same thing happens.
Trying to look for the issue on the web, I found the actual source code of the sample: http://read.pudn.com/downloads107/sourcecode/graph/texture_mapping/440409/RAWDevelop/RAWDevelopDlg.cpp__.htm
My error, on the given source, is in the void CRAWDevelopDlg::LoadImage() function -
err = EdsGetImage( m_ImageRef , source , kEdsTargetImageType_RGB , rect , size , DstStreamRef );
if( err == EDS_ERR_OK ) {...}
else
{
AfxMessageBox("The error occurred with the EdsGetImage function.");
}
The above (on line 481 on the page) is the same method that I use, and i get the same error - with error code 35 (instead of 0).
The error seems to be
#define EDS_ERR_FILE_OPEN_ERROR 0x00000023L
So... could there be something wrong with the files ? I experimented with files taken by different versions, including the newest cameras... The files open in Photoshop... And the demo does show header information, as it gives the error. So it can see something.
Am I missing anything ?
All the required dll's used are on the system path...
Thank you.
Old question, still, might help someone:
To open a raw file with the SDK you need to call these functions (you should check for errors, of course):
EdsStreamRef stream = NULL;
EdsImageRef imgRef = NULL;
EdsCreateFileStream("filename", kEdsFile_OpenExisting, kEdsAccess_Read, &stream);
EdsCreateImageRef(stream, &imgRef);
EdsRelease(stream);
Then you can set and get properties with the imgRef.
To save the image as jpg/tiff/RGB image use EdsSaveImage function.

Failed to programmatically uninstall a font in Windows CE

I would like to programmatically uninstall and delete 'simsun.ttc' font collection from '\Hard Disk\Fonts' on WinCE 5. I try to use RemoveFontResource() function but it fails and the last error is (0x2) (The system cannot find the file specified). I definitely know that the file is present.
...
BOOL res = RemoveFontResource(L"\\Hard Disk\\Fonts\\simsun.ttc");
DWORD err = GetLastError(); // 0x2
...
What is the problem?
Did you install the font, or did it come in the OS? If it came in the OS (which is likely) then you probably can't delete it. Why would you want to? Are you trying to save space? If so, get a new OS image without the font in it.