duanjia8215 2013-08-18 20:12
浏览 114

如何阻止Google字体请求本地字体?

I've been trying to use the Ubuntu fonts for my website using the Google Fonts API but I've ran into a problem because of a conflict between the Ubuntu fonts Google has and the local Ubuntu fonts installed on my machine. The end result is that I can't use the Ubuntu fonts at font-weight 400; I am restricted to font-weight 300 and 500. The main site is in font-weight 300 anyway, but I was hoping to use font-weight 400 to give some emphasis to my headings.

I'd like to circumvent this by editing the CSS that Google Fonts uses to remove all references to local fonts. I have tried this manually and it works. The problem is that the CSS Google provides is customized to the browser, so editing the CSS I see and storing that locally will result in the fonts only working for people using the same browser as me.

So, would it be possible to edit the CSS on the fly to remove references to local fonts?

.

I have an idea of a rough solution, but am insufficiently educated to know if it can actually be implemented. Basically, one could have a PHP script that the end user requests through <link rel="css.php">. This PHP file reads the HTTP request of the user including information about the browser, then shoots off a request to Google Fonts API with the exact same HTTP information. Google Fonts provides the CSS our user needs. The rest is string manipulation. Could this work?

  • 写回答

1条回答 默认 最新

  • duangu1878 2013-08-18 20:16
    关注

    You'll have to make the font request manually then. So, you may be requesting http://fonts.googleapis.com/css?family=Ubuntu. Go there (in different browsers - it serves requests per browser) and copy the code. I'll just use what it gives me in Chrome for now, but there'll be different font formats you'll want to include. It says:

    @font-face {
      font-family: 'Ubuntu';
      font-style: normal;
      font-weight: 400;
      src: local('Ubuntu'), url(http://themes.googleusercontent.com/static/fonts/ubuntu/v4/_xyN3apAT_yRRDeqB3sPRg.woff) format('woff');
    }
    

    Copy that it your local CSS, removing the local in the source parameter. Then, just change the font-family name to something like.

    @font-face {
      font-family: 'Webuntu';
      font-style: normal;
      font-weight: 400;
      src: url(http://themes.googleusercontent.com/static/fonts/ubuntu/v4/_xyN3apAT_yRRDeqB3sPRg.woff) format('woff');
    }
    

    Finally, update your font references to use the new name:

    p { font-family:'Webuntu', sans-serif; }
    

    No PHP necessary :)

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图