Forgot password?
 Create new account
Author: abababa

请教能不能单独对某些字用别的字体?

[Copy link]

418

Threads

1628

Posts

110K

Credits

Credits
11891

Show all posts

 Author| abababa Posted at 2023-8-30 18:24:36
hbghlyj 发表于 2023-8-30 17:23
可以找一个“山”偏旁的字和“餐”偏旁的字,如7#在Windows中用Private Character Editor剪贴到一起,就可 ...
这种拼接的,我在latex里试过了,如下,这个字我也一直没找到编码:
\hbox{\hbox{\scalebox{0.65}[1.0]{钅}}\hbox{\kern-0.25em \scalebox{0.75}[1.0]{坒}}}
原文是“旌*杰气”,那个*就是指上面拼接的那个字。

418

Threads

1628

Posts

110K

Credits

Credits
11891

Show all posts

 Author| abababa Posted at 2023-8-30 18:28:10
kuing 发表于 2023-8-30 17:38
当时你是为了啥而看那个PDF呢?能想起来吗
还有你还记得当时那字体是宋体还是别的字体?有衬线还是无衬线 ...
有个人名里带“㲿”字,当时电脑里没有这个字,其实用繁体的“瀇”也能代替,但人家就叫这个名,也不能改,后来就查这些和汉字有关的资料,才知道有unicode这些,然后下载了一个pdf,发现里面的字就是我说的那种排列方式的,我看着像宋体,有衬线,那个竖弯勾都很明显是像挑上去的写法。

3147

Threads

8493

Posts

610K

Credits

Credits
66163
QQ

Show all posts

hbghlyj Posted at 2023-8-30 19:09:28
kuing 发表于 2023-8-30 19:02
只含一个字的PDF为什么会800k这么大呢?
因为把1.12MB的字体文件嵌入了

701

Threads

110K

Posts

910K

Credits

Credits
94177
QQ

Show all posts

kuing Posted at 2023-8-30 19:13:08
hbghlyj 发表于 2023-8-30 19:09
因为把1.12MB的字体文件嵌入了
不能子集化吗?

3147

Threads

8493

Posts

610K

Credits

Credits
66163
QQ

Show all posts

hbghlyj Posted at 2023-8-30 19:26:19
kuing 发表于 2023-8-30 19:13
不能子集化吗?
  1. import fitz
  2. font = fitz.Font(fontfile="BBEGEF+UCS_G_ExtGv1.ttf")
  3. print(font.glyph_count)
Copy the Code

有2365个字形。应该可以子集化

3147

Threads

8493

Posts

610K

Credits

Credits
66163
QQ

Show all posts

hbghlyj Posted at 2023-8-31 00:05:41

从命令行使用 Fonttools 的 pyftsubset

Last edited by hbghlyj at 2023-8-31 00:23:00
kuing 发表于 2023-8-30 19:13
不能子集化吗?
https://fonttools.readthedocs.io/en/latest/subset/index.html
  1. pyftsubset "BBEGEF+UCS_G_ExtGv1.ttf" --gids=2004
Copy the Code

报错KeyError: 'post'fontTools\subset\__init__.py中出错的行注释掉即可
又报错KeyError: 'cmap'fontTools\subset\__init__.py中出错的行注释掉即可
字体文件由1.12MB缩减为1.92KB
$type BBEGEF UCS_G_ExtGv1.subset.ttf (1.93 KB, Downloads: 10)
  1. from fontTools import ttLib
  2. tt = ttLib.TTFont("BBEGEF+UCS_G_ExtGv1.subset.ttf")
  3. tt['maxp'].numGlyphs
Copy the Code
输出2(只有2个字形)
  1. tt.getGlyphOrder()
Copy the Code
输出['.notdef', 'glyph00001']

3147

Threads

8493

Posts

610K

Credits

Credits
66163
QQ

Show all posts

