フォントのリンクを取得し、送信されたフォントの形式で書かれたものの画像を返すことになっているWebアプリケーションを作成しようとしています。
フォントファイルを読み取るストリームを開いて、ファイルのInPtrをPrivateFontCollection.AddMemoryFontに送信しているので、後でグラフィックオブジェクトで使用できます。有効な.ttfファイルを使用するとすべてが正常に機能しますが、.woffフォントファイルを使用しようとすると、AddMemoryFont行に「System.IO.FileNotFoundException:ファイルが見つかりません」というファイルが表示されます。エラーなしで読み取ります。
オンラインコンバーターを使用して.woffファイルを.ttfに変換しようとしましたが、新しい.ttfファイルでコードを実行すると、完全に機能しました。
.woffファイルの使用に問題がありますか、それとも機能させるために何かを変更する必要がありますか?
WebClient client = new WebClient();
Stream stream = client.OpenRead("http://www.drivehq.com/file/df.aspx/shareID2129391/fileID59377155/arial.ttf"); // Works fine.
//Stream stream = client.OpenRead("http://themes.googleusercontent.com/static/fonts/kiteone/v1/VNHoD96LpZ9rGZTwjozAOvesZW2xOQ-xsNqO47m55DA.woff"); // Doesn't work.
PrivateFontCollection fonts;
FontFamily family = LoadFontFamily(stream, out fonts);
public static FontFamily LoadFontFamily(Stream stream, out PrivateFontCollection fontCollection)
{
byte[] buffer = null;
using (MemoryStream ms = new MemoryStream())
{
int count = 0;
do
{
byte[] buf = new byte[1024];
count = stream.Read(buf, 0, 1024);
ms.Write(buf, 0, count);
} while (stream.CanRead && count > 0);
buffer = ms.ToArray();
}
var handle = System.Runtime.InteropServices.GCHandle.Alloc(buffer, System.Runtime.InteropServices.GCHandleType.Pinned);
try
{
var ptr = System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0);
fontCollection = new PrivateFontCollection();
fontCollection.AddMemoryFont(ptr, buffer.Length);
return fontCollection.Families[0];
}
finally
{
handle.Free();
}
}