Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;;  Copyright (C) KolibriOS team 2016. All rights reserved.     ;;
  4. ;; Distributed under terms of the GNU General Public License    ;;
  5. ;;                                                              ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7.  
  8. format MS COFF
  9.  
  10. public @EXPORT as 'EXPORTS'
  11.  
  12. include '../../../macros.inc'
  13. include '../../../proc32.inc'
  14.  
  15.  
  16. ; calculate string width in pixels
  17. proc    stringWidth, charQuantity, charHeight
  18.         mov     eax,[charHeight]
  19.         shr     eax,1
  20.         mul     [charQuantity]
  21.         ret
  22. endp
  23.  
  24.  
  25. ; calculate amount of chars that fits given width
  26. proc    charsFit, areaWidth, charHeight
  27.         shr     [charHeight],1
  28.         mov     eax,[areaWidth]
  29.         xor     edx,edx
  30.         div     [charHeight]
  31.         ret
  32. endp
  33.  
  34.  
  35. ; calculate amount of valid chars in UTF-8 string
  36. ; supports zero terminated string (set byteQuantity = -1)
  37. proc    countUTF8, string, byteQuantity
  38.         push    esi
  39.         mov     edx,[byteQuantity]
  40.         inc     edx
  41.         xor     ecx,ecx
  42.         dec     ecx
  43.         mov     esi,[string]
  44. @@:
  45.         inc     ecx
  46.         dec     edx
  47.         jz      .done
  48.         lodsb
  49.         test    al,al
  50.         jz      .done
  51.         jns     @b
  52.         dec     ecx
  53.         shl     al,1
  54.         jns     @b
  55. .next:
  56.         mov     ah,[esi]
  57.         test    ah,ah
  58.         jns     @b
  59.         shl     ah,1
  60.         js      @b
  61.         inc     esi
  62.         dec     edx
  63.         jz      @f
  64.         shl     al,1
  65.         js      .next
  66.         inc     ecx
  67.         jmp     @b
  68. @@:
  69.         inc     ecx
  70. .done:
  71.         mov     eax,ecx
  72.         pop     esi
  73.         ret
  74. endp
  75.  
  76.  
  77. ; draw text on 24bpp or 32bpp image
  78. ; autofits text between 'x' and 'xSize'
  79. proc    drawText, canvas, x, y, string, charQuantity, fontColor, params
  80. ; [canvas]:
  81. ;  xSize        dd  ?
  82. ;  ySize        dd  ?
  83. ;  picture      rb  xSize * ySize * bpp
  84.  
  85. ; fontColor     dd  AARRGGBB
  86. ;  AA = alpha channel   ; 0 = transparent, FF = non transparent
  87.  
  88. ; params        dd  ffeewwhh
  89. ;  hh = char height
  90. ;  ww = char width      ; 0 = auto (proportional)
  91. ;  ee = encoding        ; 1 = cp866, 2 = UTF-16LE, 3 = UTF-8
  92. ;  ff = flags           ; 0001 = bold, 0010 = italic
  93.                         ; 0100 = underline, 1000 = strike-through
  94. ; 00010000 = align right, 00100000 = align center
  95. ; 01000000 = set text area between higher and lower halfs of 'x'
  96. ; 10000000 = 32bpp canvas insted of 24bpp
  97. ; all flags combinable, except align right + align center
  98.  
  99. ; returns: eax = char width (0 = error), ecx = text start X
  100.         pusha
  101.         movzx   eax,byte[params+1]
  102.         test    eax,eax
  103.         jnz     @f
  104.         mov     al ,byte[params]
  105.         shr     al ,1
  106.         mov     byte[params+1],al
  107. @@:
  108.         cmp     al, 22
  109.         jc      @f
  110.         mov     al, 21
  111.         mov     byte[params+1],21
  112. @@:
  113.         inc     [charQuantity]
  114.         mul     [charQuantity]
  115.         mov     ebx,eax
  116.         mov     esi,[canvas]
  117.         mov     esi,[esi]
  118.         test    byte[params+3],64
  119.         jz      .fit
  120.         movzx   eax,word[x]
  121.         movzx   ecx,word[x+2]
  122.         cmp     eax,ecx
  123.         jnc     @f
  124.         xchg    eax,ecx
  125. @@:
  126.         mov     [x],ecx
  127.         cmp     esi,eax
  128.         jc      .fit
  129.         mov     esi,eax
  130. .fit:
  131.         mov     eax,esi
  132.         sub     eax,[x]
  133.         jnc     @f
  134.         popa
  135.         xor     eax,eax
  136.         jmp     .exit
  137. @@:
  138.         cmp     eax,ebx
  139.         jnc     @f
  140.         mov     ebx,eax
  141.         div     [charQuantity]
  142.         mov     byte[params+1],al
  143.         sub     ebx,edx
  144. @@:
  145.         mov     eax,esi
  146.         sub     eax,ebx
  147.         test    byte[params+3],32
  148.         jz      @f
  149.         sub     eax,[x]
  150.         shr     eax,1
  151.         add     [x],eax
  152.         jmp     .ok
  153. @@:
  154.         test    byte[params+3],16
  155.         jz      .ok
  156.         mov     [x],eax
  157. .ok:
  158.         movzx   eax,byte[params+1]
  159.         lea     eax,[eax*2+eax]
  160.         shr     eax,3
  161.         test    byte[params+1],7
  162.         jz      @f
  163.         inc     eax
  164. @@:
  165.         mov     ecx,eax
  166.         push    eax
  167.         shl     eax,3
  168.         mul     [charQuantity]
  169.         shl     ecx,4
  170.         push    ecx
  171.         push    eax
  172.         mul     ecx
  173.         push    eax
  174.         lea     ecx,[eax*4+8]
  175.         mcall   68,12
  176.  
  177.         pop     ecx
  178.         popd    [eax]
  179.         popd    [eax+4]
  180.         push    eax
  181.         lea     edi,[eax+8]
  182.         xor     eax,eax
  183.         rep stosd
  184.         pop     edi
  185.         pop     ecx
  186.         shl     ecx,4
  187.         mov     ch, byte[params+2]
  188.         shl     ecx,22
  189.         shr     ecx,2
  190.         dec     ecx
  191.         bts     ecx,27
  192.         mov     esi,[charQuantity]
  193.         dec     esi
  194.         xor     ebx,ebx
  195.         mcall   4,,,[string]
  196.  
  197.         xor     eax,eax
  198.         mov     ebx,[edi]
  199.         mov     ecx,[edi+4]
  200.         push    edi
  201.         add     edi,8
  202.         test    byte[params+3],1
  203.         jnz     .bold
  204.         movzx   esi,byte[params]
  205. @@:
  206.         pusha
  207.         call    verSub
  208.         popa
  209.         sub     esi,16
  210.         jg      @b
  211.         jmp     @f
  212. .bold:
  213.         imul    ecx,ebx
  214.         dec     eax
  215.         movzx   ebx,byte[params+1]
  216. .loop:
  217.         push    edi
  218.         push    ecx
  219.         call    horAdd
  220.         pop     ecx
  221.         pop     edi
  222.         sub     ebx,8
  223.         jg      .loop
  224. @@:
  225.  
  226.         test    byte[params+3],2
  227.         jz      @f
  228.         mov     edi,[esp]
  229.         mov     ecx,[edi]
  230.         mov     ebx,[edi+4]
  231.         add     edi,8
  232.         mov     esi,edi
  233.         call    italic
  234. @@:
  235.  
  236.         mov     edi,[esp]
  237.         mov     eax,[edi]
  238.         mov     ebx,[edi+4]
  239.         add     edi,8
  240.         mov     esi,edi
  241.         movzx   edx,byte[params]
  242.         call    verScale
  243.  
  244.         mov     eax,[charQuantity]
  245.         mul     byte[params+1]
  246.         mov     esi,[esp]
  247.         mov     edx,[esi]
  248.         add     esi,8
  249.         mov     edi,esi
  250.         mov     ebx,eax
  251.         push    eax
  252.         movzx   eax,byte[params]
  253.         call    ClearType
  254.  
  255.         test    byte[params+3],4
  256.         jz      @f
  257.         movzx   eax,byte[params]
  258.         movzx   esi,byte[params+1]
  259.         mov     ebx,eax
  260.         dec     eax
  261.         call    drawLine
  262. @@:
  263.  
  264.         test    byte[params+3],8
  265.         jz      @f
  266.         movzx   eax,byte[params]
  267.         movzx   esi,byte[params+1]
  268.         mov     ebx,eax
  269.         shr     eax,1
  270.         call    drawLine
  271. @@:
  272.  
  273.         mov     esi,[canvas]
  274.         mov     eax,[esi]
  275.         mul     [y]
  276.         add     eax,[x]
  277.         mov     edi,eax
  278.         mov     eax,[esi]
  279.         pop     ecx
  280.         sub     eax,ecx
  281.         mov     ebx,[fontColor]
  282.         movzx   edx,byte[params]
  283.         test    byte[params+3],128
  284.         mov     ebp,3
  285.         jnz     @f
  286.         lea     edi,[edi*2+edi+8]
  287.         lea     eax,[eax*2+eax]
  288.         jmp     .go
  289. @@:
  290.         lea     edi,[edi*4+8]
  291.         shl     eax,2
  292.         inc     ebp
  293. .go:
  294.         add     edi,esi
  295.         mov     esi,[esp]
  296.         add     esi,8
  297.         call    putOnPicture
  298.  
  299.         pop     ecx
  300.         mcall   68,13
  301.         popa
  302.         movzx   eax,byte[params+1]
  303.         mov     ecx,[x]
  304. .exit:
  305.         ret
  306. endp
  307.  
  308.  
  309. drawLine:
  310.         mov     ecx,[esp+4]
  311.         lea     ecx,[ecx*2+ecx]
  312.         mul     ecx
  313.         lea     esi,[esi*2+esi]
  314.         sub     ecx,esi
  315.         add     esi,ecx
  316.         add     esi,ecx
  317.         shr     ecx,2
  318.         add     eax,[esp+8]
  319.         lea     edi,[eax+8]
  320.         mov     eax,-1
  321. @@:
  322.         push    ecx
  323.         rep stosd
  324.         sub     edi,esi
  325.         pop     ecx
  326.         sub     ebx,16
  327.         jg      @b
  328.         ret
  329.  
  330.  
  331. ; make horizontal lines thinner
  332. ; one color background only
  333. verSub:
  334. ; edi -> buffer (32bpp)
  335. ; eax = background color
  336. ; ebx = width
  337. ; ecx = height
  338.         push    ebp
  339.         mov     edx,ebx
  340.         mov     esi,edi
  341.         mov     ebp,ecx
  342.         shl     ebx,2
  343. .start:
  344.         cmp     [edi],eax
  345.         jnz     @f
  346. .loop:
  347.         add     edi,ebx
  348.         dec     ecx
  349.         jnz     .start
  350.         jmp     .next
  351. @@:
  352.         mov     [edi],eax
  353. @@:
  354.         add     edi,ebx
  355.         dec     ecx
  356.         jz      .next
  357.         cmp     [edi],eax
  358.         jnz     @b
  359.         jmp     .loop
  360. .next:
  361.         add     esi,4
  362.         mov     edi,esi
  363.         mov     ecx,ebp
  364.         dec     edx
  365.         jnz     .start
  366.         pop     ebp
  367.         ret
  368.  
  369.  
  370. ; make vertical lines thicker
  371. horAdd:
  372. ; edi -> buffer (32bpp)
  373. ; eax = font color
  374. ; ecx = total number of pixels
  375.         repnz scasd
  376.         jcxz    .end
  377.         repz  scasd
  378.         mov     [edi-4],eax
  379.         jmp     horAdd
  380. .end:
  381.         ret
  382.  
  383.  
  384. ; esi=edi supported (32bpp)
  385. italic:
  386. ; esi -> source buffer
  387. ; edi -> result buffer
  388. ; ebx = height
  389. ; ecx = width
  390.         shl     ecx,2
  391.         shr     ebx,2
  392.         mov     eax,ecx
  393.         mul     ebx
  394.         shl     eax,2
  395.         add     esi,eax
  396.         add     edi,eax
  397.         dec     ecx
  398.         sub     esi,8
  399.         sub     edi,4
  400.         push    ebx
  401.         std
  402. @@:
  403.         push    ecx
  404.         rep movsd
  405.         pop     ecx
  406.         sub     esi,4
  407.         dec     ebx
  408.         jnz     @b
  409.         pop     ecx
  410.         mov     eax,[edi+4]
  411.         rep stosd
  412.         cld
  413.         ret
  414.  
  415.  
  416. ; vertical downscale
  417. ; white-black-gray only
  418. ; esi=edi supported (32bpp)
  419. verScale:
  420. ; esi -> source buffer
  421. ; edi -> result buffer
  422. ; eax = width
  423. ; ebx = source height
  424. ; edx = result height
  425.         push    ebp
  426.         dec     eax
  427.         shl     eax,2
  428.         push    eax
  429.         add     eax,4
  430.         push    edx
  431.         push    esi
  432.         push    edi
  433.         push    eax
  434.         mov     ecx,edx
  435. .scale:
  436.         mov     al, [esi]
  437.         add     esi,[esp]
  438.         mul     cl
  439.         neg     ecx
  440.         add     ecx,ebx
  441.         mov     ebp,eax
  442.         mov     al, [esi]
  443. @@:
  444.         cmp     edx,ecx
  445.         jnc     @f
  446.         add     esi,[esp]
  447.         mul     dl
  448.         sub     ecx,edx
  449.         add     ebp,eax
  450.         mov     al, [esi]
  451.         jmp     @b
  452. @@:
  453.         mul     cl
  454.         add     eax,ebp
  455.         div     bl
  456.         mov     [edi],al
  457.         mov     [edi+1],al
  458.         mov     [edi+2],al
  459.         add     edi,[esp]
  460.         neg     ecx
  461.         add     ecx,edx
  462.         jnz     @f
  463.         add     ecx,edx
  464.         add     esi,[esp]
  465. @@:
  466.         dec     dword[esp+12]
  467.         jnz     .scale
  468.         mov     edi,[esp+4]
  469.         mov     esi,[esp+8]
  470.         mov     [esp+12],edx
  471.         add     edi,[esp+16]
  472.         add     esi,[esp+16]
  473.         sub     dword[esp+16],4
  474.         jnc     .scale
  475.         add     esp,20
  476.         pop     ebp
  477.         ret
  478.  
  479.  
  480. ; horizontal downscale
  481. ; minimum — x3, maximum — x6
  482. ; white-black-gray only
  483. ; esi=edi supported
  484. ClearType:
  485. ; esi -> source buffer (32bpp)
  486. ; edi -> result buffer (24bpp)
  487. ; eax = height
  488. ; edx = source width
  489. ; ebx = result width
  490.         push    ebp
  491.         lea     ebx,[ebx*2+ebx]
  492.         imul    eax,ebx
  493.         push    eax
  494.         push    edi
  495.         push    eax
  496.         push    edx
  497.         mov     ecx,ebx
  498. .scale:
  499.         movzx   eax,byte[esi]
  500.         add     esi,4
  501.         mul     ecx
  502.         neg     ecx
  503.         add     ecx,[esp]
  504.         mov     ebp,eax
  505.         movzx   eax,byte[esi]
  506.         cmp     ebx,ecx
  507.         jnc     @f
  508.         add     esi,4
  509.         mul     ebx
  510.         sub     ecx,ebx
  511.         add     ebp,eax
  512.         movzx   eax,byte[esi]
  513. @@:
  514.         mul     ecx
  515.         add     eax,ebp
  516.         div     dword[esp]
  517.         stosb
  518.         neg     ecx
  519.         add     ecx,ebx
  520.         jnz     @f
  521.         add     ecx,ebx
  522.         add     esi,4
  523. @@:
  524.         dec     dword[esp+4]
  525.         jnz     .scale
  526.         pop     edi
  527.         pop     edi
  528.         mov     edi,[esp]
  529.         mov     ecx,[esp+4]
  530.         movzx   ebx,byte[edi]
  531.         xor     eax,eax
  532.         dec     ecx
  533. .degradation:
  534.         mov     al, [edi]
  535.         shl     eax,1
  536.         lea     eax,[eax*2+eax]
  537.         lea     edx,[ebx*4+ebx]
  538.         mov     bl, [edi+1]
  539.         add     eax,edx
  540.         lea     edx,[ebx*4+ebx]
  541.         mov     bl, [edi]
  542.         add     eax,edx
  543.         shr     eax,4
  544.         stosb
  545.         dec     ecx
  546.         jnz     .degradation
  547.         pop     edi
  548.         pop     ecx
  549. .colRev:
  550.         mov     al,[edi]
  551.         xchg    al,[edi+2]
  552.         mov     [edi],al
  553.         add     edi,3
  554.         sub     ecx,3
  555.         jnz     .colRev
  556.         pop     ebp
  557.         ret
  558.  
  559.  
  560. ; apply color on font, put font on picture
  561. ; white font on black background only, smoothing allowed
  562. putOnPicture:
  563. ; esi -> font buffer (24bpp)
  564. ; edi -> picture buffer
  565. ; ebx = font color
  566. ; ecx = width
  567. ; edx = height
  568. ; eax = picture buffer line gap in bytes
  569. ; ebp = picture buffer bytes per pixel
  570.         push    edx
  571.         push    eax
  572.         push    ecx
  573.         push    ebp
  574.         xor     eax,eax
  575.         rol     ebx,8
  576.         mov     ebp,ecx
  577. .start:
  578.         cmp     byte[esi], 0
  579.         jz      @f
  580.         mov     al, [esi]
  581.         mul     bl
  582.         mov     al, ah
  583.         shr     ah, 7
  584.         add     al, ah
  585.         mov     cl, 255
  586.         sub     cl, al
  587.         mul     bh
  588.         mov     edx,eax
  589.         mov     al, [edi]
  590.         mul     cl
  591.         add     eax,edx
  592.         mov     al, ah
  593.         shr     ah, 7
  594.         add     al, ah
  595.         mov     [edi],al
  596. @@:
  597.         cmp     byte[esi+1], 0
  598.         jz      @f
  599.         mov     al, [esi+1]
  600.         mul     bl
  601.         mov     al, ah
  602.         shr     ah, 7
  603.         add     al, ah
  604.         mov     cl, 255
  605.         sub     cl, al
  606.         rol     ebx,16
  607.         mul     bl
  608.         rol     ebx,16
  609.         mov     edx,eax
  610.         mov     al, [edi+1]
  611.         mul     cl
  612.         add     eax,edx
  613.         mov     al, ah
  614.         shr     ah, 7
  615.         add     al, ah
  616.         mov     [edi+1],al
  617. @@:
  618.         cmp     byte[esi+2], 0
  619.         jz      @f
  620.         mov     al, [esi+2]
  621.         mul     bl
  622.         mov     al, ah
  623.         shr     ah, 7
  624.         add     al, ah
  625.         mov     cl, 255
  626.         sub     cl, al
  627.         rol     ebx,16
  628.         mul     bh
  629.         rol     ebx,16
  630.         mov     edx,eax
  631.         mov     al, [edi+2]
  632.         mul     cl
  633.         add     eax,edx
  634.         mov     al, ah
  635.         shr     ah, 7
  636.         add     al, ah
  637.         mov     [edi+2],al
  638. @@:
  639.         add     esi,3
  640.         add     edi,[esp]
  641.         dec     ebp
  642.         jnz     .start
  643.         mov     ebp,[esp+4]
  644.         add     edi,[esp+8]
  645.         dec     dword[esp+12]
  646.         jnz     .start
  647.         add     esp,16
  648.         ret
  649.  
  650. align 4
  651. @EXPORT:
  652. export  drawText,       'drawText', \
  653.         countUTF8,      'cntUTF-8', \
  654.         charsFit,       'charsFit', \
  655.         stringWidth,    'strWidth'
  656.