Subversion Repositories Kolibri OS

Rev

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