Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Fonts inside WebBrowser control show up only when being "helped" from outside

Currently, I'm seeing a strange behavior in my .NET 2.0 WinForms application.

Hosting a WebBrowser control that pulls data from its own app-built-in web server (this one), everything works fine, until it comes to Google Web Fonts.

Steps:

Under Windows 7, the following behavior occurs:

1.) Start the application. The page does not display the Google Web Fonts:

enter image description here

2.) Open the URL of the built-in web server inside a stand-alone instance of Internet Explorer:

enter image description here

3.) Go back to the C# application, hit F5:

enter image description here

Now, the font is visible inside my application, too.

Behavior:

It seems that the stand-alone IE does additional things, that the hosted IE inside my application is not allowed to do.

Once the URL was opened inside the stand-alone IE, I can close both, my application and IE and restart my application and still get the correct behavior.

When clearing IE data (cache, cookies, etc.) the steps 1-3 are required again to get the Google Web Font into the hosted web browser.

My assumption:

I am guessing that this has something to do with permissions that IE seems to require to "install" the web font in its locale cache. I've just added a Access-Control-Allow-Origin:* to my request header, but this does not seem to improve anything.

My question:

Do you have any hints on how to make Google Web Fonts working in my scenario, without the need to fire up the stand-alone IE?

Update 2013-08-22 - SOLUTION:

Based on Adam's suggestion, I changed the user agent of the WebBrowser control and this worked. Now I do have web fonts inside my WinForms application with a hosted browser.

The code looks something like:

public void ChangeUserAgent()
{
    // https://stackoverflow.com/a/12648705/107625
    const string ua = @"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)";

    // https://stackoverflow.com/q/937573/107625
    UrlMkSetSessionOption(UrlmonOptionUseragent, ua, ua.Length, 0);
}

With those P/Invoke helper functions:

[DllImport(@"urlmon.dll", CharSet = CharSet.Ansi)]
private static extern int UrlMkSetSessionOption(
    int dwOption, 
    string pBuffer, 
    int dwBufferLength, 
    int dwReserved);

private const int UrlmonOptionUseragent = 0x10000001;
like image 534
Uwe Keim Avatar asked Nov 03 '22 21:11

Uwe Keim


1 Answers

Have you tried downloading the fonts and running them from your web server, instead of from Google's? You should be able to download the CSS file and WOFF/EOT file's from Google's web servers and place them locally on your web server. You will need to change Google's CSS file to point to your local WOFF files, after that.

I know this doesn't really fix the strange behavior of the WebBrowser control, but it may serve as a work-around.

Edit: It seems like the CSS is generated dynamically per browser. I get different CSS for Chrome than I do IE. This might still work for you though, since it seems it's running in a controlled environment through the WebBrowser control so it should always be IE.

like image 169
Adam Plocher Avatar answered Nov 14 '22 00:11

Adam Plocher