~reviczky/context/mirror

« back to all changes in this revision

Viewing changes to tex/context/base/mkxl/lpdf-lmt.lmt

  • Committer: Context Git Mirror Bot
  • Author(s): Hans Hagen
  • Date: 2022-07-06 20:05:18 UTC
  • Revision ID: git-v1:82c674fdcf5bcff4ad0dc0936d638fc729145616
2022-07-06 21:35:00

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
local setmetatableindex = table.setmetatableindex
40
40
local loaddata = io.loaddata
41
41
 
42
 
local bpfactor               = number.dimenfactors.bp
 
42
local bpfactor       <const> = number.dimenfactors.bp
43
43
 
44
44
local osuuid                 = os.uuid
45
45
local zlibcompresssize       = xzip.compresssize
129
129
local getxformname, getximagename
130
130
local boundingbox, shippingmode, objectnumber
131
131
local tmrx, tmry, tmsx, tmsy, tmtx, tmty
132
 
local cmrx, cmry, cmsx, cmsy, cmtx, cmty
 
132
----- cmrx, cmry, cmsx, cmsy, cmtx, cmty
 
133
local cmrx, cmry,             cmtx, cmty
133
134
local tmef
 
135
local c_effect
134
136
 
135
137
local function usefont(t,k) -- a bit redundant hash
136
138
 -- local v = pdfgetfontname(k)
142
144
local function reset_variables(specification)
143
145
    pdf_h, pdf_v  = 0, 0
144
146
    cmrx, cmry    = 1.0, 1.0
145
 
    cmsx, cmsy    = 0.0, 0.0
 
147
 -- cmsx, cmsy    = 0.0, 0.0
146
148
    cmtx, cmty    = 0.0, 0.0
147
149
    tmrx, tmry    = 1.0, 1.0
148
150
    tmsx, tmsy    = 0.0, 0.0
169
171
    cur_factor    = 0
170
172
    tj_delta      = 0.0
171
173
    cw            = 0.0
 
174
    c_effect      = nil
172
175
    usedfonts     = setmetatableindex(usefont)
173
176
    usedxforms    = { }
174
177
    usedximages   = { }
264
267
-- helpers (maybe in collapse mode we have to go %.9N)
265
268
 
266
269
local f_cm = formatters["%.6N %.6N %.6N %.6N %.6N %.6N cm"]
 
270
local f_cz = formatters["%.6N 0 0 %.6N %.6N %.6N cm"]
267
271
local f_tm = formatters["%.6N %.6N %.6N %.6N %.6N %.6N Tm"]
268
272
 
269
273
local saved_text_pos_v = 0
277
281
    need_font  = true
278
282
    need_width = 0
279
283
    need_mode  = 0
 
284
    c_effect   = nil
280
285
    mode       = "text"
281
286
end
282
287
 
295
300
    mode  = "page"
296
301
end
297
302
 
298
 
local saved_chararray_pos_h
299
 
local saved_chararray_pos_v
300
 
 
301
 
local saved_b = 0
302
 
 
303
 
local function begin_chararray()
304
 
    saved_chararray_pos_h = pdf_h
305
 
    saved_chararray_pos_v = pdf_v
306
 
    cw = horizontalmode and saved_chararray_pos_h or - saved_chararray_pos_v
307
 
    tj_delta = 0
308
 
    saved_b = b
309
 
    b = b + 1 ; buffer[b] = " ["
310
 
    mode = "chararray"
311
 
end
312
 
 
313
 
local function end_chararray()
314
 
    b = b + 1 ; buffer[b] = "] TJ"
315
 
    buffer[saved_b] = concat(buffer,"",saved_b,b)
316
 
    b = saved_b
317
 
    pdf_h = saved_chararray_pos_h
318
 
    pdf_v = saved_chararray_pos_v
319
 
    mode  = "text"
 
303
local begin_chararray, end_chararray do
 
304
 
 
305
    local saved_chararray_pos_h
 
306
    local saved_chararray_pos_v
 