hbghlyj Posted at 2023-8-31 00:18:18
kuing 发表于 2023-8-30 19:13
不能子集化吗?
将20#的代码略作调整:
  1. import fitz
  2. doc = fitz.open()
  3. page = doc.new_page(width=612,height=792)
  4. page.insert_font(fontname="TT9", fontfile="BBEGEF+UCS_G_ExtGv1.subset.ttf")
  5. page.insert_text((0,0),"abababa",fontname="TT9",fontsize = 1)
  6. doc.update_stream(page.get_contents()[0],b"/TT9 1 Tf\n21 0 0 21 111.24 691.62 Tm\n<0001>Tj")
  7. doc.save("cjktest.pdf")
Copy the Code

得到3.19KB的PDF
$type cjktest.pdf (3.19 KB, Downloads: 12)
再用Adobe在线压缩得到2.88KB的PDF
$type cjktest-compressed.pdf (2.88 KB, Downloads: 12)

701

Threads

110K

Posts

910K

Credits

Credits
94177
QQ

Show all posts

kuing Posted at 2023-8-31 02:19:35
kuing 发表于 2023-8-30 15:23
...
...
或者你也可以去找找有没有黑体的生僻字字体,但我还没见过。
我找到一个包含扩展 G 区的黑体字体:
github.com/Fitzgerald-Porthmouth-Koenigsegg/Plangothic-Project

下载 releases 里面的 PlangothicP2-Regular.ttf

3147

Threads

8493

Posts

610K

Credits

Credits
66163
QQ

Show all posts

hbghlyj Posted at 2023-8-31 03:52:13
Last edited by hbghlyj at 2023-8-31 13:26:00
kuing 发表于 2023-8-30 19:13
不能子集化吗?

在ghostscript中用-dSubsetFonts=true子集化字体

假设字体在C:/Users/lenovo/Downloads/PlangothicP2-Regular.ttf
  1. %!PS-Adobe-3.0
  2. %%DocumentMedia: Letter 612 792 0 () ()
  3. %%BoundingBox: 0 0 612 792
  4. %%Pages: 1
  5. (C:/Users/lenovo/Downloads/PlangothicP2-Regular.ttf) 21 selectfont
  6. 111.24 691.62 moveto
  7. /u303C0 glyphshow
  8. showpage
Copy the Code

将上面保存为test.ps,执行
  1. ghostscript -sDEVICE=pdfwrite -dSubsetFonts=true -dEmbedAllFonts=true -o test.pdf test.ps
Copy the Code

