Subversion Repositories Kolibri OS

Rev

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

  1. (*
  2.     Copyright 2016, 2018, 2022 Anton Krotov
  3.  
  4.     This file is part of fb2read.
  5.  
  6.     fb2read is free software: you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation, either version 3 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     fb2read is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with fb2read. If not, see <http://www.gnu.org/licenses/>.
  18. *)
  19.  
  20. MODULE Font;
  21.  
  22. IMPORT W := Window, S := Strings, G := Graph, sys := SYSTEM, K := KOSAPI, Conv, Ini, KF := kfonts;
  23.  
  24. VAR
  25.  
  26.   kf_font, kf_loaded, kf_enabled: BOOLEAN;
  27.   cp1251buf: ARRAY 102400 OF CHAR;
  28.  
  29.   KFont*: KF.TFont;
  30.  
  31.   Font*: RECORD
  32.     color* : INTEGER;
  33.     size   : INTEGER;
  34.     bold   : BOOLEAN;
  35.     italic : BOOLEAN;
  36.     strike : BOOLEAN
  37.   END;
  38.  
  39.   ItalicColor, NormalColor: INTEGER;
  40.  
  41.  
  42. PROCEDURE KFText(X, Y: INTEGER; first, quantity: INTEGER; canvas: G.PBuffer);
  43. BEGIN
  44.   KF.TextOut(KFont, canvas.adr - 8, X, Y, first, quantity, Font.color, ORD(Font.bold) + ORD(Font.italic) * 2 + ORD(Font.strike) * 8)
  45. END KFText;
  46.  
  47.  
  48. PROCEDURE sysfont*(sf: BOOLEAN);
  49. BEGIN
  50.   kf_font := ~sf & kf_enabled;
  51. END sysfont;
  52.  
  53.  
  54. PROCEDURE params*(): INTEGER;
  55.   RETURN Font.size + 0 + LSL(3, 16) + LSL(ORD(Font.bold) + ORD(Font.italic) * 2 + 128, 24)
  56. END params;
  57.  
  58.  
  59. PROCEDURE SetFontColor*(color: INTEGER);
  60. BEGIN
  61.         Font.color := color
  62. END SetFontColor;
  63.  
  64.  
  65. PROCEDURE Bold*(bold: BOOLEAN);
  66. BEGIN
  67.   Font.bold := bold
  68. END Bold;
  69.  
  70.  
  71. PROCEDURE Italic*(italic, notLink: BOOLEAN);
  72. BEGIN
  73.         Font.italic := italic;
  74.         IF italic THEN
  75.                 IF notLink THEN
  76.                         SetFontColor(ItalicColor)
  77.                 END
  78.         ELSE
  79.                 IF notLink THEN
  80.                         SetFontColor(NormalColor)
  81.                 END
  82.         END
  83. END Italic;
  84.  
  85.  
  86. PROCEDURE Strike*(strike: BOOLEAN);
  87. BEGIN
  88.   Font.strike := strike
  89. END Strike;
  90.  
  91.  
  92. PROCEDURE FontW(): INTEGER;
  93.   RETURN ASR(Font.size, 1)
  94. END FontW;
  95.  
  96.  
  97. PROCEDURE FontH*(): INTEGER;
  98. VAR res: INTEGER;
  99. BEGIN
  100.   IF kf_font THEN
  101.     res := KF.TextHeight(KFont)
  102.   ELSE
  103.     res := Font.size
  104.   END
  105.   RETURN res
  106. END FontH;
  107.  
  108.  
  109. PROCEDURE TextWidth*(text: S.CHARS; length: INTEGER): INTEGER;
  110. VAR res: INTEGER;
  111. BEGIN
  112.   IF kf_font THEN
  113.     Conv.convert(text.first, sys.ADR(cp1251buf[0]), length);
  114.     res := KF.TextWidth(KFont, sys.ADR(cp1251buf[0]), length, ORD(Font.bold) + ORD(Font.italic) * 2)
  115.   ELSE
  116.     res := length * FontW()
  117.   END
  118.   RETURN res
  119. END TextWidth;
  120.  
  121.  
  122. PROCEDURE MonoWidth*(): INTEGER;
  123.   RETURN FontW()
  124. END MonoWidth;
  125.  
  126.  
  127. PROCEDURE StrikeText*(Rect: W.TRect; X, Y: INTEGER; width: INTEGER);
  128. VAR y: INTEGER;
  129. BEGIN
  130.   IF Font.strike THEN
  131.     y := Y + FontH() DIV 2;
  132. //    X := X + ORD(Font.italic & kf_font) * ((KF.TextHeight(KFont) DIV 2) DIV 3);
  133.     G.SetColor(Font.color);
  134.     G.HLine(X + Rect.Left, X + Rect.Left + width, y + Rect.Top);
  135.     IF Font.size >= 28 THEN
  136.       INC(y);
  137.       G.HLine(X + Rect.Left, X + Rect.Left + width, y + Rect.Top);
  138.     END
  139.   END
  140. END StrikeText;
  141.  
  142.  
  143. PROCEDURE Text*(Rect: W.TRect; X, Y: INTEGER; adr: INTEGER; length: INTEGER);
  144. BEGIN
  145.   IF kf_font THEN
  146.     Conv.convert(adr, sys.ADR(cp1251buf[0]), length);
  147.     KFText(X + Rect.Left, Y + Rect.Top, sys.ADR(cp1251buf[0]), length, G.Buffer)
  148.   ELSE
  149.     G.SetColor(Font.color);
  150.     G.TextOut(X + Rect.Left, Y + Rect.Top, adr, length, Font.size, params())
  151.   END
  152. END Text;
  153.  
  154.  
  155. PROCEDURE Init*(italic, normal, fs: INTEGER);
  156. BEGIN
  157.   ItalicColor := italic;
  158.   NormalColor := normal;
  159.   IF KF.SetSize(KFont, fs) THEN
  160.     Font.size := KF.TextHeight(KFont);
  161.     kf_font := TRUE;
  162.     kf_enabled := TRUE
  163.   ELSE
  164.     Font.size := fs;
  165.     kf_font := FALSE;
  166.     kf_enabled := FALSE
  167.   END
  168. END Init;
  169.  
  170.  
  171. BEGIN
  172.   KFont := KF.LoadFont(Ini.Font);
  173.   kf_loaded := KFont # NIL;
  174.   kf_font := kf_loaded;
  175.   kf_enabled := kf_loaded
  176. END Font.
  177.