• java结合Python解析猫眼字体


    公司框架用java开发的,java解析woff字体的文档很少,所以用Python的模块解析字体,嵌入到java中。下面是大体思路:

    1.把字体下载到本地,data url 格式

        猫眼的字体是data url 格式,格式是data:application/font-woff;charset=utf-8;base64,   +base64编码字符串,是把文件用base64编码成字符串写入到网页中,所以只需要用base64解码后边的字符串。Base64包是jdk 1.8新出的

       String     dade="d09GRgABAAAAAAggAAsAAAAAC7gAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAABHU1VCAAABCAAAADMAAABCsP6z7U9TLzIAAAE8AAAARAAAAFZW7lhQY21hcAAAAYAAAAC7AAACTEKqxZNnbHlmAAACPAAAA5MAAAQ0l9+jTWhlYWQAAAXQAAAALwAAADYSQOHcaGhlYQAABgAAAAAcAAAAJAeKAzlobXR4AAAGHAAAABIAAAAwGhwAAGxvY2EAAAYwAAAAGgAAABoGvAW0bWF4cAAABkwAAAAfAAAAIAEZADxuYW1lAAAGbAAAAVcAAAKFkAhoC3Bvc3QAAAfEAAAAWwAAAI/FSeTCeJxjYGRgYOBikGPQYWB0cfMJYeBgYGGAAJAMY05meiJQDMoDyrGAaQ4gZoOIAgCKIwNPAHicY2Bk0mWcwMDKwMHUyXSGgYGhH0IzvmYwYuRgYGBiYGVmwAoC0lxTGBwYKn7cZNb5r8MQw6zDcAUozAiSAwDngwvjeJzFkj0OgzAMhV8KpTQUqWMP0QMyIiFVHKDiDox06soxGBESAz8LUzb6glkqwdo6+iLZTmzLNoAjAIfciQuoNxSsvGhVi92BXuwuHtRvuNLiI6rTpmyTLuzzIR6LSZvMVPPMF/ueLVGMuHWsR+PEXAEOrNKFhzPzQnk7kX4g6n+pv+Wy3M9VC0i0whLrVLBza0qBnUSbCHbeXSjYv30usM8YYsHuxVgIdi8mLXAKMJnAecBUAvwPraxFDAB4nD2Tz3MSZxjH33dxlkiQENllhSiwLNldIAmb/UWAzYIQ0PykJEAIUQwZRUyrphljo2ZaxdYZdfoH2IszHnpxevBuZzr1VO20OfQP6Eyvvd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Wt7QyV/vw5s4u3EglpSz4H4UV4MIAeJxjYGRgYADicov13fH8Nl8ZuFkYQOD6hPNCCPr/GxYGpvNALgcDE0gUADE8CvQAeJxjYGRgYNb5r8MQw8IAAkCSkQEV8AAAM2IBzXicY2EAghQGBiYd4jAAN4wCNQAAAAAAAAAMAFAAigCmAOgBMAFiAagBwgHmAhoAAHicY2BkYGDgYTBgYGYAASYg5gJCBob/YD4DAA6DAVYAeJxlkbtuwkAURMc88gApQomUJoq0TdIQzEOpUDokKCNR0BuzBiO/tF6QSJcPyHflE9Klyyekz2CuG8cr7547M3d9JQO4xjccnJ57vid2cMHqxDWc40G4Tv1JuEF+Fm6ijRfhM+oz4Ra6eBVu4wZvvMFpXLIa40PYQQefwjVc4Uu4Tv1HuEH+FW7i1mkKn6Hj3Am3sHC6wm08Ou8tpSZGe1av1PKggjSxPd8zJtSGTuinyVGa6/Uu8kxZludCmzxMEzV0B6U004k25W35fj2yNlCBSWM1paujKFWZSbfat+7G2mzc7weiu34aczzFNYGBhgfLfcV6iQP3ACkSaj349AxXSN9IT0j16JepOb01doiKbNWt1ovippz6sVYYwsXgX2rGVFIkq7Pl2PNrI6qW6eOshj0xaSq9mpNEZIWs8LZUfOouNkVXxp/d5woqebeYIf4D2J1ywQB4nG3IOw6AIBCE4R3fIt4FJDFLyUPuYmNn4vGNS+s0X+anhuoU/U+jQYsOPQaMmDBDYYHGSnjG+zqPGLy4sxNztGJi81k2E+S7JL1w9lW/S7fMYjCZ6AUhZRemAA==";
            final Base64.Decoder decoder = Base64.getDecoder();
            try {
       //System.out.println(new String(decoder.decode(dade), "UTF-8"));
              byte bt[] = new byte[1024]; 
        bt =decoder.decode(dade);
        FileOutputStream in = new FileOutputStream("C:\Users\user\Desktop\maoyan.woff"); 
        in.write(bt, 0, bt.length); 
         in.close(); 
      } catch (UnsupportedEncodingException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            }

    2.Python 解析字体(py文件)

    from fontTools.ttLib import TTFont
    import sys
    import requests
    #maoyan=sys.argv[1]
    baseFont = TTFont('C:\Users\user\Desktop\base.woff')
    maoyanFont = TTFont('C:\Users\user\Desktop\maoyan.woff')
    uniList = maoyanFont['cmap'].tables[0].ttFont.getGlyphOrder()
    numList = []
    baseNumList = ['.', '3', '5', '1', '2', '7', '0', '6', '9', '8', '4']
    baseUniCode = ['x', 'uniEF2A', 'uniE345', 'uniE175', 'uniEE86', 'uniE5D2', 'uniF7D8',
    'uniF06F', 'uniE252', 'uniF3FE', 'uniE806']
    for i in range(1, 12):
    maoyanGlyph = maoyanFont['glyf'][uniList[i]]
    for j in range(11):
    baseGlyph = baseFont['glyf'][baseUniCode[j]]
    if maoyanGlyph == baseGlyph:
    numList.append(baseNumList[j]+':'+uniList[i])
    break
    uniList[1] = 'uni0078'
    print(numList)

    3.java中嵌入Python,网上有很多方法,我使用的方法需要在Python中print出结果,java代码以输出流的方式得到打印的结果。传参的话需要在Python代码中接收参数。

    //可以在后边加参数 如: String[] argss = new String[] { "D:\python\python.exe", "C:\Users\user\Desktop\test.py","arg1","arg2"};
            String[] argss = new String[] { "D:\python\python.exe", "C:\Users\user\Desktop\test.py"};
            Process pr;
            //需要print出来java接受
      try {
       pr = Runtime.getRuntime().exec(argss);
        BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
              String line;
              String result = "";
              while ((line = in.readLine()) != null) {
                result += line;
                System.out.println(result);
              }
              in.close();
              try {
         pr.waitFor();
        } catch (InterruptedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }

    4.以上方法是对两个字体文件进行字形名称进行比较。58页面的字体是没有字形名称的,通过python的 baseFont = TTFont('D:pythonworkspace\58.ttf'),研究发现页面中展示的是十进制转十六进制的一种编码。

     https://www.jianshu.com/p/5a626b3422ac

    
    
    
    
  • 相关阅读:
    设置页面标题title
    路由跳转,页面位置不在顶部
    PyTorch深度学习入门
    删除ubuntu后开机进入grub的解决办法
    研一上英语
    python中常用的几个函数的正确用法-lambda/filter/map/reduce
    【Python教程】 re 模块中findall() 函数返回值展现方式的用法详解
    【Python教程】5种常见字符串去除空格的操作方法
    对python中浅拷贝和深拷贝分析详细介绍
    Git使用
  • 原文地址:https://www.cnblogs.com/srp750115867/p/9451092.html
Copyright © 2020-2023  润新知