307
 
 
308
    local saved_b = 0
 
309
 
 
310
    begin_chararray = function()
 
311
        saved_chararray_pos_h = pdf_h
 
312
        saved_chararray_pos_v = pdf_v
 
313
        cw = horizontalmode and saved_chararray_pos_h or - saved_chararray_pos_v
 
314
        tj_delta = 0
 
315
        saved_b = b
 
316
        b = b + 1 ; buffer[b] = " ["
 
317
        mode = "chararray"
 
318
    end
 
319
 
 
320
    end_chararray = function()
 
321
        b = b + 1 ; buffer[b] = "] TJ"
 
322
        buffer[saved_b] = concat(buffer,"",saved_b,b)
 
323
        b = saved_b
 
324
        pdf_h = saved_chararray_pos_h
 
325
        pdf_v = saved_chararray_pos_v
 
326
        mode  = "text"
 
327
    end
 
328
 
320
329
end
321
330
 
322
331
local function begin_charmode()
353
362
local function pdf_set_pos(h,v)
354
363
    local move = calc_pdfpos(h,v)
355
364
    if move then
356
 
        b = b + 1 ; buffer[b] = f_cm(cmrx, cmsx, cmsy, cmry, cmtx*bpfactor, cmty*bpfactor)
 
365
     -- b = b + 1 ; buffer[b] = f_cm(cmrx, cmsx, cmsy, cmry, cmtx*bpfactor, cmty*bpfactor)
 
366
        b = b + 1 ; buffer[b] = f_cz(cmrx, cmry,             cmtx*bpfactor, cmty*bpfactor)
357
367
        pdf_h = pdf_h + cmtx
358
368
        pdf_v = pdf_v + cmty
359
369
    end
385
395
            return
386
396
        end
387
397
    end
388
 
    b = b + 1 ; buffer[b] = f_cm(cmrx, cmsx, cmsy, cmry, cmtx*bpfactor, cmty*bpfactor)
 
398
 -- b = b + 1 ; buffer[b] = f_cm(cmrx, cmsx, cmsy, cmry, cmtx*bpfactor, cmty*bpfactor)
 
399
    b = b + 1 ; buffer[b] = f_cz(cmrx,             cmry, cmtx*bpfactor, cmty*bpfactor)
389
400
    pdf_h = pdf_h + cmtx
390
401
    pdf_v = pdf_v + cmty
391
402
end
393
404
local function pdf_set_pos_temp(h,v)
394
405
    local move = calc_pdfpos(h,v)
395
406
    if move then
396
 
        b = b + 1 ; buffer[b] = f_cm(cmrx, cmsx, cmsy, cmry, cmtx*bpfactor, cmty*bpfactor)
 
407
     -- b = b + 1 ; buffer[b] = f_cm(cmrx, cmsx, cmsy, cmry, cmtx*bpfactor, cmty*bpfactor)
 
408
        b = b + 1 ; buffer[b] = f_cz(cmrx,             cmry, cmtx*bpfactor, cmty*bpfactor)
397
409
    end
398
410
end
399
411
 
522
534
        collapse = v
523
535
    end)
524
536
 
525
 
    local function setup_fontparameters(font,factor,sx,sy)
526
 
        local slant   = fontparameters.slantfactor   or 0
527
 
        local extend  = fontparameters.extendfactor  or 1
528
 
        local squeeze = fontparameters.squeezefactor or 1
529
 
        local expand  = 1 + factor / 1000000
530
 
        local format  = fontproperties.format
531
 
        tmef       = expand
532
 
        tmrx       = expand * extend
533
 
        tmsy       = slant
534
 
        tmry       = squeeze
535
 
        need_width = fontparameters.width or 0
536
 
        need_mode  = fontparameters.mode or 0
 
537
    local function setup_fontparameters(font,factor,sx,sy,effect)
 
538
        local format = fontproperties.format
 
539
        local expand = 1 + factor / 1000000
 
540
        tmef         = expand
 
541
 
 
542
     -- local slant   = fontparameters.slantfactor   or 0
 
