xFonts.pas 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. unit xFonts;
  2. interface
  3. uses Classes, Graphics, SysUtils;
  4. procedure StringToFont(sFont: string; Font: TFont; bIncludeColor: Boolean = True);
  5. function FontToString(Font: TFont; bIncludeColor: Boolean = True): string;
  6. function FontStyleToString(Font: TFont):String;
  7. implementation
  8. const
  9. csfsBold = '|Bold';
  10. csfsItalic = '|Italic';
  11. csfsUnderline = '|Underline';
  12. csfsStrikeout = '|Strikeout';
  13. //
  14. // Expected format:
  15. // "Arial", 9, [Bold], [clRed]
  16. //
  17. procedure StringToFont(sFont: string; Font: TFont; bIncludeColor: Boolean = True);
  18. var
  19. P : Integer;
  20. sStyle: string;
  21. sColor: string;
  22. begin
  23. with Font do
  24. try
  25. // get font name
  26. P := Pos(',', sFont);
  27. name := Copy(sFont, 2, P - 3);
  28. Delete(sFont, 1, P);
  29. // get font size
  30. P := Pos(',', sFont);
  31. Size := StrToInt(Copy(sFont, 2, P - 2));
  32. Delete(sFont, 1, P);
  33. // get font style
  34. P := Pos(',', sFont);
  35. sStyle := '|' + Copy(sFont, 3, P - 4);
  36. Delete(sFont, 1, P);
  37. // get font color
  38. if bIncludeColor then
  39. begin
  40. sColor := Copy(sFont, 3, Length(sFont) - 3);
  41. if sColor <> '' then
  42. Color := StringToColor(sColor);
  43. end;
  44. // convert str font style to
  45. // font style
  46. Style := [];
  47. if (Pos(csfsBold, sStyle) > 0) then
  48. Style := Style + [fsBold];
  49. if (Pos(csfsItalic, sStyle) > 0) then
  50. Style := Style + [fsItalic];
  51. if (Pos(csfsUnderline, sStyle) > 0) then
  52. Style := Style + [fsUnderline];
  53. if (Pos(csfsStrikeout, sStyle) > 0) then
  54. Style := Style + [fsStrikeOut];
  55. except
  56. end;
  57. end;
  58. //
  59. // Output format:
  60. // "Aril", 9, [Bold|Italic], [clAqua]
  61. //
  62. function FontToString(Font: TFont; bIncludeColor: Boolean = True): string;
  63. var
  64. sStyle: string;
  65. begin
  66. with Font do
  67. begin
  68. // convert font style to string
  69. sStyle := '';
  70. if (fsBold in Style) then
  71. sStyle := sStyle + csfsBold;
  72. if (fsItalic in Style) then
  73. sStyle := sStyle + csfsItalic;
  74. if (fsUnderline in Style) then
  75. sStyle := sStyle + csfsUnderline;
  76. if (fsStrikeOut in Style) then
  77. sStyle := sStyle + csfsStrikeout;
  78. if ((Length(sStyle) > 0) and ('|' = sStyle[1])) then
  79. sStyle := Copy(sStyle, 2, Length(sStyle) - 1);
  80. Result := Format('"%s", %d, [%s]',[name, Size, sStyle]);
  81. if bIncludeColor then
  82. Result := Result + Format(', [%s]',[ColorToString(Color)]);
  83. end;
  84. end;
  85. function FontStyleToString(Font: TFont):String;
  86. var
  87. sStyle: string;
  88. begin
  89. with Font do
  90. begin
  91. // convert font style to string
  92. sStyle := '';
  93. if (fsBold in Style) then
  94. sStyle := sStyle + csfsBold;
  95. if (fsItalic in Style) then
  96. sStyle := sStyle + csfsItalic;
  97. if (fsUnderline in Style) then
  98. sStyle := sStyle + csfsUnderline;
  99. if (fsStrikeOut in Style) then
  100. sStyle := sStyle + csfsStrikeout;
  101. if ((Length(sStyle) > 0) and ('|' = sStyle[1])) then
  102. sStyle := Copy(sStyle, 2, Length(sStyle) - 1);
  103. Result := Format('%s',[sStyle]);
  104. end;
  105. end;
  106. end.