(我的电脑上MiKTeX的ghostscript的路径是D:\MiKTeX\miktex\bin\x64\mgs.exe
得到131KB的PDF
$type test.pdf (131.34 KB, Downloads: 11)

3147

Threads

8493

Posts

610K

Credits

Credits
66163
QQ

Show all posts

hbghlyj Posted at 2023-8-31 04:03:54
分析29#的test.pdf
  1. import fitz
  2. doc = fitz.open("test.pdf")
  3. page = doc[0]
  4. for xref in page.get_contents():
  5.     stream = doc.xref_stream(xref).decode()
  6.     print(stream)
Copy the Code

输出
  1. q 0.1 0 0 0.1 0 0 cm
  2. /R7 gs
  3. 0 g
  4. q
  5. 10 0 0 10 0 0 cm BT
  6. /R8 21 Tf
  7. 1 0 0 1 111.24 691.62 Tm
  8. ()Tj
  9. ET
  10. Q
  11. Q
Copy the Code

其中gs是graphics state operator

3147

Threads

8493

Posts

610K

Credits

Credits
66163
QQ

Show all posts

hbghlyj Posted at 2023-8-31 04:15:15
Last edited by hbghlyj at 2023-8-31 04:30:00
仿照26#可以从命令行子集化字体:
  1. pyftsubset PlangothicP2-Regular.ttf u303C0
Copy the Code

$type PlangothicP2-Regular.subset.ttf (2.34 KB, Downloads: 9)
由8205KB减小到3KB
  1. from fontTools import ttLib
  2. tt = ttLib.TTFont("C:/Users/lenovo/Downloads/PlangothicP2-Regular.subset.ttf")
  3. tt.getGlyphOrder()
Copy the Code
输出['.notdef', 'u303C0']
只含这2个字形(其中.notdef是fallback character)

701

Threads

110K

Posts

910K

Credits

Credits
94177
QQ

Show all posts

kuing Posted at 2023-8-31 14:20:42
hbghlyj 发表于 2023-8-31 04:15
仿照26#可以从命令行子集化字体:

由8205KB减小到3KB
我想要的子集化并不是把 .ttf 子集化,而是对 PDF 里面嵌入的字体子集化。

418

Threads

1628

Posts

110K

Credits

Credits
11891

Show all posts

 Author| abababa Posted at 2023-8-31 17:48:46
kuing 发表于 2023-8-31 02:19
我找到一个包含扩展 G 区的黑体字体:
https://github.com/Fitzgerald-Porthmouth-Koenigsegg/Plangothic ...
这个是思源的那套字体修改的,好像是修复了一些字形。
如果设置默认字体为思源字体:\setCJKmainfont{Noto Sans CJK SC},然后再对个别生僻字用\newCJKfontfamily\spz{Plangothic P2},字形就应该一致了。

Comment

嗯嗯😊  Posted at 2023-8-31 18:47

418

Threads

1628

Posts

110K

Credits

Credits
11891

Show all posts

 Author| abababa Posted at 2023-9-11 19:11:21
abababa 发表于 2023-8-31 17:48
这个是思源的那套字体修改的,好像是修复了一些字形。
如果设置默认字体为思源字体:\setCJKmainfont{Not ...
我突然想到,这种方法得对每个生僻字都加命令,那能不能在导言区单独弄一个什么命令,然后对默认字体不能显示的那些字,全部都用另一种字体,这样就方便了。

418

Threads

1628

Posts

110K

Credits

Credits
11891

Show all posts

 Author| abababa Posted at 2023-9-14 14:45:39
abababa 发表于 2023-9-11 19:11
我突然想到,这种方法得对每个生僻字都加命令,那能不能在导言区单独弄一个什么命令,然后对默认字体不能 ...
找到方法了,设置一个退回字体,当在主字体里找不到的时候,就自动使用退回字体了:
  1. \xeCJKsetup{AutoFallBack=true}
  2. \setCJKmainfont{Noto Sans CJK SC}[FallBack=Plangothic P2]
Copy the Code


这样就可以直接打那些生僻字了。

Comment

噢,原来 xeCJK 也有 fallback 机制😃  Posted at 2023-9-14 14:49

418

Threads

1628

Posts

110K

Credits

Credits
11891

Show all posts

 Author| abababa Posted at 2023-9-14 15:43:57
abababa 发表于 2023-9-14 14:45
找到方法了,设置一个退回字体,当在主字体里找不到的时候,就自动使用退回字体了:
这个好像不能设置多个,像下面这样就不行:
  1. \setCJKmainfont{Noto Sans CJK SC}[FallBack=Plangothic P1,Plangothic P2]
Copy the Code


设置多个,用下面这种:
  1. \setCJKmainfont{Noto Sans CJK SC}
  2. \setCJKfallbackfamilyfont{\CJKrmdefault}{ {Plangothic P1}, {Plangothic P2} }
Copy the Code

Comment

嗯,P1 P2 这样应该完备了  Posted at 2023-9-14 15:57

701

Threads

110K

Posts

910K

Credits

Credits
94177
QQ

Show all posts

kuing Posted at 2023-9-14 16:01:03
xecjk fallback 相关帖子:ask.latexstudio.net/ask/question/3571.html

手机版Mobile version|Leisure Math Forum

2025-4-20 22:23 GMT+8

Powered by Discuz!

× Quick Reply To Top Return to the list