Subversion Repositories Kolibri OS

Rev

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

  1. ; BGIFONT.INC v1.0 beta
  2. ;
  3. ; Written in pure assembler by Ivushkin Andrey aka Willow
  4. ;
  5. ; Created: December 16, 2004
  6. ;
  7. ; Last changed: February 2, 2005
  8. ;
  9. ; Compile with FASM
  10.  
  11. ; BGI constants
  12. BGI_NODRAW   equ 0x10000
  13. BGI_ITALIC   equ 0x20000
  14. BGI_BOLD     equ 0x40000
  15. BGI_HALEFT   equ 0x0
  16. BGI_HARIGHT  equ 0x1000
  17. BGI_HACENTER equ 0x2000
  18. BGI_VABOTTOM equ 0x0
  19. BGI_VATOP    equ 0x4000
  20. BGI_VACENTER equ 0x8000
  21.  
  22. BGI_FREE     equ 0x80000000
  23. BGI_HAMASK   equ 0x3000
  24. BGI_VAMASK   equ 0xc000
  25.  
  26. ; Freetext structure
  27. struc BGIfree FontName,XY,Angle,ScaleX,ScaleY,StrPtr,StrLen,Color,Align
  28. {
  29.     dd FontName ;0
  30.     dd XY           ;4
  31.     dd Angle    ;8
  32.     dd ScaleX   ;12
  33.     dd ScaleY   ;16
  34.     dd StrPtr   ;20
  35.     dd StrLen   ;24
  36.     dd Color    ;28
  37.     dd Align    ;32
  38. }
  39.  
  40. ; font options structure
  41. struc BGIrec FontName,CharsCount,FirstChar,UpperMargin,LowerMargin,\
  42.     Widths,FirstData,EOF,font_data
  43. {
  44.  .FontName     dd ?  ; 0
  45.  .CharsCount   db ?  ; 4
  46.  .FirstChar    db ?  ; 5
  47.  .UpperMargin  db ?  ; 6
  48.  .LowerMargin  db ?  ; 7
  49.  .Widths       dd ?  ; 8
  50.  .FirstData    dd ?  ; 12
  51.  .EOF          dd ?    ; 16
  52.  .font_data    dd ?  ; 20 follows (Offsets)
  53. }
  54.  
  55. macro BGIfont_GetID
  56. {
  57.     call _BGIfont_GetID
  58. }
  59.  
  60. macro BGIfont_Prepare
  61. {
  62.     call _BGIfont_Prepare
  63. }
  64.  
  65. macro BGIfont_Freetext
  66. {
  67.     call _BGIfont_Freetext
  68. }
  69.  
  70. macro BGIfont_Outtext
  71. {
  72.     call _BGIfont_Outtext
  73. }
  74.  
  75. macro _FI name,_size
  76. {
  77.     db name
  78. if BGI_LEVEL eq KERNEL
  79.     dw _size
  80. end if
  81. }
  82.  
  83. BGIfont_names:
  84. _FI 'LCOM',11485   ;7
  85. _FI 'EURO',8117    ;5
  86. _FI 'GOTH',13816   ;6
  87. _FI 'LITT',3596    ;8
  88. _FI 'TRIP',11932   ;14
  89. _FI 'SCRI',8490    ;11
  90. _FI 'SMAL',4162    ;13
  91. _FI 'TSCR',12134   ;15
  92. _FI 'SANS',8453    ;10
  93. _FI 'SIMP',9522    ;12
  94. BGIfont_names_end:
  95.  
  96. macro BGIfont_Init
  97. {
  98. ; in:  ecx - number of fonts to load;
  99. ;      esi-> _FI structure
  100. ;      edi-> where to load
  101.     push edi
  102. if  BGI_LEVEL eq KERNEL
  103.     mov  edi,0x40000
  104. end if
  105.   .nfont:
  106.     mov  edx,[esi]
  107. if  BGI_LEVEL eq KERNEL
  108.     movzx ebx,word[esi+4]
  109.     mov  [BGIfont_Prepare.okflag],'N'
  110. end if
  111.     call _BGIfont_Prepare
  112. if ~ BGI_LEVEL eq KERNEL
  113.     add  esi,4
  114. else
  115.     push esi
  116.     test eax,eax
  117.     jz   .fail
  118.     mov  [BGIfont_Prepare.okflag],'*'
  119.   .fail:
  120.     mov  esi,BGIfont_Prepare.font
  121.     call boot_log
  122.     pop  esi
  123.     add  esi,6
  124. end if
  125.     loop .nfont
  126.     dph2 _BGI_BOLD,300,550
  127. ;    movzx edi,byte[0x40000]
  128.     pop  edi
  129. }
  130.  
  131. BGIfont_get2head:
  132.     shr  ecx,28 ; font #
  133.     sub  ecx,4
  134.     jb   .exit2 ; invalid #
  135.     mov  edi,[BGIfont_Ptr]
  136.     inc  edi
  137.     cmp  cl,[edi-1]
  138.     jae  .exit2 ; # too large
  139.     jecxz .ex
  140.   .fnext:
  141.     mov  edi,[edi+16]
  142.     loop .fnext
  143.     jmp  .ex
  144.   .exit2:
  145.     xor  edi,edi
  146.   .ex:
  147.     ret
  148.  
  149. BGIfont_GetName:
  150. ; in: ecx-fontID;
  151. ; out: edx-font name.
  152.     call BGIfont_get2head
  153.     xor  edx,edx
  154.     test edi,edi
  155.     jz   .ex
  156.     mov  edx,[edi]
  157.   .ex:
  158.     ret
  159.  
  160. macro dps2 _str
  161. {
  162. if ~ BGI_LEVEL eq KERNEL
  163.   if LOAD_MSG eq 1
  164.     dps _str
  165.   end if
  166. else
  167.     pusha
  168.     mov  esi,BGIfont_Prepare.okflag
  169.     mov  byte[esi], _str
  170.     call boot_log
  171.     popa
  172. end if
  173. }
  174.  
  175. macro dph2 num,x,y
  176. {
  177. if  BGI_LEVEL eq KERNEL
  178.     pusha
  179.     mov  eax,0x00080100
  180.     mov  ebx,num
  181.     mov  ecx,x shl 16+y
  182.     mov  edx,0xFF0000
  183.     call display_number
  184.     popa
  185. end if
  186. }
  187.  
  188. _BGIfont_GetID:
  189. ; in:  edx-font name;
  190. ; out: eax-fontID, edi->BGIrec
  191.     push ecx edi
  192.     mov  edi,[BGIfont_Ptr]
  193.     movzx ecx,byte[edi] ; ecx-font count
  194.     mov  eax,ecx
  195.     inc  edi ; edi->FontName
  196.     jecxz .ex
  197.   .fnext:
  198.     cmp  edx,[edi]
  199.     jne  .floop
  200.     sub  eax,ecx
  201.     add  eax,4
  202.     shl  eax,28
  203.     jmp  .ex
  204.   .floop:
  205.     mov  edi,[edi+16]
  206.     loop .fnext
  207.   .num0:
  208.     xor  eax,eax
  209.   .ex:
  210.     pop  edi ecx
  211.     ret
  212.  
  213. _BGIfont_Prepare:
  214. ; in:  edx-font name, edi->pointer to load fonts (fonts_count)
  215. ; out: eax-ID of new font loaded; eax=0 error
  216.     cmp  [BGIfont_Ptr],0
  217.     jne  .already
  218.     mov  [BGIfont_Ptr],edi
  219.   .already:
  220.     pusha
  221.     mov  edi,[BGIfont_Ptr]
  222.     movzx ecx,byte[edi] ; ecx-font count
  223.     mov  eax,ecx
  224.     inc  edi ; edi->FontName
  225.     jecxz .fload
  226.   .fnext:
  227.     cmp  edx,[edi]
  228.     jne  .loop
  229.     sub  eax,ecx
  230.     inc  eax
  231.     jmp  .cr_id
  232.   .loop:
  233.     mov  edi,[edi+16]
  234.     loop .fnext
  235.   .fload:
  236.     mov  dword[.font],edx ; filename
  237.     mov  esi,edi     ; esi->FontName
  238.     mov  [.dest],edi ; ptr to load font
  239. if ~ BGI_LEVEL eq KERNEL
  240.     mov  [.fsize],1
  241.     mov  eax,58
  242.     mov  ebx,.fontinfo
  243.     int  0x40
  244.     test eax,eax
  245.     jnz   .fail
  246.     dps2  '1'
  247.     shr  ebx,9
  248.     inc  ebx
  249.     mov  [.fsize],ebx
  250.     mov  ebx,.fontinfo
  251.     mov  eax,58
  252.     int  0x40        ; ebx - file size
  253. else
  254.     push edi esi edx
  255.     mov  eax,.font
  256.     xor  ebx,ebx
  257.     mov  esi,12
  258.     mov  ecx,ebx
  259.     mov  edx,edi
  260.     call fileread
  261.     pop  edx esi edi
  262.     mov  ebp,edi
  263.     add  ebp,ebx
  264.     cmp  ebp,0x50000
  265.     ja   .fail
  266. end if
  267.     cmp  dword[edi],0x08084b50 ; 'PK',8,8
  268.     jne  .fail
  269.     dps2 '2'
  270.     inc  edi
  271.     mov  eax,26 ; #EOF
  272.     mov  ecx,253
  273.     cld
  274.     repne scasb  ; skip Copyright
  275.     test ecx,ecx
  276.     jz   .fail
  277.     dps2  '3'
  278.     cmp  edx,[edi+2] ; FontName
  279.     jne  .fail
  280.     dps2  '4'
  281.     movzx ecx,word[edi] ; HeaderSize
  282.     sub  ebx,ecx  ; Filesize-Headersize
  283.     movzx eax,word[edi+6] ; FontSize
  284.     cmp  eax,ebx
  285.     jb   .fail    ; file truncated
  286.     add  ecx,[.dest]
  287.     dps2  '5'
  288.     cmp  byte[ecx],'+'  ; ParPrefix
  289.     jne  .fail
  290. ; font is valid, let's fill parameter table
  291.     dps2  '>'
  292.     mov  [esi],edx ; FontName
  293.     mov  edx,eax
  294.     add  eax,ecx
  295.     mov  [esi+16],eax ; Font EOF
  296.     movzx eax,word[ecx+5]
  297.     add  eax,ecx
  298.     mov  [esi+12],eax
  299.     lea  edi,[esi+4]  ; edi->CharsCount
  300.     lea  esi,[ecx+1] ; esi->ParPrefix+1
  301.     xor  eax,eax
  302.     lodsw
  303.     stosb  ; CharsCount
  304.     inc  esi
  305.     movsb  ; FirstChar
  306.     add  esi,3
  307.     lodsw
  308.     stosb  ; UpperMargin
  309.     movsb  ; LowerMargin
  310.     add  esi,5 ; esi->offsets
  311.     mov  eax,[esi]
  312.     push edi ; edi->Widths
  313. ; prepare moving data
  314.     add  edi,12 ; edi->offsets
  315.     lea  ecx,[edx-16]
  316.     rep  movsb
  317.     pop  edi ; edi->Widths
  318.     mov  [edi+8],esi ; EOF
  319. ;    mov  eax,[edi]
  320.     movzx ecx,byte[edi-4] ; CharsCount
  321.     lea  eax,[edi+12+ecx*2] ; eax->widths
  322.     stosd  ; edi->FirstData
  323.     add  eax,ecx
  324.     stosd  ; edi->EOF
  325.     mov  eax,[esp] ; eax->fonts_count
  326.     inc  byte[eax] ; increase font counter
  327.     movzx eax,byte[eax]
  328.   .cr_id:
  329.     add  eax,0x3   ; create unique ID
  330.     shl  eax,28    ; to easy use in color(ecx)
  331.     jmp  .exit
  332.   .fail:
  333.     xor  eax,eax
  334.   .exit:
  335.     mov  [esp+28],eax
  336.     popa
  337.     ret
  338.  
  339. if ~ BGI_LEVEL eq KERNEL
  340. .fontinfo:
  341.         dd 0
  342.         dd 0
  343. .fsize  dd 0
  344. .dest   dd 0
  345.         dd 0x10000
  346.         db BGIFONT_PATH
  347. .font   db 'FONT.CHR',0
  348. else
  349.   .dest   dd 0
  350.   .font   db 'FONT    CHR'
  351.   .okflag db ' ',0
  352. end if
  353.  
  354. BGIfont_Coo:
  355. ; y->word[txt.y1], x->word[txt.x1]
  356.     fild [txt.y1] ;y
  357.     fmul st0,st0; y*y
  358.     fild [txt.x1] ;x
  359.     fmul st0,st0; x*x
  360.     faddp  ; x*x+y*y
  361.     fsqrt  ; sqrt, angle
  362.     fild [txt.y1];y
  363.     fabs
  364.     fild [txt.x1] ; x
  365.     fabs
  366.     fpatan ; arctg(y/x)
  367.   .skip:
  368.     cmp  [txt.x1],0
  369.     jge  .xplus
  370.     fchs
  371.     fadd st0,st3
  372.   .xplus:
  373.     cmp  [txt.y1],0
  374.     jge  .yplus
  375.     fchs
  376.   .yplus:
  377.     fadd st0,st2
  378.     fsincos
  379.     fmul st0,st2
  380.     fiadd [txt.x0]
  381.     fistp [txt.x1] ; x=r*cos a
  382.     fmulp ; y=r*sin a,angle
  383.     fiadd [txt.y0]
  384.     fistp [txt.y1]
  385.     ret
  386.  
  387. _BGIfont_Freetext:
  388. ; in: ebx-BGIfree structure
  389. ; out: eax-new drawing coords
  390.     mov  edx,[ebx]
  391.     call _BGIfont_GetID
  392.     test eax,eax
  393.     jnz  .fexists
  394.     ret
  395.   .fexists:
  396.     pusha
  397.     fninit
  398.     fldpi
  399.     fld  [pi180]
  400.     fimul dword[ebx+8]
  401.     fst  [BGIangle]
  402.     mov  esi,[ebx+28]
  403.     and  esi,0xffffff
  404.     add  esi,eax
  405.     mov  eax,[ebx+32]
  406.     and  [deform],0
  407.     test eax,BGI_ITALIC
  408.     jz   .norm
  409.     mov  [deform],0.4
  410.   .norm:
  411.     mov  ebp,eax
  412.     or    ebp,BGI_FREE
  413.     mov  eax,[ebx+12]
  414.     mov  [Xscale],eax
  415.     mov  eax,[ebx+16]
  416.     mov  [Yscale],eax
  417.     mov  ecx,[ebx+20]
  418.     mov  edx,ebp
  419.     and  edx,BGI_FREE+BGI_VAMASK+BGI_HAMASK
  420.     add  edx,[ebx+24]
  421.     mov  eax,[ebx+4]
  422.     mov  ebx,esi
  423.     add  ebx,0x6000000
  424.     mov  [esp+4],edx
  425.     mov  [esp+20],ecx
  426.     jmp  txt
  427.  
  428.     pi180 dd 0.017453
  429.  
  430. _BGIfont_Outtext:
  431. ; in: ebx-[x][y], ecx-color, edx-string, esi-length
  432.     pusha
  433.     mov  ebp,esi
  434. if ~ BGI_LEVEL eq KERNEL
  435.     mov  eax,ebx
  436.     mov  ebx,ecx
  437.     mov  ecx,edx
  438.     mov  edx,esi
  439. end if
  440. ; in: eax-[x][y], ebx-color, ecx-string, edx-length
  441. txt:
  442. if  ~ BGI_LEVEL eq KERNEL
  443.   if  BGI_WINDOW_CLIP eq 1
  444.     pusha
  445.     mov  eax,9
  446.     mov  ebx,BGI_PRC_INFO
  447.     mov  ecx,-1
  448.     int  0x40
  449.     popa
  450.   end if
  451. end if
  452.     mov  [.y0],ax
  453.     shr  eax,16
  454.     mov  [.x0],ax
  455.     mov  ecx,ebx ; color
  456.     and  ebx,0xfffffff
  457.     mov  [.color],ebx
  458.     call BGIfont_get2head
  459.     test edi,edi
  460.     jz   .exit
  461.     mov  ecx,[esp+4]; str length
  462.     mov  esi,[esp+20]; str ptr
  463.     movzx eax,byte[edi+5]
  464.     push ecx
  465.     and  ecx,0xff
  466.     jnz  .lenok
  467.     add  esp,4
  468.     jmp  .ex2
  469.   .lenok:
  470.     pusha
  471.     push dword[txt.y0]
  472.     and  dword[txt.y0],0
  473.     xor  edx,edx
  474.     mov  ebx,[edi+8]
  475.    .next:
  476.     call txt.BGIfont_GetChar
  477.     movzx eax,byte[ebx+eax]
  478.     add  edx,eax
  479.     loop .next
  480.     mov  ecx,edx ; ecx - x size
  481.     movzx dx,byte[edi+6]
  482.     mov  [BGIheight],dx
  483.     mov  ebx,[esp+36]
  484.     and  ebx,BGI_HAMASK
  485.     cmp  ebx,BGI_HARIGHT
  486.     je   .nova
  487.     ja   .subv
  488.     xor  ecx,ecx
  489.     jmp  .nova
  490.   .subv:
  491.     shr  cx,1
  492.   .nova:
  493.     mov  ebx,[esp+36]
  494.     and  ebx,BGI_VAMASK
  495.     cmp  ebx,BGI_VATOP
  496.     je   .def
  497.     ja   .subh
  498.     xor  edx,edx
  499.     jmp  .def
  500.   .subh:
  501.     shr  dx,1
  502.   .def:
  503.     call txt.BGIfont_Deform
  504.     pop  dword[txt.y0]
  505.     popa
  506.     pop  ebx
  507.     mov  ax,[txt.y1]
  508.     sub  [txt.y0],ax
  509.     mov  ax,[txt.x1]
  510.     sub  [txt.x0],ax
  511.     xor  eax,eax
  512.     cld
  513. .mloop:
  514.     push [.y0]
  515.     pop  [.y]
  516.     push [.x0]
  517.     pop  [.x]
  518.     call .BGIfont_GetChar
  519.     push esi
  520.     lea  esi,[edi+20] ; offset
  521.     movzx edx,word[esi+eax*2] ; ofs1
  522.     add  edx,[edi+12]
  523.     inc  eax
  524.     cmp  al,[edi+4]
  525.     je   .eof
  526.     movzx eax,word[esi+eax*2]; ofs2
  527.     add   eax,[edi+12]
  528.     jmp   .prc_vec
  529.   .eof:
  530.     mov  eax,[edi+16] ; ofs2=eof
  531.   .prc_vec:  ; edx-vec cmd ifs, eax-cmd limit
  532.     mov  [.vec_end],eax
  533.     push ecx
  534.   .vec_loop:
  535.     mov  ax,word[edx]
  536.     push edx
  537.     mov  ecx,eax
  538.     and  eax,0x8080 ; op
  539.     and  ecx,0x7f ; xx
  540.     mov  edx,[edx+1]
  541.     and  edx,0x7f ; yy
  542.     cmp  edx,63
  543.     jbe  .positive
  544.     sub  edx,128  ; yy-=128
  545.   .positive:
  546.     cmp  ecx,63
  547.     jbe  .positive2
  548.     sub  ecx,128  ; xx-=128
  549.   .positive2:
  550.     call .BGIfont_Deform
  551.     cmp  eax,0x8080
  552.     jne  .noline
  553.     test ebp,BGI_NODRAW
  554.     jnz  .noline
  555. ; draw vector
  556. if ~ BGI_LEVEL eq KERNEL
  557.     push eax
  558.     mov  ebx,dword[.x1]
  559.     mov  ecx,dword[.y1]
  560.   if BGI_WINDOW_CLIP eq 1
  561.     movzx eax,[.x]
  562.     cmp  eax,dword[BGI_PRC_INFO+42]
  563.     ja   .nobold
  564.     movzx eax,[.y]
  565.     cmp  eax,dword[BGI_PRC_INFO+46]
  566.     ja  .nobold
  567.     xor  eax,eax
  568.     cmp  ax,bx
  569.     jg   .nobold
  570.     cmp  ax,cx
  571.     jg   .nobold
  572.   end if
  573.     mov  edx,[.color]
  574.     mov  eax,38
  575.     int  0x40
  576.     test ebp,BGI_BOLD
  577.     jz   .nobold
  578.     test ebp,BGI_FREE
  579.     jnz  .free5
  580.   .free5:
  581.     add  ebx,1 shl 16+1
  582.     int  0x40
  583.   .nobold:
  584.     pop  eax
  585. else
  586.     pusha
  587.     mov  eax,dword[.x1]
  588.     mov  ebx,dword[.y1]
  589.     mov  ecx,[.color]
  590. ;    call syscall_drawline
  591.     test dword[esp+8],BGI_BOLD
  592.     jz   .nobold
  593.     add  eax,1 shl 16+1
  594. ;    call syscall_drawline
  595.   .nobold:
  596.     popa
  597. end if
  598.   .noline:
  599.     pop  edx
  600.     test eax,eax
  601.     je   .eovecs  ; op=0
  602.     push [.y1]
  603.     pop  [.y]
  604.     push [.x1]
  605.     pop  [.x]
  606.     add  edx,2
  607.     cmp  edx,[.vec_end]
  608.     jb   .vec_loop
  609.   .eovecs:
  610.     pop  ecx esi
  611.     push [.y]
  612.     pop  [.y0]
  613.     push [.x]
  614.     pop  [.x0]
  615.     loop .mloop1
  616.     jmp  .exit
  617.   .mloop1:
  618.     jmp  .mloop
  619.   .exit:
  620.     mov  eax,dword[.y0]
  621.     mov  [esp+28],eax
  622.   .ex2:
  623.     popa
  624.     ret
  625.  
  626. .BGIfont_Deform:
  627.     test ebp,BGI_FREE
  628.     jnz  .free0
  629.     movzx ebx,byte[.color+3] ;ebx=scale
  630.     imul ecx,ebx
  631.     add  ecx,2
  632.     shr  ecx,2
  633.     imul edx,ebx
  634.     add  edx,2
  635.     shr  edx,2
  636.     neg  edx
  637.     mov  [.x1],cx
  638.     mov  [.y1],dx
  639.     jmp  .add
  640.   .free0:
  641.     mov  [.x1],cx
  642.     mov  [.y1],dx
  643.     fild [.y1]
  644.     fld  st0
  645.     fmul [Yscale]
  646.     fchs
  647.     fistp [.y1]
  648.     fmul [deform]
  649.     fiadd [.x1]
  650.     fmul [Xscale]
  651.     fistp [.x1]
  652.     cmp  [BGIangle],0
  653.     je   .add
  654.     call BGIfont_Coo
  655.     jmp  .eax
  656.   .add:
  657.     mov  cx,[.x0]
  658.     add  [.x1],cx
  659.     mov  cx,[.y0]
  660.     add  [.y1],cx
  661.   .eax:
  662.     ret
  663.  
  664. .BGIfont_GetChar:
  665. ; in:  esi -> string; edi -> BGIrec
  666. ; out: esi -> next char; al - char obtained
  667.     lodsb  ; al - char from str
  668.     sub  al,[edi+5]
  669.     jb   .out
  670.     cmp  al,[edi+4]
  671.     jb   .in
  672.   .out:
  673.     xor  al,al ; al - 1st symbol available
  674.   .in:
  675.     ret
  676.  
  677. .y0      dw ?
  678. .x0      dw ?
  679.  
  680. .x1      dw ?
  681. .x       dw ?
  682. .y1      dw ?
  683. .y       dw ?
  684.  
  685. .color   dd ?
  686. .vec_end dd ?
  687. BGIfont_Ptr  dd 0
  688. BGIheight dw ?
  689. deform dd ?
  690. BGIangle dd ?
  691. Xscale  dd ?
  692. Yscale  dd ?
  693.