Debian Codegraph @

texk/web2c/mfluadir/mfluatrap/mflua_ttx_backend.lua

raw symbols SCIP
1 local format = string.format
2 local table_insert = table.insert
3 local table_remove = table.remove
4 local table_concat = table.concat
5 local table_sort = table.sort
6 local print_int = mflua.MF.print_int
7 local abs = math.abs
8 local math_floor = math.floor
9 local math_tointeger = math.tointeger or math.floor
10
11 local print_scaled = mflua.MF.print_scaled
12 local unity = mflua.MF.unity
13
14 local GF = mflua.GF
15 local GF_chars = GF.chars
16
17 local ttx = {}
18 local xmlstream = {}
19 local scrt = {} -- general table for local use
20
21
22
23
24 local function DUMP(t)
25 if type(t) == 'table' then
26 print("table",#table)
27 for k,v in pairs(t) do print(k,v) end
28 end
29 end
30
31 local function print_err(msg)
32 print(format("! ttx backend: %s",tostring(msg)))
33 end
34
35 local function tointeger(n)
36 return math_tointeger(n)
37 end
38
39 local function _normalize(w,n)
40 n = tonumber(n) or 5
41 n = math_floor(abs(n))
42 n = (n<25 and n) or 24
43 local fmt = format("%%0.%sf", n)
44 local W = tonumber(format(fmt,w))
45 nx = tointeger(W)
46 if nx==W then
47 return nx
48 else
49 return w
50 end
51 end
52
53 local function normalize(w,n)
54 return print_scaled(w)
55 end
56
57 local function _normalize_and_simplify(w,n)
58 n = tonumber(n) or 2
59 n = math_floor(abs(n))
60 n = (n<25 and n) or 24
61 local fmt = format("%%0.%sf", n)
62 local W = tonumber(format(fmt,w))
63 nx = tointeger(W)
64 if nx==W then
65 return nx
66 else
67 return W
68 end
69 end
70
71 local function normalize_and_simplify(w,n)
72 return print_scaled(w)
73 end
74
75
76 local function toscaled(n)
77 return tonumber(format("%0.0f",n*unity))
78 end
79
80 local function table_sorted(t)
81 if type(t)=='table' then
82 table_sort(t)
83 return t
84 else
85 return {t}
86 end
87 end
88
89
90 local xml_comment = 'c'
91 local xml_processing_instruction = 'p'
92 local xml_element = 'e'
93 local xml_header = 'h'
94
95
96 --------------------------------------------------------------------------------
97 --
98 -- Errors and warning
99 --------------------------------------------------------------------------------
100 local function warning(t,msg)
101 print("Warning:"..msg..".")
102 print("Element:")
103 if type(t)=='table' then
104 for k,v in pairs(t) do print(k,v) end
105 print(#t.." values")
106 else
107 print(tostring(s))
108 end
109 end
110
111
112 --------------------------------------------------------------------------------
113 --
114 -- Element management
115 --------------------------------------------------------------------------------
116
117
118 -- Todo: valid XML totext & toattr & tocomment
119 local function totext(s)
120 if not(type(s)=='string') then
121 warning(s,"text:the data in not a string")
122 end
123 return tostring(s)
124 end
125
126 local function toattr(s)
127 if not(type(s)=='string') then
128 warning(s,"attr.:the data in not a string")
129 end
130 return tostring(s)
131 end
132
133 local function tocomment(s)
134 if not(type(s)=='string') then
135 warning(s,"comment:the data in not a string")
136 end
137 return tostring(s)
138 end
139
140 local function new_comment(text)
141 local t ={}
142 t[0]=xml_comment
143 t[1]=false
144 t[2]=tocomment(text)
145 return t
146 end
147
148 local function new_element(name,attr)
149 local t ={}
150 t[0]=xml_element
151 t[1]=tostring(name)
152 if attr and type(attr)=='table' then
153 for k,v in pairs(attr) do
154 if type(k)~='number' then
155 t[k]=v
156 end
157 end
158 end
159 return t
160 end
161
162 local function get_elementname(t)
163 if t and type(t)=='table' then
164 return t[1]
165 else
166 return nil
167 end
168 end
169
170 local function insert_child(f,c)
171 table_insert(f,c)
172 end
173
174 local function insert_child_at_beginning(f,c)
175 -- f[1] is always the name
176 table_insert(f,2,c)
177 end
178
179 local function insert_children(f,cc)
180 for j=1,#cc do
181 table_insert(f,cc[j])
182 end
183 end
184
185
186 --
187 -- get all the children (not descendants!)
188 -- of source that match target name and attr.
189 -- The existence of an attribute can be checked
190 -- with @attrname=true (not @attrname="true") in
191 -- target
192 local function get_children(source,target)
193 local res={}
194 local t_attr={}
195 -- only key/val from target
196 for k,v in pairs(target) do
197 if type(k)=='number' then
198 t_attr[k]=v
199 end
200 end
201 for i=2,#source do
202 local e = source[i]
203 if e[1]==target[1] then
204 local found=true
205 for k,v in pairs(t_attr) do
206 if not(e[k] and ((e[k]==v) or (v==true))) then
207 found=false
208 break
209 end
210 end
211 if found then
212 res[#res+1]=e
213 end
214 end
215 end
216 return res
217 end
218
219 --
220 -- remove the first xml_element inserted
221 -- with name. Return the element removed
222 -- false otherwise
223 local function remove_child(t,name)
224 for i=1,#t do
225 if t[i][0]==xml_element and t[i][1]==tostring(name) then
226 return table.remove(t,i)
227 end
228 end
229 return false
230 end
231
232 --
233 -- call remove_child on all elements of
234 -- names
235
236 local function remove_children(t,names)
237 local res = {}
238 local name, r
239 for _,v in pairs(names) do
240 name = tostring(v)
241 r = remove_child(t,name)
242 if r then
243 table_insert(res,r)
244 end
245 end
246 return res
247 end
248
249
250
251 --------------------------------------------------------------------------------
252 --
253 -- Serialization
254 ---------------------------------------------------------------------------------
255
256 local function serialize_comment(t)
257 if not(t[0]== xml_comment) then
258 warning(t,'the node to serialize is not a comment')
259 return
260 end
261 local name=t[1] -- false, discarded
262 local elem={}
263 elem[#elem+1]='<--'
264 elem[#elem+1]=tocomment(t2)
265 elem[#elem+1]='-->'
266 elem[#elem+1]='\n'
267 xmlstream[#xmlstream+1] = table_concat(elem)
268 end
269
270
271 local function serialize_processing_instruction(t)
272 if not(t[0]== xml_processing_instruction) then
273 warning(t,'the node to serialize is not a processing_instruction')
274 return
275 end
276 local name=t[1]
277 local elem={}
278 elem[#elem+1]='<?'
279 elem[#elem+1]=name
280 local _t={}
281 for k,v in pairs(t) do
282 if not(type(k)=='number') then
283 _t[#_t+1]=k
284 end
285 end
286 for j=1,#_t do
287 elem[#elem+1]=format([[ %s="%s"]],_t[j],toattr(t[_t[j]]))
288 end
289 elem[#elem+1]='?>'
290 elem[#elem+1]="\n"
291 xmlstream[#xmlstream+1] = table_concat(elem)
292 end
293
294 local function serialize_xml_header(t)
295 local _t={}
296 local elem={}
297 elem[#elem+1]='<?'
298 elem[#elem+1]='xml '
299 elem[#elem+1]=t
300 elem[#elem+1]='?>'
301 elem[#elem+1]='\n'
302 xmlstream[#xmlstream+1] = table_concat(elem)
303 end
304
305 local function serialize_empty_elem(t)
306 local name=t[1]
307 local elem={}
308 elem[#elem+1]='<'
309 elem[#elem+1]=name
310 local _t={}
311 for k,v in pairs(t) do
312 if not(type(k)=='number') then
313 _t[#_t+1]=k
314 end
315 end
316 table.sort(_t)
317 for j=1,#_t do
318 elem[#elem+1]=format([[ %s="%s"]],_t[j],toattr(t[_t[j]]))
319 end
320 elem[#elem+1]='/>'
321 elem[#elem+1]="\n"
322 xmlstream[#xmlstream+1] = table_concat(elem)
323 end
324
325 local function serialize_elem(t)
326 if #t==1 then
327 serialize_empty_elem(t)
328 else
329 local name=t[1]
330 local elem={}
331 elem[#elem+1]='<'
332 elem[#elem+1]=name
333 -- attributes
334 local _t={}
335 for k,v in pairs(t) do
336 if not(type(k)=='number') then
337 _t[#_t+1]=k
338 end
339 end
340 table.sort(_t)
341 for j=1,#_t do
342 elem[#elem+1]=format([[ %s="%s"]],_t[j],toattr(t[_t[j]])) --
343 end
344 elem[#elem+1]='>'
345 if type(t[2])~= 'string' then
346 elem[#elem+1]="\n"
347 end
348 xmlstream[#xmlstream+1] = table_concat(elem)
349 elem = {}
350 for j=2,#t do
351 if type(t[j])=='table' then
352 serialize_elem(t[j])
353 elseif type(t[j])=='string' then
354 xmlstream[#xmlstream+1] = totext(t[j])
355 end
356 end
357 elem[#elem+1]='</'
358 elem[#elem+1]=name
359 elem[#elem+1]='>'
360 elem[#elem+1]="\n"
361 xmlstream[#xmlstream+1] = table_concat(elem)
362 end
363 end
364
365
366 --------------------------------------------------------------------------------
367 --
368 -- First children of XML ttx, plus utilities
369 --------------------------------------------------------------------------------
370
371 local ttFont
372 local GlyphOrder
373 local GlyphID
374 local head
375 local hhea
376 local maxp
377 local OS_2,panose -- handy to have
378 local name
379 local cmap
380 local post
381 local CFF, CFFFont, Private -- only a single CFFFont, for the moment
382 local FFTM
383 local GDEF
384 local hmtx
385
386 -- For variable fonts
387 local extranames -- handy to have
388 local CFF2
389 local HVAR,VarStore,VarRegionList,Regions,VarRegionAxis,VarData,AdvWidthMap
390 local fvar
391
392
393 -- Important, child of CFFFont
394 local CharStrings
395
396 local function get_GlyphID(id,name)
397 return new_element('GlyphID',{['id']=tostring(id),['name']=tostring(name)})
398 end
399
400 local function put_head(name,value)
401 table_insert(head, new_element(name, {['value']=tostring(value)}))
402 end
403
404 local function put_hhea(name,value)
405 table_insert(hhea, new_element(name, {['value']=tostring(value)}))
406 end
407
408 local function put_maxp(name,value)
409 table_insert(maxp,new_element(name, {['value']=tostring(value)}))
410 end
411
412 local function put_OS_2(name,value)
413 table_insert(OS_2,new_element(name, {['value']=tostring(value)}))
414 end
415 -- handy
416 local function put_panose(name,value)
417 table_insert(panose,new_element(name, {['value']=tostring(value)}))
418 end
419
420 local function get_map(code,name)
421 return new_element('map', {['code']=tostring(code),['name']=tostring(name)})
422 end
423
424 local function put_post(name,value)
425 table_insert(post,new_element(name, {['value']=tostring(value)}))
426 end
427
428 local function put_CFF(name,value)
429 table_insert(CFF,new_element(name, {['value']=tostring(value)}))
430 end
431
432 local function put_CFF2(name,value)
433 table_insert(CFF2,new_element(name, {['value']=tostring(value)}))
434 end
435
436
437 local function put_CFFFont(name,value)
438 table_insert(CFFFont,new_element(name, {['value']=tostring(value)}))
439 end
440
441 local function put_Private(name,value)
442 table_insert(Private,new_element(name, {['value']=tostring(value)}))
443 end
444
445 local function put_FFTM(name,value)
446 table_insert(FFTM,new_element(name, {['value']=tostring(value)}))
447 end
448
449 local function get_ClassDef(glyph,class)
450 return new_element('ClassDef', {['glyph']=tostring(glyph),['class']=tostring(class)})
451 end
452
453 local function get_mtx(name,width,lsb)
454 return new_element('mtx', {['name']=tostring(name),['width']=tostring(width),['lsb']=tostring(lsb)})
455 end
456
457 --
458 -- convenient method for a cmap0
459 --
460 -- local function get_cmap0_maps(maps)
461 -- local t = {}
462 -- if not(#maps==256) then
463 -- warning(maps,"maps has not 256 values")
464 -- end
465 -- local s=(#maps>=256 and 256) or #maps
466 -- for j=1,s do
467 -- t[#t+1]=get_map(format("0x%x",j-1),maps[j])
468 -- end
469 -- return t
470 -- end
471
472 local function get_raw_cmap0()
473 local t = {}
474 for j=0,255 do
475 t[format("0x%02x",j)] = ".notdef"
476 end
477 return t
478 end
479
480
481
482
483 --------------------------------------------------------------------------------
484 --
485 -- Create & save xml font
486 --------------------------------------------------------------------------------
487
488 local function checkall(tfm)
489 -- several checks & fixes here
490 local gf_chars = {}
491 if GF then
492 for k,_ in pairs(GF.chars) do
493 if type(k)=='number' then
494 table_insert(gf_chars,k)
495 end
496 end
497 table_sort(gf_chars)
498 end
499 --
500 -- fix head
501 if ttx.userdata.update_head and
502 type(ttx.userdata.update_head)=='function' then
503 ttx.userdata.update_head(tfm)
504 elseif GF then
505 remove_children(head,{'xMin', 'xMax','yMin','yMax','modified'})
506 put_head('xMin',GF_chars.min_m);
507 put_head('yMin',GF_chars.min_n);
508 put_head('xMax',GF_chars.max_m-GF_chars.min_m+1);
509 put_head('yMax',GF_chars.max_n-GF_chars.min_n+1);
510 -- Date is UTC
511 put_head('modified',os.date("!%c"));
512 end
513 --
514 -- fix CFFFont, only one for the moment
515 if ttx.userdata.update_CFFFont and
516 type(ttx.userdata.update_CFFFont)=='function' then
517 ttx.userdata.update_CFFFont(CFFont)
518 elseif GF then
519 remove_child(CFFFont,'FontBBox')
520 put_CFFFont('FontBBox',
521 format("%s %s %s %s",GF_chars.min_m,GF_chars.min_n,
522 GF_chars.max_m-GF_chars.min_m+1,
523 GF_chars.max_n-GF_chars.min_n+1))
524 end
525 --
526 -- fix OS_2
527 if ttx.userdata.update_OS_2 and
528 type(ttx.userdata.update_OS_2)=='function' then
529 ttx.userdata.update_CFFFont(OS_2)
530 elseif mflua.chartable then
531 local design_size=tonumber ( print_scaled(MFbuiltin.designsize()) ) --pt
532 local emsize = ttx.emsize or 1000
533 local avgx = 0
534 local avgx_cnt=0
535 for k,v in pairs(mflua.chartable) do
536 if tonumber(v.char_wd) > 0 then
537 avgx = tonumber(v.char_wd)*emsize/design_size+avgx
538 avgx_cnt=avgx_cnt+1
539 end
540 end
541 avgx = math_floor(avgx/avgx_cnt+0.5)
542 remove_child(OS_2,'xAvgCharWidth')
543 put_OS_2('xAvgCharWidth',avgx)
544 end
545 -- fix cmap0
546 -- local t = {}
547 -- for k,_ in pairs(scrt.cmap.rawcmap0) do t[#t+1]=k end
548 -- table.sort(t)
549 -- for k=1,256 do
550 -- insert_child(scrt.cmap.cmap0, get_map(t[k],scrt.cmap.rawcmap0[t[k]]))
551 -- end
552
553 end
554
555 local function savettxfont(t)
556 serialize_xml_header([[version="1.0" encoding="UTF-8"]])
557 serialize_elem(t)
558 return table_concat(xmlstream)
559
560 end
561
562 local function makefont(tfm)
563 --
564 checkall(tfm)
565 serialize_xml_header([[version="1.0" encoding="UTF-8"]])
566 serialize_elem(ttFont)
567 return table_concat(xmlstream)
568 end
569
570
571
572
573 --------------------------------------------------------------------------------
574 --
575 -- mflua layer
576 --------------------------------------------------------------------------------
577
578 local function _eval_tonumber(q,offset)
579 local qx,qy,xo,yo
580 local w
581 local _offset = offset
582 if _offset == nil then _offset = '(0,0)' end
583 w=string.gmatch(q,"[-0-9.]+"); qx,qy=w(),w()
584 w=string.gmatch(_offset,"[-0-9.]+"); xo,yo=w(),w()
585 return {tonumber(qx+xo),tonumber(qy+yo)}
586 end
587
588 local function setup(tfm,index,chartable)
589 -- Default emsize is 1000 for CFF and CFF2
590 ttx.emsize = ttx.emsize or 1000
591
592 scrt.font_space = tfm.printfloat(ttx.emsize*(tfm.font.space),3)
593
594 ttFont = new_element('ttFont', {['sfntVersion']="OTTO", ['ttLibVersion']="3.1"})
595 GlyphOrder = new_element('GlyphOrder')
596 GlyphID = new_element('GlyphID', {['id']="0", ['name']=".notdef"})
597 head = new_element('head')
598 hhea = new_element('hhea')
599 maxp = new_element('maxp')
600 OS_2 = new_element('OS_2')
601 name = new_element('name')
602 cmap = new_element('cmap')
603 post = new_element('post')
604 if ttx.cffver==1 then
605 CFF = new_element('CFF')
606 CFF2 = nil
607 elseif ttx.cffver==2 then
608 CFF2 = new_element('CFF2')
609 CFF = nil
610 end
611 -- FFTM = new_element('FFTM') --FontForge TimeStamp table
612 GDEF = new_element('GDEF')
613 if ttx.cffver==2 then
614 HVAR = new_element('HVAR')
615 end
616 hmtx = new_element('hmtx')
617
618 -- handy
619 panose = new_element('panose')
620 if ttx.cffver==1 then
621 CFFFont = new_element('CFFFont',{['name']= (ttx.userdata.CFFFont['FullName'] or "mfluatestfont"..tostring(math.random(0,1000000)))})
622 elseif ttx.cffver==2 then
623 CFFFont = new_element('CFFFont',{['name']="CFF2Font"})
624 end
625 Private = new_element('Private')
626
627 CharStrings= new_element('CharStrings')
628
629 table_insert(ttFont,GlyphOrder)
630 table_insert(ttFont,head)
631 table_insert(ttFont,hhea)
632 table_insert(ttFont,maxp)
633 table_insert(ttFont,OS_2)
634 table_insert(ttFont,name)
635 table_insert(ttFont,cmap)
636 table_insert(ttFont,post)
637 if ttx.cffver==1 then
638 table_insert(ttFont,CFF)
639 elseif ttx.cffver==2 then
640 table_insert(ttFont,CFF2)
641 end
642 -- table_insert(ttFont,FFTM)
643 table_insert(ttFont,GDEF)
644
645 if ttx.cffver==2 then
646 table_insert(ttFont,HVAR)
647 insert_child(HVAR,{'Version', ['value']="0x00010000"})
648 VarStore = new_element('VarStore',{['Format']="1"})
649 insert_child(VarStore,{'Format',['value']="1"})
650 VarRegionList = new_element('VarRegionList')
651 -- For the moment, 1 axis and 1 region
652 Regions = {new_element('Region',{['index']="0"})}
653 VarRegionAxis = new_element('VarRegionAxis',{['index']="0"})
654 insert_child(VarRegionAxis,new_element('StartCoord',{['value']="-1.0"}))
655 insert_child(VarRegionAxis,new_element('PeakCoord',{['value']="-1.0"}))
656 insert_child(VarRegionAxis,new_element('EndCoord',{['value']="0.0"}))
657 -- insert_child(VarRegionAxis,new_element('StartCoord',{['value']="0.0"}))
658 -- insert_child(VarRegionAxis,new_element('PeakCoord',{['value']="1.0"}))
659 -- insert_child(VarRegionAxis,new_element('EndCoord',{['value']="1.0"}))
660 insert_child(Regions[1],VarRegionAxis)
661 insert_children(VarRegionList,Regions)
662 insert_child(VarStore,VarRegionList)
663 VarData = new_element('VarData',{['index']="0"})
664 insert_child(VarData,new_element('NumShorts',{['value']="1"}))
665 insert_child(VarData,new_element('VarRegionIndex',{['value']="0"}))
666 insert_child(VarData,new_element('Item',{['index']="0", ['value']="[-125]"}))
667 insert_child(VarStore,VarData)
668 AdvWidthMap = new_element('AdvWidthMap')
669 insert_child(AdvWidthMap,new_element('Map', {['index']="0", ['outer']="0", ['inner']="0"}))
670 insert_child(HVAR,VarStore)
671 --insert_child(HVAR,AdvWidthMap)
672 end
673
674 table_insert(ttFont,hmtx)
675
676 table_insert(GlyphOrder,GlyphID)
677
678 if ttx.userdata.head then
679 for k,v in pairs(ttx.userdata.head) do
680 put_head(k,v)
681 end
682 end
683
684 if ttx.userdata.hhea then
685 for k,v in pairs(ttx.userdata.hhea) do
686 put_hhea(k,v)
687 end
688 end
689
690 if ttx.userdata.maxp then
691 for k,v in pairs(ttx.userdata.maxp) do
692 put_maxp(k,v)
693 end
694 end
695
696 if ttx.userdata.OS_2 then
697 for k,v in pairs(ttx.userdata.OS_2) do
698 put_OS_2(k,v)
699 end
700 end
701
702 if ttx.userdata.panose then
703 for k,v in pairs(ttx.userdata.panose) do
704 put_panose(k,v)
705 end
706 end
707 insert_child(OS_2,panose)
708
709 if ttx.userdata.namerecord then
710 local _e
711 local t
712 for i=1,#ttx.userdata.namerecord do
713 t = ttx.userdata.namerecord[i]
714 _e = new_element('namerecord',{['nameID']=t.nameID,['platformID']=t.platformID,
715 ['platEncID']=t.platEncID, ['langID']=t.langID, ['unicode']=t.unicode})
716 insert_child(_e,t.value); insert_child(name,_e)
717 end
718 end
719
720
721 local cmap0,cmap4_0,cmap4_3
722 cmap4_0 = new_element('cmap_format_4', {['platformID']="0",['platEncID']="3",['language']="0",})
723 --cmap0 = new_element('cmap_format_0', {['platformID']="1",['platEncID']="0",['language']="0",})
724 cmap4_3 = new_element('cmap_format_4', {['platformID']="3",['platEncID']="1",['language']="0",})
725
726
727 insert_child(cmap4_0,get_map("0x0",".notdef"))
728 insert_child(cmap4_3,get_map("0x0",".notdef"))
729
730 scrt.cmap = scrt.cmap or {}
731 scrt.cmap.cmap4_0 = cmap4_0
732 scrt.cmap.cmap4_3 = cmap4_3
733 --scrt.cmap.cmap0 = cmap0
734 --scrt.cmap.rawcmap0 = get_raw_cmap0()
735
736
737 insert_child(cmap,{'tableVersion', ['version']="0"})
738 insert_child(cmap,cmap4_0)
739 --insert_child(cmap,cmap0)
740 insert_child(cmap,cmap4_3)
741
742
743 if ttx.userdata.post then
744 for k,v in pairs(ttx.userdata.post) do
745 put_post(k,v)
746 end
747 end
748 if ttx.cffver==2 then
749 insert_child(post,{'psNames'})
750 extranames = new_element('extraNames')
751 insert_child(post,extranames)
752 end
753
754 if ttx.cffver==1 then
755 put_CFF('major',"1")
756 put_CFF('minor',"0")
757 insert_child(CFF,CFFFont)
758 elseif ttx.cffver==2 then
759 put_CFF2('major',"2")
760 put_CFF2('minor',"0")
761 insert_child(CFF2,CFFFont)
762 end
763
764 if ttx.cffver==1 then
765 if ttx.userdata.CFFFont then
766 for k,v in pairs(mflua.ttx.userdata.CFFFont) do
767 put_CFFFont(k,v)
768 end
769 end
770
771 insert_child(CFFFont,{'Encoding', ['name']=mflua.ttx.userdata.CFFFont.Encoding})
772
773 insert_child(CFFFont,Private)
774 if ttx.userdata.CFFFont.Private then
775 for k,v in pairs(ttx.userdata.CFFFont.Private) do
776 put_Private(k,v)
777 end
778 end
779
780 local subrs = new_element('Subrs')
781 scrt.subrs = subrs
782 if ttx.userdata.CFFFont.Private.Subrs then
783 for k,v in pairs(ttx.userdata.CFFFont.Private.Subrs) do
784 insert_child(subrs,{'CharString', ['index']=k, v})
785 end
786 end
787 insert_child(Private,subrs)
788
789 elseif ttx.cffver==2 then
790 insert_child(CFFFont,{'FontMatrix', ['value']=mflua.ttx.userdata.CFF2Font['FontMatrix']})
791 local FDArray = new_element('FDArray')
792 local FontDict = new_element('FontDict', {['index']="0"})
793 insert_child(FontDict,{'FontMatrix', ['value']=mflua.ttx.userdata.CFF2Font['FontMatrix']})
794 insert_child(FontDict,{'Private'})
795 insert_child(FDArray,FontDict)
796 insert_child(CFFFont,FDArray)
797 end
798
799 -- .notdef char
800 local notdef = new_element('CharString', {['name']=".notdef"})
801 if ttx.cffver == 1 then
802 insert_child(notdef,"endchar\n")
803 elseif ttx.cffver == 2 then
804 insert_child(notdef,"0 0 0 0 0 0 rrcurveto \n")
805 end
806
807 insert_child_at_beginning(CharStrings, notdef)
808
809 insert_child(CFFFont,CharStrings)
810
811 if CFF2 and mflua.ttx.userdata.varstore then
812 insert_child(CFFFont,mflua.ttx.userdata.varstore)
813 end
814
815
816 --
817 -- TODO: check GlobalSubrs!!
818 --
819 local globsubrs = new_element('GlobalSubrs')
820 if ttx.cffver==1 then
821 insert_child(CFF,globsubrs)
822 elseif ttx.cffver==2 then
823 insert_child(CFF2,globsubrs)
824 end
825 if ttx.userdata.CFF.GlobalSubrs then
826 for k,v in pairs(ttx.userdata.CFF.GlobalSubrs) do
827 insert_child(globsubrs,{'CharString', ['index']=k, v})
828 end
829 end
830
831 local glyphclassdef = new_element('GlyphClassDef',{['Format']="2"})
832 insert_child(GDEF,{'Version',['value']="0x00010000"})
833 insert_child(GDEF,glyphclassdef)
834 scrt.gdef = scrt.gdef or {}
835 scrt.gdef.glyphclassdef = glyphclassdef
836
837 local LigCaretList = new_element('LigCaretList')
838 insert_child(LigCaretList,new_element('Coverage',{['Format']="2"}))
839 insert_child(GDEF,LigCaretList)
840
841
842 insert_child(hmtx, get_mtx('.notdef',
843 ttx.userdata.char_notdef['width'],
844 ttx.userdata.char_notdef['lsb']))
845
846 if CFF2 and ttx.userdata.fvar then
847 local fvar = new_element('fvar')
848 local v, axis, _e
849 for i=1,#ttx.userdata.fvar.Axis do
850 v = ttx.userdata.fvar.Axis[i]
851 axis = new_element('Axis')
852 _e = new_element('AxisTag'); _e[2] = v['AxisTag']; insert_child(axis,_e)
853 _e = new_element('MinValue'); _e[2] = v['MinValue']; insert_child(axis,_e)
854 _e = new_element('DefaultValue'); _e[2] = v['DefaultValue']; insert_child(axis,_e)
855 _e = new_element('MaxValue'); _e[2] = v['MaxValue']; insert_child(axis,_e)
856 _e = new_element('AxisNameID'); _e[2] = v['AxisNameID']; insert_child(axis,_e)
857 end
858 insert_child(fvar,axis)
859 insert_child(ttFont,fvar)
860 end
861
862 end
863
864
865 local function reverse(cycle)
866 local t={}
867 local curve
868 local p,c1,c2,q
869 for i=#cycle,1,-1 do
870 curve=cycle[i]
871 p,c1,c2,q=curve[1],curve[2],curve[3],curve[4]
872 curve[1],curve[2],curve[3],curve[4]=q,c2,c1,p
873 t[#t+1]=curve
874 end
875 return t
876 end
877
878
879 local function sort(cycle)
880 local t={}
881 local curve
882 local p,q, index
883 local xmin,ymin = 4096,4096
884 index = 1
885 for i=1,#cycle do
886 curve=cycle[i]
887 p=curve[1];
888 t = _eval_tonumber(p)
889 -- Align to leftmost x and y
890 --if t[1]<=xmin and t[2]<=ymin then index = i;xmin,ymin=t[1],t[2] end
891 -- Align to the leftmost x
892 if t[1]<=xmin then index = i;xmin,ymin=t[1],t[2] end
893 end
894 t={}
895 for i=index,#cycle do t[#t+1]=cycle[i] end
896 for i=1,index-1 do t[#t+1]=cycle[i] end
897 for i=1,#t do
898 curve=t[i]
899 p,c1,c2,q=curve[1],curve[2],curve[3],curve[4]
900 end
901 return t
902 end
903
904
905 local function _reverse(cycle)
906 local cv1, cv2
907 local p,c1,c2,q
908 local l=#cycle
909 for i=1,l/2 do
910 cv1=cycle[i]
911 cv2=cycle[l+1-i]
912 p,c1,c2,q = cv1[1],cv1[2],cv1[3],cv1[4]
913 cv1[1],cv1[2],cv1[3],cv1[4] = q,c2,c1,p
914 p,c1,c2,q = cv2[1],cv2[2],cv2[3],cv2[4]
915 cv2[1],cv2[2],cv2[3],cv2[4] = q,c2,c1,p
916 cycle[l+1-i]=cv1
917 cycle[i]=cv2
918 end
919 end
920
921
922 -- local function updateminmax(p,c1,c2,q)
923 -- ttx = ttx or {}
924 -- ttx.userdata = ttx.userdata or {}
925 -- ttx.userdata.head = ttx.userdata.head or {}
926 -- local t;
927 -- -- hm not ok, too raw
928 -- -- we need at least 3 bisections
929 -- t = {p[1],c1[1],c2[1],q[1]}
930 -- table_sort(t)
931 -- ttx.userdata.head['xMin']= math_floor(t[1])
932 -- ttx.userdata.head['xMax']= math_floor(t[4])
933 -- t = {p[2],c1[2],c2[2],q[2]}
934 -- table_sort(t)
935 -- ttx.userdata.head['yMin']= math_floor(t[1])
936 -- ttx.userdata.head['yMax']= math_floor(t[4])
937 -- end
938
939
940 local function update_char_lsb(glyph_name,char_wd_emunit,index)
941 local err = true
942 if ttx.userdata.update_char_lsb and
943 type(ttx.userdata.update_char_lsb)=='function' then
944 ttx.userdata.update_char_lsb(glyph_name,char_wd_emunit,index)
945 return
946 end
947 if GF_chars[index] then
948 insert_child(hmtx, get_mtx(glyph_name,
949 char_wd_emunit,GF_chars[index].min_m))
950 return
951 end
952 if err then print_err("update_char_lsb failed") end
953 end
954
955
956
957
958
959 local function isvhline(py,c1y,c2y,qy)
960 if (py==c1y) and (c1y==c2y) and (c2y==qy) then
961 return true
962 end
963 --local abs = math.abs
964 local eps = ttx.eps or 0.01
965 local d1,d2,d3,d4 = abs(py-c1y), abs(c1y-c2y), abs(c2y-qy),abs(py-qy)
966 if d1<eps and d2<eps and d3<eps and d4<eps then
967 return true
968 end
969 return false
970 end
971
972
973 local function isline(px,py,c1x,c1y,c2x,c2y,qx,qy)
974 local px,py,c1x,c1y,c2x,c2y,qx,qy = 0,0,c1x-px,c1y-py,c2x-px,c2y-py,qx-px,qy-py
975 local eps = ttx.deps or 0.01
976 local atan2,abs = math.atan2, math.abs
977 a = atan2(qy,qx)
978 a1 = atan2(c1y,c1x)
979 a2 = atan2(c2y,c2x)
980 if abs(a-a1)<eps and abs(a-a2)<eps and abs(a1-a2)<eps then
981 return true
982 end
983 return false
984 end
985
986
987 local function insert_stem(index,stem_type)
988 if mflua and mflua.userdata and mflua.userdata.char and mflua.userdata.char[index] and mflua.userdata.char[index][stem_type] then
989 -- stem[i]={base, delta}
990 local stem = mflua.userdata.char[index][stem_type]
991 local content = {}
992 for i=1,#stem do
993 --table_insert(content, format("%s %s", normalize(stem[i][1]),normalize(stem[i][2])))
994 table_insert(content, format("%s %s",stem[i][1],stem[i][2]))
995 end
996 table_insert(content,stem_type)
997 return table_concat(content, " ")
998 end
999 return nil
1000 end
1001
1002
1003
1004 local function make_glyph(valid_curves,char,cycles,tfm)
1005 -- Write the ttx
1006
1007 if #cycles == 0 and #valid_curves==0 then
1008 print("no outlines ")
1009 --return
1010 end
1011 local tfm = tfm
1012 local mflua_index = tointeger( char['index'] ) -- better a string or a number
1013 local index = mflua_index
1014 if mflua.ttx.userdata.charindex and mflua.ttx.userdata.charindex[index] then
1015 index = mflua.ttx.userdata.charindex[index]
1016 end
1017 local design_size=tonumber ( print_scaled(MFbuiltin.designsize()) ) --pt
1018 local char_wd=tonumber( char['char_wd'] ) -- pt
1019 local char_ht=tonumber( char['char_ht'] ) -- pt
1020 local char_dp=tonumber( char['char_dp'] ) -- pt
1021
1022 local cmap4_0 = scrt.cmap.cmap4_0
1023 local cmap4_3 = scrt.cmap.cmap4_3
1024 --local rawcmap0 = scrt.cmap.rawcmap0
1025
1026 local glyphclassdef = scrt.gdef.glyphclassdef
1027
1028
1029 local x_resolution = math_floor(0.5+tonumber( print_scaled(MFbuiltin.hppp()) )* 72.27)
1030 local y_resolution = math_floor(0.5+tonumber( print_scaled(MFbuiltin.vppp()) )* 72.27)
1031 assert(x_resolution==y_resolution, format('Error on _make_glyph x_res=%d and y_res=%d differ',x_resolution,y_resolution))
1032
1033 local resolution = x_resolution
1034 local emsize = ttx.emsize -- 1000, type 1, also known as em_unit: 1000 emsize = 1em
1035 local em_unit = emsize
1036 local em_unit_for_pixel = (72.27/design_size) * (emsize / resolution)
1037 local bp_for_pt = 72/72.27
1038 local char_wd_emunit = tonumber(format("%0.0f", (char_wd/design_size) *em_unit))
1039 local char_ht_emunit = tonumber(format("%0.0f", (char_ht/design_size) *em_unit))
1040 local char_dp_emunit = tonumber(format("%0.0f", (char_dp/design_size) *em_unit))
1041 local glyph_name = tostring(char['charname'])
1042
1043 if glyph_name and glyph_name ~= '' and not(index==0) then
1044 table_insert(GlyphOrder,get_GlyphID(index,glyph_name))
1045 end
1046
1047 local content = {}
1048 table_insert(content," ")
1049 if CFF2 and (abs(char_wd_emunit-scrt.font_space)>1e-4) then
1050 table_insert(content,format("%0.3f w",char_wd_emunit-scrt.font_space))
1051 end
1052
1053
1054 table_insert(content,insert_stem(mflua_index,"hstem"))
1055 table_insert(content,insert_stem(mflua_index,"vstem"))
1056 --table_insert(content,"mask ?") -- TODO
1057
1058 local origin = {0,0}
1059 for i,_cycle in pairs(cycles) do
1060 local cycle
1061 local p,c1,c2,q,offset
1062 --cycle = reverse(_cycle)
1063 cycle = _cycle
1064 -- do we need a sort ?
1065 --cycle = sort(cycle)
1066 for i1,curve in ipairs(cycle) do
1067 p,c1,c2,q,offset=curve[1],curve[2],curve[3],curve[4],curve[5]
1068 p =_eval_tonumber(p ,offset)
1069 c1=_eval_tonumber(c1,offset)
1070 c2=_eval_tonumber(c2,offset)
1071 q =_eval_tonumber(q ,offset)
1072 -- em_unit_for_pixel
1073 p[1],p[2] = p[1] *em_unit_for_pixel, p[2]*em_unit_for_pixel
1074 q[1],q[2] = q[1] *em_unit_for_pixel, q[2]*em_unit_for_pixel
1075 c1[1],c1[2] = c1[1]*em_unit_for_pixel, c1[2]*em_unit_for_pixel
1076 c2[1],c2[2] = c2[1]*em_unit_for_pixel, c2[2]*em_unit_for_pixel
1077
1078
1079 p[1],p[2] = toscaled(p[1]), toscaled(p[2])
1080 q[1],q[2] = toscaled(q[1]), toscaled(q[2])
1081 c1[1],c1[2] = toscaled(c1[1]), toscaled(c1[2])
1082 c2[1],c2[2] = toscaled(c2[1]), toscaled(c2[2])
1083
1084
1085 if CFF then
1086 -- print(format("p1 %s path=(%0.14f,%0.14f) (%0.14f,%0.14f) (%0.14f,%0.14f) (%0.14f,%0.14f)", index,
1087 -- p[1], p[2],
1088 -- c1[1], c1[2],
1089 -- c2[1], c2[2],
1090 -- q[1],q[2]))
1091 -- print(format("p2 %s path=(%0.4f,%0.4f) (%0.4f,%0.4f) (%0.4f,%0.4f) (%0.4f,%0.4f)", index,
1092 -- p[1], p[2],
1093 -- c1[1], c1[2],
1094 -- c2[1], c2[2],
1095 -- q[1],q[2]))
1096 -- print(format("p3 %s path=(%s,%s) (%s,%s) (%s,%s) (%s,%s)", index,
1097 -- normalize(p[1]), normalize(p[2]),
1098 -- normalize(c1[1]), normalize(c1[2]),
1099 -- normalize(c2[1]), normalize(c2[2]),
1100 -- normalize(q[1]), normalize(q[2])))
1101
1102 if i1==1 then
1103 --print(format("m %s %s %s rmoveto",index, normalize(p[1]-origin[1]),normalize(p[2]-origin[2])))
1104 table_insert(content,format("%s %s rmoveto",normalize(p[1]-origin[1]),normalize(p[2]-origin[2])))
1105 origin = {p[1],p[2]}
1106 end
1107 if isvhline(p[2],c1[2],c2[2], q[2]) then -- check horizontal line
1108 --print(format("h %s %s hlineto", index, normalize(q[1]-p[1])))
1109 table_insert(content,format("%s hlineto", normalize(q[1]-p[1])))
1110 elseif isvhline(p[1],c1[1],c2[1], q[1]) then -- check vertical line
1111 --print(format("v %s %s vlineto", index,normalize(q[2]-p[2])))
1112 table_insert(content,format("%s vlineto", normalize(q[2]-p[2])))
1113 elseif isline(p[1],p[2],c1[1],c1[2],c2[1],c2[2],q[1],q[2]) then -- check a line
1114 --print(format("r %s %s %s rlineto", index, normalize(q[1]-p[1]), normalize(q[2]-p[2])))
1115 table_insert(content,format("%s %s rlineto", normalize(q[1]-p[1]), normalize(q[2]-p[2])))
1116 else
1117 local prec = 3;
1118 -- p[1] = normalize_and_simplify(p[1],prec);p[2] = normalize_and_simplify(p[2],prec);
1119 -- c1[1] = normalize_and_simplify(c1[1],prec);c1[2] = normalize_and_simplify(c1[2],prec);
1120 -- c2[1] = normalize_and_simplify(c2[1],prec);c2[2] = normalize_and_simplify(c2[2],prec);
1121 -- q[1] = normalize_and_simplify(q[1],prec);q[2] = normalize_and_simplify(q[2],prec);
1122
1123
1124 table_insert(content,(format("%s %s %s %s %s %s rrcurveto",
1125 normalize_and_simplify(c1[1] -p[1],prec), normalize_and_simplify(c1[2]-p[2],prec),
1126 normalize_and_simplify(c2[1]-c1[1],prec), normalize_and_simplify(c2[2]-c1[2],prec),
1127 normalize_and_simplify(q[1] -c2[1],prec), normalize_and_simplify(q[2]-c2[2],prec))))
1128
1129 end
1130
1131 elseif CFF2 then --
1132 -- reverse the key, because we reverse the cycle
1133 local kp = table.concat({curve[4],curve[3],curve[2],curve[1]})
1134 local deltas_width = {{0,0},{0,0}, {0,0},{0,0}, {0,0},{0,0}, {0,0},{0,0}}
1135 local origin_deltas_width = {0,0}
1136 local a, b, c, d, da, Da, db, Db, dc, Dc, dd, Dd
1137 if mflua.userdata.deltas_width[glyph_name] then
1138 deltas_width = mflua.userdata.deltas_width[glyph_name][kp]
1139 -- Original from MF, we have to reverse it; d..c..b..a is the key,
1140 -- da and Da are the delta min and delta max,
1141 -- where delta is (variated_instance - reference_instance)
1142 -- delta are not affected by offset, but scaling are cannot be discarded
1143 a, b, c, d, da, Da, db, Db, dc, Dc, dd, Dd = table.unpack(deltas_width)
1144 deltas_width = {dd, Dd,dc, Dc,db, Db,da, Da}
1145 for _,v in ipairs(deltas_width) do
1146 v[1],v[2]=v[1]*em_unit_for_pixel, v[2]*em_unit_for_pixel
1147 end
1148 end
1149 da, Da, db, Db, dc, Dc, dd, Dd = table.unpack(deltas_width)
1150 -- One axis only, width
1151 -- Condensed Regular (still not Extended)
1152 local blend
1153 if i1==1 then
1154 blend = format("%0.2f %0.2f 2 blend",da[1]-origin_deltas_width[1],da[2]-origin_deltas_width[2]) ;
1155 table_insert(content,format("%0.0f %0.0f %s rmoveto",p[1]-origin[1],p[2]-origin[2],blend))
1156 origin = {p[1],p[2]}
1157 origin_deltas_width ={da[1],da[2]}
1158 end
1159 blend = format("%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f 6 blend",
1160 db[1]-da[1],db[2]-da[2],
1161 dc[1]-db[1],dc[2]-db[2],
1162 dd[1]-dc[1],dd[2]-dc[2])
1163
1164 table_insert(content,format("%0.0f %0.0f %0.0f %0.0f %0.0f %0.0f %s rrcurveto",
1165 c1[1] -p[1], c1[2]-p[2],
1166 c2[1]-c1[1], c2[2]-c1[2],
1167 q[1] -c2[1], q[2]-c2[2],blend))
1168
1169
1170
1171 -- if i1==1 then
1172 -- blend = '0 0 2 blend' ;
1173 -- table_insert(content,format("%0.0f %0.0f %s rmoveto",p[1]-origin[1],p[2]-origin[2],blend))
1174 -- origin = {p[1],p[2]}
1175 -- end
1176 -- --table_insert(content,format("%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f rrcurveto",
1177 -- if i1==1 then blend = '0 0 0 0 0 0 6 blend' ; else blend = '0 0 0 0 -10 0 6 blend' end
1178 -- table_insert(content,format("%0.0f %0.0f %0.0f %0.0f %0.0f %0.0f %s rrcurveto",
1179 -- c1[1] -p[1], c1[2]-p[2],
1180 -- c2[1]-c1[1], c2[2]-c1[2],
1181 -- q[1] -c2[1], q[2]-c2[2],blend))
1182 end
1183 end
1184 end
1185 if ttx.cffver == 1 then
1186 table_insert(content,"endchar\n")
1187 elseif ttx.cffver == 2 then
1188 table_insert(content,"")
1189 end
1190 local CharString = new_element('CharString', {['name']=glyph_name})
1191 insert_child(CharString,table_concat(content,"\n"))
1192
1193 insert_child(CharStrings,CharString)
1194 insert_child(cmap4_0,get_map(format("0x%x",index),glyph_name))
1195 --rawcmap0[format("0x%x",1+index%256)] = glyph_name
1196 insert_child(cmap4_3,get_map(format("0x%x",index),glyph_name))
1197
1198 insert_child(glyphclassdef, get_ClassDef(glyph_name,"1"))
1199 update_char_lsb(glyph_name,char_wd_emunit,mflua_index)
1200
1201
1202 if ttx.cffver==2 then
1203 local psName = new_element('psName', {['name']=glyph_name})
1204 local Item = new_element('Item', {['index']=format("%d",index), ['value']="[-125]"})
1205 local Map = new_element('Map', {['index']=format("%d",index), ['outer']="0", ['inner']="0"})
1206 local VarStore = get_children(HVAR,{'VarStore'})
1207 local VarData = get_children(VarStore[1],{'VarData'})
1208 --local AdvWidthMap = get_children(HVAR,{'AdvWidthMap'})
1209 insert_child(VarData[1],Item)
1210 --insert_child(AdvWidthMap[1],Map)
1211 insert_child(extranames,psName)
1212
1213 end
1214
1215 end -- make_glyph
1216
1217
1218
1219
1220
1221 ttx.make_glyph = make_glyph
1222 ttx.makefont = makefont
1223 ttx.setup = setup
1224 ttx.userdata = {}
1225 ttx.cffver = 1
1226 ttx.eps = 0.01
1227 ttx.deps = 0.01
1228 return ttx