home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / cpm / list / ep-src.ark / PRTSBUF.MAC < prev    next >
Encoding:
Text File  |  1988-05-21  |  16.9 KB  |  1,255 lines

  1.  
  2.     include    BDSYM.EQU
  3.     include    EPDATA
  4.  
  5.     .comment    `
  6. /************************************************/
  7. /* Called when there are enough characters in    */
  8. /*    outbuf to make up a line.        */
  9. /*  Determines how many of these should be    */
  10. /*    printed, prints them, and moves the    */
  11. /*    remainder back so they will be part of    */
  12. /*    next line                */
  13. /************************************************/
  14. prtsbuf()
  15. {    int /* endcol, begcol,*/ i, savlen;
  16.     char c;
  17.  
  18. /* debug */
  19. /*printf("\n .IN is %d; lindent is %d.", in, lindent);*/
  20.  
  21.     /* If no char's, or char's with no width, just reset values    */
  22.     if (!outpoint || !glen)
  23.      {newoutline();    return;}
  24.  
  25.     /* If break, print it all        */
  26.     if (brkflag || !co || nc) {gstr(); return;}
  27.  
  28.     /* Determine how much to print        */
  29.         /* later when move unprinted stuff back, stop here */
  30.     endovr = outpoint;
  31.         /* go back and find first preceding word break    */
  32.     while (outpoint && (c = outbuf[--outpoint]) != ' '
  33.             && c != SOFTHY)
  34.         glen -= widbuf[outpoint];
  35.  
  36.         /* save in case can't find any word break    */
  37.     savlen = glen;
  38.         /* later when move unprinted stuff back, start here */
  39.     begovr = outpoint + 1;
  40.         /* trim off interword spaces            */
  41.     while (outpoint >= 0 && outbuf[outpoint] == ' ')
  42.     {    glen -= widbuf[outpoint];
  43.         outpoint--;
  44.     }
  45.  
  46.     /* maybe line all tabs or SP from gotocol        */
  47.     if (!glen || outpoint < 0) {newoutline(); return; }
  48.  
  49.         /* if it was soft hyphen, substitute real one    */
  50.     if (c == SOFTHY)
  51.     {    outbuf[outpoint] = '-';
  52.         /* and correct width                */
  53.         glen += widbuf[outpoint];
  54.     }
  55.  
  56.         /* print up to first interword space or through '-' */ 
  57.     if (outpoint) outpoint++;
  58.         else {outpoint = begovr = endovr - 1; glen = savlen;}
  59.  
  60. /* debug    */
  61. putchar('\n');
  62.  
  63.     /* Print it        */
  64.     gstr();
  65.  
  66.     /* Move back remainder    */
  67.     while (begovr < endovr)
  68.       {    glen += widbuf[outpoint] = widbuf[begovr];
  69.         attrbuf[outpoint] = attrbuf[begovr];
  70.           outbuf[outpoint++] = outbuf[begovr++];
  71.       }
  72. }            `
  73.  
  74. prtsbuf::
  75.     push    b
  76. ;BC not used yet
  77.  
  78. ;    /* If no char's, or char's with no width, just reset values    */
  79. ;    if (!outpoint || !glen)
  80. ;     {newoutline();    return;}
  81.  
  82.  
  83.     lhld    outpoint
  84.     mov    a,h
  85.     ora    l
  86.     jz    .pb9    ;added 1/12/85
  87. ;;    jz    .pb1
  88.  
  89.     lhld    glen
  90.     mov    a,h
  91.     ora    l
  92.     jz    .pb9    ;added 1/12/85 (.pb9 = .pb1)
  93. ;;    jnz    .pb2
  94. ;;
  95. ;;.pb1:    call    newoutline##
  96. ;;    pop    b
  97. ;;    ret
  98. ;
  99. ;    /* If break, print it all        */
  100. ;    if (brkflag || !co || nc) {gstr(); return;}
  101. .pb2:    lda    brkflag
  102.     ora    a
  103.     jnz    .pb3
  104.  
  105.     lda    co
  106.     ora    a
  107.     jz    .pb3
  108.  
  109.     lda    nc
  110.     ora    a
  111.     jz    .pb4
  112.  
  113. .pb3:    call    gstr##
  114.     pop    b
  115.     ret
  116. ;
  117. ;    /* Determine how much to print        */
  118. ;        /* later when move unprinted stuff back, stop here */
  119. ;    endovr = outpoint;
  120. .pb4:
  121. ;[Save original glen here in case justify wants a tight line]
  122.     lhld    glen
  123.     shld    biglen
  124.  
  125.     lhld    outpoint
  126.     shld    endovr
  127. ;no before break spaces yet
  128.     lxi    h,0
  129.     shld    jbspaces
  130.     shld    jblen
  131.  
  132.  
  133. ;        /* go back and find first preceding word break    */
  134. ;    while (outpoint && (c = outbuf[--outpoint]) != ' '
  135. ;            && c != SOFTHY)
  136. ;        glen -= widbuf[outpoint];
  137. .pb5:
  138.     lhld    outpoint
  139.     mov    a,h
  140.     ora    l
  141.     jz    .pb6
  142.  
  143.     lhld    outbuf
  144.     xchg
  145.     lhld    outpoint
  146.     dcx    h
  147.     shld    outpoint
  148.     dad    d
  149.  
  150.     mov    a,m
  151.     sta    jbchar
  152.  
  153.     cpi    ' '
  154.     jz    .pb6
  155.  
  156.     cpi    SOFTHY
  157.     jz    .pb6
  158.  
  159.     lhld    widbuf
  160.     xchg
  161.     lhld    outpoint
  162.     dad    h
  163.     dad    d
  164.  
  165.     mov    e,m
  166.     inx    h
  167.     mov    d,m
  168.     call    cmd
  169.     lhld    glen
  170.     dad    d
  171.     shld    glen
  172.     jmp    .pb5
  173. ;
  174. ;        /* save in case can't find any word break    */
  175. ;    savlen = glen;
  176. .pb6:    lhld    glen
  177.     shld    savlen
  178.  
  179. ;        /* later when move unprinted stuff back, start here */
  180. ;    begovr = outpoint + 1;
  181.     lhld    outpoint
  182.     inx    h
  183.     shld    begovr
  184.  
  185. ;        /* trim off interword spaces            */
  186. ;    while (outpoint >= 0 && outbuf[outpoint] == ' ')
  187. ;    {    glen -= widbuf[outpoint];
  188. ;        outpoint--;
  189. ;    }
  190. .pb7:    lhld    outpoint
  191.     mov    a,h
  192.     ral
  193.     jc    .pb8
  194.  
  195.     xchg
  196.     lhld    outbuf
  197.     dad    d
  198.     mov    a,m
  199.     cpi    ' '
  200.     jnz    .pb8
  201.  
  202.     lxi    h,jbspaces
  203.     inr    m
  204.  
  205.     lhld    widbuf
  206.     xchg
  207.     lhld    outpoint
  208.     dad    h
  209.     dad    d
  210.     mov    e,m
  211.     inx    h
  212.     mov    d,m
  213.  
  214.     lhld    jblen
  215.     dad    d
  216.     shld    jblen
  217.  
  218. ;now wait and subtract total jblen
  219. ;    call    cmd
  220. ;    lhld    glen
  221. ;    dad    d
  222. ;    shld    glen
  223.  
  224.     lhld    outpoint
  225.     dcx    h
  226.     shld    outpoint
  227.     jmp    .pb7
  228.  
  229. ;
  230. ;    /* maybe line all tabs or SP from gotocol        */
  231. ;    if (!glen || outpoint < 0) {newoutline(); return; }
  232. .pb8:
  233. ;now first update glen
  234.     lhld    jblen
  235.     call    cmh
  236.     xchg
  237.     lhld    glen
  238.     dad    d
  239.     shld    glen
  240.     mov    a,h
  241.     ora    l
  242.     jz    .pb9
  243.  
  244.     lhld    outpoint
  245.     mov    a,h
  246.     ral
  247.     jnc    .pb10
  248.  
  249. .pb9:    call    newoutline##
  250.     pop    b
  251.     ret
  252. ;
  253. ;        /* if it was soft hyphen, substitute real one    */
  254. ;    if (c == SOFTHY)
  255. .pb10:
  256.     lda    jbchar
  257.     cpi    SOFTHY
  258.     jnz    .pb11
  259.  
  260. ;    {    outbuf[outpoint] = '-';
  261. ;        /* and correct width                */
  262. ;        glen += widbuf[outpoint];
  263. ;    }
  264.     lhld    outbuf
  265.     xchg
  266.     lhld    outpoint
  267.     dad    d
  268. ;but if there's already a real hyphen before it, just leave the soft one
  269.     dcx    h
  270.     mov    a,m
  271.     inx    h
  272.     cpi    '-'
  273.     jz    .pb11
  274.  
  275.     mvi    m,'-'
  276.  
  277.     lhld    widbuf
  278.     xchg
  279.     lhld    outpoint
  280.     dad    h
  281.     dad    d
  282.     mov    e,m
  283.     inx    h
  284.     mov    d,m
  285.  
  286.     lhld    glen
  287.     dad    d
  288.     shld    glen
  289. ;
  290. ;        /* print up to first interword space or through '-' */ 
  291. ;    if (outpoint) outpoint++;
  292. .pb11:    lhld    outpoint
  293.     mov    a,h
  294.     ora    l
  295.     jz    .pb12
  296.  
  297.     inx    h
  298.     shld    outpoint
  299.     jmp    .pb13
  300.  
  301. ;        else {outpoint = begovr = endovr - 1; glen = savlen;}
  302. .pb12:    lhld    endovr
  303.     dcx    h
  304.     shld    begovr
  305.     shld    outpoint
  306.  
  307.     lhld    savlen
  308.     shld    glen
  309.  
  310. ;
  311. ;/* debug    */
  312. ;putchar('\n');
  313. .pb13:
  314. ;(now gstr puts nl)
  315.  
  316.  
  317. ;    /* Print it        */
  318. ;    gstr();
  319.     call    gstr##
  320. ;
  321. ;    /* Move back remainder    */
  322. ;    while (begovr < endovr)
  323. .pb14:    lhld    begovr
  324.     xchg
  325.     lhld    endovr
  326.     call    albu    ;(was albs)
  327.     jnc    .pb16
  328.  
  329. ;      {    glen += widbuf[outpoint] = widbuf[begovr];
  330.  
  331.     lhld    widbuf
  332.     xchg
  333.     lhld    outpoint
  334.     dad    h
  335.     dad    d
  336.     push    h
  337.  
  338.     lhld    widbuf
  339.     xchg
  340.     lhld    begovr
  341.     dad    h
  342.     dad    d
  343.     mov    e,m
  344.     inx    h
  345.     mov    d,m
  346.  
  347.     pop    h
  348.     mov    m,e
  349.     inx    h
  350.     mov    m,d
  351.  
  352.     lhld    glen
  353.     dad    d
  354.     shld    glen
  355.  
  356. ;        attrbuf[outpoint] = attrbuf[begovr];
  357.     lhld    attrbuf
  358.     xchg
  359.     lhld    outpoint
  360.     dad    h
  361.     dad    d
  362.     push    h
  363.  
  364.     lhld    attrbuf
  365.     xchg
  366.     lhld    begovr
  367.     dad    h
  368.     dad    d
  369.     mov    e,m
  370.     inx    h
  371.     mov    d,m
  372.  
  373.     pop    h
  374.     mov    m,e
  375.     inx    h
  376.     mov    m,d
  377.  
  378. ;          outbuf[outpoint++] = outbuf[begovr++];
  379. ;      }
  380.     lhld    outbuf
  381.     xchg
  382.     lhld    outpoint
  383.     inx    h
  384.     shld    outpoint
  385.     dcx    h
  386.  
  387.     dad    d
  388.     push    h
  389.  
  390.     lhld    outbuf
  391.     xchg
  392.     lhld    begovr
  393.     inx    h
  394.     shld    begovr
  395.     dcx    h
  396.     dad    d
  397.     mov    e,m
  398.     pop    h
  399.     mov    m,e
  400.     jmp    .pb14
  401.  
  402. ;}
  403. .pb16:
  404.     pop    b
  405.     ret
  406.  
  407. savlen:    dw    0
  408.  
  409. ;length of line if made tight:
  410. biglen:        dw    0
  411. ;number of spaces before the break word:
  412. jbspaces:    dw    0
  413. ;length of these spaces
  414. jblen:        dw    0
  415. ;char that made the break
  416. jbchar:        db    0
  417.  
  418.  
  419.  
  420. ;/********************************************************/
  421. ;/* Add enough width to the spaces to line up on right    */
  422. ;/********************************************************/
  423. ;    int jui, juspcs, cepos, rapos, adds0, adds1, addsb;
  424. ;    char juc;
  425. ;
  426.  
  427. ;    FUNCTION    JUSTIFY
  428. JUSTIFY::
  429.  
  430. ;justify()
  431. ;{    /*int i, spcs, cepos, rapos, adds0, adds1, addsb;
  432. ;    char c;*/
  433. ;
  434.  
  435.  
  436.     push    b
  437.  
  438.     lxi    h,jxit
  439.     push    h
  440. ;    /* this should not be necessary, but it is        */
  441. ;    while (outpoint && outbuf[outpoint-1] == ' ')
  442. trimsp:
  443.     lhld    outpoint
  444.     mov    a,h
  445.     ora    l
  446.     rz
  447.  
  448.     XCHG
  449.     LHLD    outbuf
  450.     DCX    D
  451.     dad    d
  452.     MOV    A,M
  453.     cpi    ' '
  454.     jnz    ra_ce
  455.  
  456. ;    {    outpoint--;
  457.  
  458.     XCHG
  459.     shld    outpoint
  460. ;        glen -= widbuf[outpoint];
  461. ;    }
  462.  
  463.     call    widget
  464.  
  465.     call    cmd
  466.     LHLD    GLEN
  467.     dad    d
  468.     shld    glen
  469.     jmp    trimsp
  470.  
  471. ;/* (should go up past initial "gotocol" space, here) */
  472.  
  473. ;    cepos = rapos = -1;
  474.  
  475. ra_ce:
  476.     lxi    h,-1
  477.     shld    rapos
  478.     shld    cepos
  479. ;    for (jui = 0, juspcs = 0; jui < outpoint; jui++)
  480.  
  481.     LHLD    OUTBUF
  482. ;indexing outbuf removed from loop below
  483.     XCHG
  484. ;keep it in DE
  485.  
  486.     LHLD    OUTPOINT
  487.     MOV    B,H
  488.     MOV    C,L
  489. ;keep outpoint for comparison in BC
  490.  
  491.     lxi    h,0
  492.     shld    tswidth
  493. ;jui kept in HL
  494.     shld    juspcs
  495.     shld    jstrt
  496.  
  497. cntsp:
  498. ; outpoint is in BC and jui in HL,
  499. ; and we don't need a signed comparison.
  500.     MOV    A,H
  501.     CMP    B
  502.     JNZ    ps.c1
  503.     MOV    A,L
  504.     CMP    C
  505. ps.c1:    jnc    ce_chk
  506. ;    {    if ((juc = outbuf[jui]) == ' ') juspcs++;
  507.  
  508.     LDAX    D
  509.  
  510. ;char just kept in A, so juc store not necessary
  511.     cpi    ' '
  512.     jnz    pr.c2
  513.     PUSH    H    ;save jui
  514. ;and save ptr outbuf
  515.     push    d
  516. ;get widbuf[HL=jui] in DE
  517.     call    widget
  518.  
  519. ;update total of spaces width
  520.     lhld    tswidth
  521.     dad    d
  522.     shld    tswidth
  523.  
  524.     pop    d
  525.     lxi    h,juspcs
  526.     inr    m
  527.     POP    H
  528. ;    jmp    cnxt
  529. ;        else if (juc == CEFLAG) cepos = jui;
  530.  
  531. pr.c2:
  532.     cpi    CEFLAG    ;90H
  533.     jnz    c3
  534.     shld    cepos
  535. ;    jmp    cnxt jump anyway below
  536.  
  537. ;        else if (juc == RAFLAG) rapos = jui;
  538. c3:
  539.     cpi    RAFLAG    ;91H
  540.     jnz    c4
  541.     push    h
  542.     lhld    rapos
  543.     inx    h
  544.     mov    a,h
  545.     ora    l
  546.     jz    c3.1
  547.     dcx    h
  548.     shld    cepos
  549. c3.1:    pop    h
  550.     shld    rapos
  551.     jmp    cnxt
  552. ;    }
  553.  
  554. ;
  555. c4:    cpi    SPFLAG
  556.     jnz    cnxt
  557.     push    h
  558.     inx    h
  559.     shld    jstrt
  560.     lxi    h,0
  561.     shld    juspcs
  562.     pop    h
  563. cnxt:
  564.     inx    h    ;next jui
  565.     INX    D    ;also next outbuf index
  566.     jmp    cntsp
  567.  
  568.  
  569. ;
  570. ;    if (cepos >= 0 || rapos >= 0)
  571.  
  572. ce_chk:
  573.     LDA    CEPOS+1
  574.     ral
  575.     jnc    addsinit
  576.     LDA    RAPOS+1
  577.     ral
  578.     jc    chkbrk
  579.  
  580.  
  581. ;    {    for (jui = 0, adds0 = adds1 = glen; jui < outpoint; jui++)
  582.  
  583. addsinit:
  584.     lhld    glen
  585.     shld    adds1
  586.     shld    adds0
  587.     lxi    h,0
  588. ;    shld    jui
  589.  
  590. adnxt:
  591.     shld    jui
  592. ;    lhld    jui
  593.     MOV    A,H
  594.     CMP    B
  595.     JNZ    ad1
  596.     MOV    A,L
  597.     CMP    C
  598. ad1:    jnc    doce
  599.  
  600. ;        {    if (jui <= cepos) adds0 -= widbuf[jui];
  601. ;    lhld    jui
  602. ;jui to DE
  603.     xchg
  604.     lhld    cepos
  605.     call    agbs
  606.     jc    ad2
  607.  
  608.     lhld    jui
  609.     call    widget
  610.     CALL    CMD
  611.  
  612.     LHLD    ADDS0
  613.     dad    d
  614.     shld    adds0
  615.  
  616. ;            if (jui <= rapos) adds1 -= widbuf[jui];
  617.  
  618. ad2:
  619.     lhld    jui
  620.     XCHG
  621.     lhld    rapos
  622.     call    agbs
  623.     jc    ad3
  624.  
  625.     lhld    jui
  626.     call    widget
  627.     CALL    CMD
  628.  
  629.     LHLD    ADDS1
  630.     dad    d
  631.     shld    adds1
  632.  
  633. ad3:    lhld    jui
  634.     inx    h
  635. ;    shld    jui
  636.     jmp    adnxt
  637. ;        }
  638.  
  639. ;
  640. ;        if (cepos >= 0)
  641.  
  642. doce:
  643.     LDA    CEPOS+1
  644.     ral
  645.     jc    dora
  646. ;        {    addsb = glen - adds0;
  647.  
  648.     lhld    adds0
  649.     call    cmh
  650.     XCHG
  651.     LHLD    GLEN
  652.     dad    d
  653.     shld    addsb
  654. ;            if (rapos >= 0) adds0 -= adds1 + widbuf[rapos];
  655.     LDA    RAPOS+1
  656.     ral
  657.     jc    ce1
  658.  
  659. ;    lhld    rapos
  660. ;    call    widget
  661.     LHLD    ADDS1
  662. ;    dad    d
  663.     call    cmh
  664.  
  665.     XCHG
  666.     LHLD    ADDS0
  667.     dad    d
  668.     shld    adds0
  669. ;            outbuf[cepos] = ' ';
  670. ce1:
  671.     lhld    cepos
  672.     XCHG
  673.     LHLD    OUTBUF
  674.     dad    d
  675.     mvi    m,' '
  676. ;            if (adds0 > 0) adds0 =
  677. ;                ((llength + ir  - lindent[cc]
  678. ;                     - adds0) >> 1) - addsb;
  679.     lhld    adds0
  680.     lxi    d,-1
  681.     dad    d
  682.     mov    a,h
  683.     ral
  684.     lxi    h,0
  685.     jc    ce2
  686.  
  687. ;lindent[cc]
  688.     call    getlindent
  689.  
  690. ;+adds0
  691.     lhld    adds0
  692.     dad    d
  693.     call    cmh
  694. ;-(lindent[cc] + adds0)
  695.  
  696.     xchg
  697.     lhld    llength
  698.     dad    d
  699.     xchg
  700.     lhld    ir
  701.     dad    d
  702.  
  703.     lxi    d,1
  704.     call    shlrbe
  705.     xchg
  706.     lhld    addsb
  707.     call    cmh
  708.     dad    d
  709. ;                else adds0 = 0;
  710. ce2:
  711.     shld    adds0
  712. ;            widbuf[cepos] += adds0;
  713.  
  714.     lhld    widbuf
  715.     xchg
  716.     lhld    cepos
  717.     dad    h
  718.     dad    d
  719.     push    h
  720.  
  721.     MOV    E,M
  722.     inx    h
  723.     MOV    D,M
  724.     lhld    adds0
  725.     dad    d
  726.     xchg
  727.     pop    h
  728.     mov    m,e
  729.     inx    h
  730.     mov    m,d
  731. ;            glen += adds0;
  732.     lhld    glen
  733.     XCHG
  734.     lhld    adds0
  735.     dad    d
  736.     shld    glen
  737. ;        }
  738. ;
  739.  
  740. ;        if (rapos >= 0)
  741.  
  742. dora:
  743.     LDA    RAPOS+1
  744.     ral
  745.     rc
  746. ;        {    addsb = glen - adds1;
  747.  
  748.     lhld    adds1
  749.     call    cmh
  750.     XCHG
  751.     LHLD    GLEN
  752.     dad    d
  753.     shld    addsb
  754. ;            outbuf[rapos] = ' ';
  755.     lhld    rapos
  756.     XCHG
  757.     LHLD    OUTBUF
  758.     dad    d
  759.     mvi    m,' '
  760. ;            adds0 = llength + ir  - lindent[cc]
  761. ;                     - adds1 - addsb;
  762.  
  763. ;-(lindent[cc] + adds1 + addsb)
  764.     call    getlindent
  765.  
  766.     lhld    adds1
  767.     dad    d
  768.     xchg
  769.     lhld    addsb
  770.     dad    d
  771.     call    cmh
  772.  
  773.     xchg
  774.     lhld    llength
  775.     dad    d
  776.     xchg
  777.     lhld    ir
  778.     dad    d
  779.  
  780.     shld    adds0
  781. ;            widbuf[rapos] += adds0;
  782.     lhld    widbuf
  783.     xchg
  784.     lhld    rapos
  785.     dad    h
  786.     dad    d
  787.     push    h
  788.  
  789.     MOV    E,M
  790.     inx    h
  791.     MOV    D,M
  792.     lhld    adds0
  793.     dad    d
  794.     xchg
  795.     pop    h
  796.     mov    m,e
  797.     inx    h
  798.     mov    m,d
  799. ;            glen += adds0;
  800. ;;    lhld    glen
  801. ;;    XCHG
  802. ;;    lhld    adds0
  803. ;;    dad    d
  804. ;;    shld    glen
  805.  
  806.     lhld    adds0
  807.     xchg
  808.     lhld    glen
  809.     dad    d
  810.     shld    glen
  811.  
  812. ;fill in right endpoint of rule #1
  813. ; first, is it defined as a horizontal?
  814.     lhld    rulist+6
  815.     mov    a,h
  816.     ora    l
  817. ; if not, forget it
  818.     rz
  819.  
  820.     lhld    rulist+4
  821.     dad    d
  822.     shld    rulist+6
  823.  
  824. ;        } 
  825.  
  826. ;        return;
  827. ;    }
  828.     ret
  829.  
  830.  
  831. ;    if (brkflag || !ju) return;
  832.  
  833. chkbrk:
  834.     LDA    BRKFLAG
  835.     ora    a
  836.     rnz
  837.  
  838.     LDA    JU
  839.     ORA    A
  840.     rz
  841.  
  842. ;    if ((adds1 = llength - (glen + lindent[cc])) <= 0) return;
  843.  
  844. ;How much do we have to loosen it? (-> adds1)
  845.  
  846. totsp:
  847.  
  848. ;note whether last char in break word is italic
  849.     lhld    attrbuf
  850.     xchg
  851.     lhld    endovr
  852.     dcx    h
  853.     dad    h
  854.     dad    d
  855. ;;    mov    a,m
  856. ;;    ani    ITALIC
  857. ;;    jz    totsp.1
  858.     mov    e,m
  859.     inx    h
  860.     mov    d,m
  861.     xchg
  862.     call    endcorr
  863.     lhld    biglen
  864. ;;    lxi    d,8
  865.     dad    d
  866.     shld    biglen
  867. totsp.1:
  868.  
  869. ;note whether last char in line is italic
  870.     lhld    attrbuf
  871.     xchg
  872.     lhld    outpoint
  873.     dcx    h
  874.     dad    h
  875.     dad    d
  876.     mov    a,m
  877.     ani    ITALIC
  878.     jz    totsp.2
  879.     lhld    glen
  880.     lxi    d,8
  881.     dad    d
  882.     shld    glen
  883. totsp.2:
  884.  
  885. ;calculate total width of spaces for tight line
  886.     lhld    tswidth
  887.     xchg
  888.     lhld    jblen
  889.     dad    d
  890.     shld    ttswidth
  891.  
  892.  
  893. ;overage of tight line is biglen+lindent-llength
  894.     call    getlindent
  895.     lhld    biglen
  896.     dad    d
  897.  
  898.     xchg
  899.     lhld    llength
  900.     call    cmh
  901.     dad    d
  902.  
  903.     shld    subs1
  904.  
  905. ;the demispace for a tight line is (subs1*256)/ttswidth,
  906. ; or infinity if not conatenating, or subs1 >= ttswidth
  907. ;(really should prevent tightening to less than a point per space)
  908.  
  909.     lda    co
  910.     ora    a
  911.     jz    .td0
  912.  
  913.     xchg
  914. ;(+ tswidth -1?)
  915.     lhld    ttswidth
  916.     xchg
  917.     call    agbs
  918.     jc    .td1
  919. .td0:    lxi    h,-1
  920.     jmp    .td2
  921. .td1:    push    d
  922.     mov    d,l
  923.     mvi    e,0
  924. ;now DE = (subs1+127)*256
  925.     pop    h
  926.     call    usdiv    ;DE/HL
  927.  
  928.     mov    d,h
  929.     mov    e,l
  930.     lda    val + 54*('L'-'@') + 2*('F'-'@')
  931.     dcr    a
  932.     dcr    a
  933. ;if lf is 0 or 1, no preference
  934.     jm    .td2
  935. ;but if it's 2, loosening is twice as good, etc.
  936. .td1.1:    dad    d
  937.     dcr    a
  938.     jp    .td1.1
  939. .td2:    shld    tdemi
  940.  
  941.  
  942.     call    setadds
  943. ;(adds1 returned in HL)
  944.  
  945. ;If 0, it's perfect (should not be possible to have
  946. ; negative looseness here, but you never can tell)
  947.     dcx    h
  948.     mov    a,h
  949.     ora    a
  950.     rm
  951.  
  952. ;now the demispace for a loose line
  953.     inx    h
  954. ;(+ (tswidth-1) ?)
  955.     mov    d,l
  956.     mvi    e,0
  957.     lhld    tswidth
  958.     call    usdiv
  959.     shld    ldemi
  960.  
  961.  
  962. ;    if (!juspcs) return;
  963. ;We need some blanks to work with
  964.     lhld    juspcs
  965.     mov    a,h
  966.     ora    l
  967.     rz
  968.  
  969. ;decide now if want a tight line
  970.  
  971. ;tight is not feasible if demispace > 255 (or infinite)
  972.     lhld    tdemi
  973.     mov    a,h
  974.     ora    a
  975.     jnz    .jloose
  976. ;tightening is 4 times worse than loosening
  977. ;(yes, but do this above)
  978. ;;    dad    h
  979. ;;    dad    h
  980.     xchg
  981.     lhld    ldemi
  982. ;which requires the least adjustment: tightening or loosening?
  983.     call    albu
  984.     jnc    .jloose
  985. ;so we tighten -- use the break word
  986.     lhld    endovr
  987.     shld    outpoint
  988. ;if we broke at a soft hyphen, change the real hyphen put in
  989. ; by prtsbuf back to a soft hyphen
  990.     dcx    h
  991.     lda    jbchar
  992.     cpi    SOFTHY
  993.     jnz    $+4
  994.     mov    m,a
  995. ;tell prtsbuf not to put the break word in the next line
  996.     lxi    h,0
  997.     shld    endovr
  998. ;length includes break word
  999.     lhld    biglen
  1000.     shld    glen
  1001. ;use the subtract routine to adjust the spaces
  1002.     lxi    h,sbjspp
  1003.     jmp    $+6
  1004.  
  1005. .jloose:
  1006. ;do proportional loosening
  1007.     lxi    h,adjspp
  1008.     shld    _chspfu
  1009.     call    chgsp
  1010.  
  1011. ;smooth out right edge with further adjustments to space widths
  1012. .jsmooth:
  1013.     call    setadds
  1014.     mov    a,h
  1015.     ora    l
  1016.     rz
  1017.     mov    a,h
  1018.     ora    a
  1019. ;did we add too much?
  1020.     lxi    h,lesssp
  1021.     jm    $+6
  1022. ;here we didn't add enough
  1023.     lxi    h,moresp
  1024.     shld    _chspfu
  1025.     call    chgsp
  1026. ;???    ret
  1027.     jmp    .jsmooth
  1028.  
  1029. jxit:
  1030.     pop    b
  1031.     ret
  1032.  
  1033.  
  1034. chgsp:
  1035.     lhld    jstrt
  1036.     shld    jui
  1037. addsp:
  1038. ;    lhld    jui (HL always has jui here)
  1039.     xchg
  1040.     lhld    outpoint
  1041.     call    albs
  1042.     rnc
  1043. ;        if (outbuf[jui] == ' ')
  1044.  
  1045.     lhld    outbuf
  1046.     xchg
  1047.     lhld    jui
  1048.     dad    d
  1049.     mov    a,m
  1050.     cpi    ' '
  1051.     jnz    nxadd
  1052.  
  1053. ;        {    widbuf[jui] += adds0;
  1054.  
  1055.     lhld    widbuf
  1056.     xchg
  1057.     lhld    jui
  1058.     dad    h
  1059.     dad    d
  1060.     push    h
  1061.  
  1062.     mov    e,m
  1063.     inx    h
  1064.     mov    d,m
  1065.  
  1066. _chspfu    equ    $+1
  1067.     call    adjspp
  1068.  
  1069. ;and store it
  1070.     pop    h
  1071.     mov    m,e
  1072.     inx    h
  1073.     mov    m,d
  1074.  
  1075. ;        };
  1076.  
  1077. nxadd:
  1078.     lhld    jui
  1079.     inx    h
  1080.     shld    jui
  1081.     jmp    addsp
  1082. ;}
  1083.  
  1084. lesssp:
  1085. ;on entry DE = present width of space
  1086.     lhld    adds1
  1087. ;adds1 is <= 0
  1088.     mov    a,h
  1089.     ora    l
  1090. ;take away a dot, if adds1 not zero yet
  1091.     rz
  1092.     inx    h
  1093.     shld    adds1
  1094.     lhld    glen
  1095.     dcx    h
  1096.     shld    glen
  1097.     dcx    d
  1098.     ret    
  1099.  
  1100.  
  1101. moresp:
  1102. ;on entry DE = present width of space
  1103.     lhld    adds1
  1104. ;adds1 is >= 0
  1105.     mov    a,h
  1106.     ora    l
  1107. ;add a dot, if adds1 not zero yet
  1108.     rz
  1109.     dcx    h
  1110.     shld    adds1
  1111.     lhld    glen
  1112.     inx    h
  1113.     shld    glen
  1114.     inx    d
  1115.     ret    
  1116.  
  1117.  
  1118.  
  1119.  
  1120. adjspp:
  1121. ;save present width
  1122.     push    d
  1123.  
  1124. ;additional is (present * demispace)/256
  1125.     lhld    ldemi
  1126.     call    usmul
  1127.     lxi    d,127
  1128.     dad    d
  1129.     mov    e,h
  1130.     mvi    d,0
  1131. .adjs1:
  1132. ;keep track of glen
  1133.     lhld    glen
  1134.     dad    d
  1135.     shld    glen
  1136.  
  1137. ;add it in
  1138.     pop    h
  1139.     dad    d
  1140. ;return new width in DE
  1141.     xchg
  1142.     ret
  1143.  
  1144.  
  1145. sbjspp:
  1146. ;save present width
  1147.     push    d
  1148.  
  1149. ;decrement is (present * demispace)/256
  1150.     lhld    tdemi
  1151.     call    usmul
  1152.     lxi    d,127
  1153.     dad    d
  1154.     mov    e,h
  1155.     mvi    d,0
  1156.     call    cmd
  1157. ;rest is same as for loosening
  1158.     jmp    .adjs1
  1159.  
  1160.  
  1161.  
  1162. getlindent::
  1163.     lhld    cc
  1164.     dad    h
  1165.     lxi    d,lindent
  1166.     dad    d
  1167.     mov    e,m
  1168.     inx    h
  1169.     mov    d,m
  1170.     ret
  1171.  
  1172. ;receive mode in HL
  1173. ;return width correction for italic or bent in DE
  1174. endcorr::
  1175.     lxi    d,0
  1176.     mov    a,l
  1177.     ani    ITALIC
  1178.     jz    $+5
  1179.     mvi    e,8
  1180.     mov    a,h
  1181.     ani    BENT shr 8
  1182.     rz
  1183.     push    d
  1184.     lda    be
  1185.     push    psw
  1186.     call    bntcorr
  1187.     pop    psw
  1188.     pop    d
  1189.     dad    d
  1190.     push    d
  1191.     rar
  1192.     rar
  1193.     rar
  1194.     rar
  1195.     call    bntcorr
  1196.     pop    d
  1197.     dad    d
  1198.     xchg
  1199.     ret
  1200.  
  1201. bntcorr:
  1202.     lxi    h,0
  1203.     ani    0fh
  1204.     rz
  1205.     cma
  1206.     ani    0fh
  1207.     inr    a
  1208.     mov    l,a
  1209.     lxi    d,46
  1210.     jmp    usdiv
  1211.  
  1212. widget:    dad    h
  1213.     xchg
  1214.     lhld    widbuf
  1215.     dad    d
  1216.     mov    e,m
  1217.     inx    h
  1218.     mov    d,m
  1219.     ret
  1220.  
  1221. setadds:
  1222.     call    getlindent
  1223.     lhld    glen
  1224.     dad    d
  1225.     call    cmh
  1226.     xchg
  1227.     lhld    llength
  1228.     dad    d
  1229.     shld    adds1
  1230.     ret
  1231.  
  1232.  
  1233. adds0:    dw    0
  1234. adds1:    dw    0
  1235. addsb:    dw    0
  1236. juc:    dw    0
  1237. jui:    dw    0
  1238. juspcs:    dw    0
  1239. cepos:    dw    0
  1240. rapos:    dw    0
  1241. jstrt:    dw    0
  1242.  
  1243. ;total width of spaces in loose line (before justification)
  1244. tswidth:    dw    0
  1245. ;total width of spaces in tight line
  1246. ttswidth:    dw    0
  1247. ;width that has to be removed from a tight line
  1248. subs1:        dw    0
  1249.  
  1250. ldemi:        dw    0
  1251. tdemi:        dw    0
  1252.  
  1253.     END
  1254.  
  1255.