首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在VS 2010安装项目中安装Type1字体

问在VS 2010安装项目中安装Type1字体
EN

Stack Overflow用户
提问于 2011-09-27 11:12:38
回答 2查看 3.4K关注 0票数 1

我正在尝试使用Visual Studio2010安装项目将一组Type1字体打包到MSI文件中,以便于安装。

我已经将我的安装项目配置为将所有PFM和PFB文件放在Fonts文件夹中,将所有PFM文件设置为vsdrfFont,并修复了此处提到的命名问题:

http://cjwdev.wordpress.com/2011/03/14/installing-non-truetype-fonts-with-visual-studio-installer/

但是,这不适用于Type1字体。

Type1字体文件已安装,但该字体仍无法识别,并且不会出现在Fonts窗口中。

如果手动安装,Type1字体将在HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Type 1 Installer\Type 1 Fonts下注册并正常工作。

如何在安装项目中实现相同的结果?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-10-05 18:03:30

这是一个涉及MSI自定义操作的解决方案。我是用C#写的,但是任何其他能够调用动态链接库的语言都可以是用户。这是C#的教程链接:Walkthrough: Creating a Custom Action

代码语言:javascript
复制
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Runtime.InteropServices;

namespace InstallType1Font
{
    [RunInstaller(true)]
    public partial class Installer1 : Installer
    {
        public Installer1()
        {
            InitializeComponent();
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver);
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);

            // here, you'll have to determine the proper path
            string path = @"c:\Windows\Fonts\MyFont.pfm";
            if (File.Exists(path))
            {
                InstallFontFile(IntPtr.Zero, path, 0);
            }
        }

        [DllImport("fontext.dll", CharSet = CharSet.Auto)]
        private static extern void InstallFontFile(IntPtr hwnd, string filePath, int flags);

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Rollback(IDictionary savedState)
        {
            base.Rollback(savedState);
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);
        }
    }
}

据我所知,InstallFontFile是没有文档的,但允许永久安装字体。使用它的风险自负。

注意:您仍然需要修改.MSI,以确保字体文件具有您给出的链接中所描述的FontTitle。

票数 1
EN

Stack Overflow用户

发布于 2011-11-08 20:11:53

要安装Type 1字体,您需要执行以下操作:

  1. 在'Type 1 fonts‘下注册字体标题
  2. 将PFM和PFB复制到windows字体目录
  3. Call

AddFontResource method

在'Type 1 Fonts'下注册字体标题

SOFTWARE\Microsoft\Windows NT\CurrentVersion\Type 1 Installer\Type 1 Fonts

字体标题是必需的,以下代码片段将允许您从PFM读取字体标题,而不是将其提供给安装程序。它基于从以下来源收集的信息:

http://partners.adobe.com/public/developer/en/font/5178.PFM.pdf

代码语言:javascript
复制
    private static string GetType1FontName(string filename)
    {
        StringBuilder postscriptName = new StringBuilder();

        FileInfo fontFile = new FileInfo(filename);
        using (FileStream fs = fontFile.OpenRead())
        {
            using (StreamReader sr = new StreamReader(fs))
            {
                using (BinaryReader inStream = new BinaryReader(fs))
                {
                    // PFM Header is 117 bytes
                    inStream.ReadBytes(117); // skip 117
                    short size = inStream.ReadInt16();
                    int extMetricsOffset = inStream.ReadInt32();
                    int extentTableOffset = inStream.ReadInt32();

                    inStream.ReadBytes(4); // skip 4
                    int kernPairOffset = inStream.ReadInt32();
                    int kernTrackOffset = inStream.ReadInt32();
                    int driverInfoOffset = inStream.ReadInt32();

                    fs.Position = driverInfoOffset;
                    while (inStream.PeekChar() != 0)
                    {
                        postscriptName.Append(inStream.ReadChar());
                    }
                }
            }
        }

        return postscriptName.ToString();
    }

将PFM和PFB复制到windows字体目录

根据这个博客http://www.atalasoft.com/cs/blogs/stevehawley/archive/2008/08/25/getting-the-fonts-folder.aspx的说法,获取windows字体文件夹的正确方法如下:

代码语言:javascript
复制
    [DllImport("shell32.dll")]
    private static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken,
               uint dwFlags, [Out] StringBuilder pszPath);

    public static string GetFontFolderPath()
    {
        StringBuilder sb = new StringBuilder();
        SHGetFolderPath(IntPtr.Zero, 0x0014, IntPtr.Zero, 0x0000, sb);

        return sb.ToString();
    }

调用AddFontResource方法

最后,应该调用方法AddFontResource,参数lpFilename应该由由管道字符'|‘分隔的pfm和pfb文件组成。在我的例子中,我把完整的路径放到了windows字体文件夹中,这似乎是可行的。在调用AddFontResource之后,您需要使用参数WM.FONTCHANGE (0x001D)调用PostMessage,以便将更改通知其他窗口。

代码语言:javascript
复制
    [DllImport("gdi32.dll")]
    static extern int AddFontResource(string lpFilename);

    // build the name for the api "<pfm>|<pfb>"
    string apiValue = string.Format("{0}|{1}", PfmFileDestination, PfbFileDestination);

    // Call the api to register the font
    int retVal = AddFontResource(apiValue);

    // Inform other windows of change
    PostMessage(HWND_BROADCAST, WM.FONTCHANGE, IntPtr.Zero, IntPtr.Zero);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7563817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档