543
     -- local squeeze = fontparameters.squeezefactor or 1
 
544
     -- local extend  = fontparameters.extendfactor  or 1
 
545
     -- tmrx          = extend * expand
 
546
     -- tmry          = squeeze
 
547
     -- tmsy          = slant
 
548
     -- need_width    = fontparameters.width or 0
 
549
     -- need_mode     = fontparameters.mode or 0
 
550
 
 
551
        if effect then
 
552
            if effect ~= c_effect then
 
553
                tmsy       = effect.slant   or fontparameters.slantfactor   or 0
 
554
                tmry       = effect.squeeze or fontparameters.squeezefactor or 1
 
555
                tmrx       = effect.extend  or fontparameters.extendfactor  or 1
 
556
                need_mode  = effect.mode    or fontparameters.mode          or 0
 
557
                need_width = effect.line    or fontparameters.width         or 0
 
558
                c_effect   = effect
 
559
            else
 
560
             -- we could check if effects have changed but effects use unique tables; for
 
561
             -- now they win over font effects (only used in math)
 
562
            end
 
563
        else
 
564
            tmsy       = fontparameters.slantfactor   or 0
 
565
            tmry       = fontparameters.squeezefactor or 1
 
566
            tmrx       = fontparameters.extendfactor  or 1
 
567
            need_mode  = fontparameters.mode          or 0
 
568
            need_width = fontparameters.width         or 0
 
569
        end
 
570
 
 
571
        tmrx       = expand * tmrx
 
572
 
537
573
        f_cur      = font
538
574
        f_pdf      = usedfonts[font] -- cache
539
575
        cur_factor = factor
665
701
        local s = data.scale
666
702
        local x = data.xoffset
667
703
        local y = data.yoffset
668
 
 
669
704
        if s then
670
705
            sx = s * sx
671
706
            sy = s * sy
679
714
            csy = 1
680
715
        end
681
716
 
 
717
        local effect = data.effect
 
718
 
682
719
     -- if sx ~= f_x_scale or sy ~= f_y_scale or need_tf or font ~= f_cur or f_pdf ~= f_pdf_cur or fs ~= fs_cur then
683
 
        if sx ~= f_x_scale or sy ~= f_y_scale or need_tf or font ~= f_cur or f_pdf ~= f_pdf_cur or fs ~= fs_cur or mode == "page" then
 
720
        if sx ~= f_x_scale or sy ~= f_y_scale or need_tf or font ~= f_cur or f_pdf ~= f_pdf_cur or fs ~= fs_cur
 
721
            or mode == "page"
 
722
            or effect ~= c_effect
 
723
        then
684
724
            pdf_goto_textmode()
685
 
            setup_fontparameters(font,factor,sx,sy) -- too often due to page
 
725
            setup_fontparameters(font,factor,sx,sy,effect) -- too often due to page
686
726
            set_font()
687
727
     -- elseif mode == "page" then
688
728
     --     pdf_goto_textmode()
689
729
     --     set_font()
690
730
        elseif cur_tmrx ~= tmrx or cur_factor ~= factor then
691
 
            setup_fontparameters(font,factor,sx,sy)
 
731
            setup_fontparameters(font,factor,sx,sy,effect)
692
732
            need_tm = true
693
733
        end
694
734
 
1120
1160
 
1121
1161
    local pdfcollectedresources = lpdf.collectedresources
1122
1162
 
1123
 
    function codeinjections.saveboxresource(box,attributes,resources,immediate,kind,margin)
 
1163
    function codeinjections.saveboxresource(box,attributes,resources,immediate,kind,margin,onum)
1124
1164
        n = n + 1
1125
1165
        local immediate = true
1126
1166
        local margin    = margin or 0 -- or dimension
1127
 
        local objnum    = pdfreserveobject()
 
1167
        local objnum    = onum or pdfreserveobject()
1128
1168
        local list      = tonut(type(box) == "number" and tex.takebox(box) or box)
1129
1169
        --
1130
1170
        if resources == true then
1146
1186
            index      = objnum,
1147
1187
            objnum     = objnum,
1148
1188
        }
 
1189
        local r = boxresources[objnum]
 
1190
        if r then
 
1191
            flushlist(l.list)
 
1192
        end
1149
1193
        boxresources[objnum] = l
1150
1194
        if immediate then
1151
1195
            localconverter(list,"xform",objnum,l)
1225
1269
        tx = cmtx * bpfactor
1226
1270
        ty = cmty * bpfactor
1227
1271
        b = b + 1 ; buffer[b] = s_b
1228
 
        b = b + 1 ; buffer[b] = f_cm(rx,0,0,ry,tx,ty)
 
1272
     -- b = b + 1 ; buffer[b] = f_cm(rx,0,0,ry,tx,ty)
 
1273
        b = b + 1 ; buffer[b] = f_cz(rx,    ry,tx,ty)
1229
1274
        b = b + 1 ; buffer[b] = f_fm(name)
1230
1275
        b = b + 1 ; buffer[b] = s_e
1231
1276
    end
1452
1497
 
1453
1498
        pdf_goto_pagemode()
1454
1499
 
1455
 
     -- local saved_b = b
1456
 
 
1457
1500
        b = b + 1 ; buffer[b] = s_b
1458
1501
 
1459
1502
        local dim_h = size_h * bpfactor
1483
1526
        b = b + 1 ; buffer[b] = rule
1484
1527
        b = b + 1 ; buffer[b] = s_e
1485
1528
 
1486
 
     -- buffer[saved_b] = concat(buffer," ",saved_b,b)
1487
 
     -- b = saved_b
1488
 
 
1489
1529
    end
1490
1530
 
1491
1531
    flushers.simplerule = function(pos_h,pos_v,pos_r,size_h,size_v)
1581
1621
        return list
1582
1622
    end
1583
1623
 
1584
 
    function lpdf.setpageorder(mapping)
 
1624
    function lpdf.setpageorder(mapping,p)
1585
1625
        -- mapping can be a hash so:
1586
1626
        local list = table.sortedkeys(mapping)
1587
1627
        local n    = #list
1588
 
        if n == nofpages then
 
1628
        local nop  = p or nofpages
 
1629
        if n == nop then
1589
1630
            local done = { }
1590
1631
            local hash = { }
1591
1632
            for i=1,n do
1603
1644
            end
1604
1645
            pages = done
1605
1646
        else
1606
 
            report("invalid page order, %i entries expected",nofpages)
 
1647
            report("invalid page order, %i entries expected",nop)
1607
1648
        end
1608
1649
    end
1609
1650
 
2070
2111
local flushdeferred -- defined later
2071
2112
 
2072
2113
local level = 0
 
2114
local state = true
 
2115
 
 
2116
function lpdf.setpagestate(s)
 
2117
    state = s
 
2118
end
2073
2119
 
2074
2120
local finalize  do
2075
2121
 
2157
2203
 
2158
2204
            -- resources can be indirect
2159
2205
 
 
2206
if state == "ignore" or state == false then
 
2207
 
 
2208
else
 
2209
 
2160
2210
            registerpage(pageattributes)
2161
2211
 
2162
2212
            lpdf.finalizepage(true)
2171
2221
            if CropBox  then pageattributes.CropBox  = pdfsharedobject(CropBox ) end
2172
2222
            if BleedBox then pageattributes.BleedBox = pdfsharedobject(BleedBox) end
2173
2223
 
 
2224
end
 
2225
 
2174
2226
        else
2175
2227
 
2176
2228
            local xformtype  = specification.type or 0
2226
2278
         -- wrapper.Resources = pdfreference(pdfflushobject(boxresources))
2227
2279
 
2228
2280
            if resources ~= "" then
2229
 
                 boxresources = boxresources + resources
 
2281
                boxresources = boxresources + resources
2230
2282
            end
2231
2283
            if attributes ~= "" then
2232
2284
                wrapper = wrapper + attributes