You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

197 lines
6.8 KiB

  1. unit ultsFontCreator;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils,
  6. utsTextSuite,
  7. ultsTypes;
  8. function ltsFontCreatorCreate (aHandle: TltsContextHandle; aType: TltsFontCreatorType): TltsFontCreatorHandle; stdcall;
  9. function ltsFontCreatorGetFontByName (aHandle: TltsFontCreatorHandle; aFontname: PAnsiChar; aSize: Integer;
  10. aStyle: TtsFontStyles; aAntiAliasing: TtsAntiAliasing): TltsFontHandle; stdcall;
  11. function ltsFontCreatorGetFontByFile (aHandle: TltsFontCreatorHandle; aFilename: PAnsiChar; aSize: Integer;
  12. aStyle: TtsFontStyles; aAntiAliasing: TtsAntiAliasing): TltsFontHandle; stdcall;
  13. function ltsFontCreatorGetFontByStream(aHandle: TltsFontCreatorHandle; aStream: PltsStream; aSize: Integer;
  14. aStyle: TtsFontStyles; aAntiAliasing: TtsAntiAliasing): TltsFontHandle; stdcall;
  15. function ltsFontCreatorDestroy (aHandle: TltsFontCreatorHandle): TltsErrorCode; stdcall;
  16. implementation
  17. uses
  18. utsUtils,
  19. ultsUtils;
  20. type
  21. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  22. TltsFontCreatorFreeType = class(TtsFontCreatorFreeType)
  23. public
  24. procedure DelSlave(const aSlave: TtsRefManager); override;
  25. end;
  26. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  27. TltsFontCreatorGDI = class(TtsFontCreatorGDI)
  28. public
  29. procedure DelSlave(const aSlave: TtsRefManager); override;
  30. end;
  31. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  32. //TltsFontCreatorFreeType///////////////////////////////////////////////////////////////////////////////////////////////
  33. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  34. procedure TltsFontCreatorFreeType.DelSlave(const aSlave: TtsRefManager);
  35. begin
  36. DelReference(aSlave);
  37. inherited DelSlave(aSlave);
  38. end;
  39. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  40. //TltsFontCreatorGDI////////////////////////////////////////////////////////////////////////////////////////////////////
  41. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  42. procedure TltsFontCreatorGDI.DelSlave(const aSlave: TtsRefManager);
  43. begin
  44. DelReference(aSlave);
  45. inherited DelSlave(aSlave);
  46. end;
  47. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  48. //FontCreato///////////////////////////////////////////////////////////////////////////////////////////////////////////
  49. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  50. function ltsFontCreatorCreate(aHandle: TltsContextHandle; aType: TltsFontCreatorType): TltsFontCreatorHandle; stdcall;
  51. var
  52. c: TtsContext;
  53. fc: TtsFontCreator;
  54. begin
  55. try
  56. result := nil;
  57. if not CheckContextHandle(aHandle, c) then
  58. exit;
  59. case aType of
  60. ltsFontCreatorFreeType: fc := TltsFontCreatorFreeType.Create(c);
  61. ltsFontCreatorGDI: fc := TltsFontCreatorGDI.Create(c);
  62. // TODO ltsRendererCustom: r := TltsRendererCustom.Create(c, aFormat);
  63. else
  64. SetLastError(ltsErrInvalidEnum, Format('%d is not a valid font creator type', [aType]));
  65. exit;
  66. end;
  67. AddReference(ltsObjTypeFontCreator, fc);
  68. result := fc;
  69. except
  70. on ex: Exception do begin
  71. SetLastError(ex);
  72. result := nil;
  73. end;
  74. end;
  75. end;
  76. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  77. function ltsFontCreatorGetFontByName(aHandle: TltsFontCreatorHandle; aFontname: PAnsiChar;
  78. aSize: Integer; aStyle: TtsFontStyles; aAntiAliasing: TtsAntiAliasing): TltsFontHandle; stdcall;
  79. var
  80. fc: TtsFontCreator;
  81. f: TtsFont;
  82. begin
  83. try
  84. result := nil;
  85. if not CheckFontCreatorHandle(aHandle, fc) then
  86. exit;
  87. f := fc.GetFontByName(aFontname, aSize, aStyle, aAntiAliasing);
  88. if not Assigned(f) then begin
  89. SetLastError(ltsErrInvalidOperation, 'GetFontByName is not supported by this font creator');
  90. exit;
  91. end;
  92. AddReference(ltsObjTypeFont, f);
  93. result := f;
  94. except
  95. on ex: Exception do begin
  96. SetLastError(ex);
  97. result := nil;
  98. end;
  99. end;
  100. end;
  101. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  102. function ltsFontCreatorGetFontByFile(aHandle: TltsFontCreatorHandle; aFilename: PAnsiChar;
  103. aSize: Integer; aStyle: TtsFontStyles; aAntiAliasing: TtsAntiAliasing): TltsFontHandle; stdcall;
  104. var
  105. fc: TtsFontCreator;
  106. f: TtsFont;
  107. begin
  108. try
  109. result := nil;
  110. if not CheckFontCreatorHandle(aHandle, fc) then
  111. exit;
  112. f := fc.GetFontByFile(aFilename, aSize, aStyle, aAntiAliasing);
  113. if not Assigned(f) then begin
  114. SetLastError(ltsErrInvalidOperation, 'GetFontByFile is not supported by this font creator');
  115. exit;
  116. end;
  117. AddReference(ltsObjTypeFont, f);
  118. result := f;
  119. except
  120. on ex: Exception do begin
  121. SetLastError(ex);
  122. result := nil;
  123. end;
  124. end;
  125. end;
  126. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  127. function ltsFontCreatorGetFontByStream(aHandle: TltsFontCreatorHandle; aStream: PltsStream;
  128. aSize: Integer; aStyle: TtsFontStyles; aAntiAliasing: TtsAntiAliasing): TltsFontHandle; stdcall;
  129. var
  130. fc: TtsFontCreator;
  131. f: TtsFont;
  132. s: TStream;
  133. begin
  134. try
  135. result := nil;
  136. if not CheckFontCreatorHandle(aHandle, fc) then
  137. exit;
  138. s := TltsStreamImpl.Create(aStream);
  139. try
  140. f := fc.GetFontByStream(s, aSize, aStyle, aAntiAliasing);
  141. finally
  142. FreeAndNil(s);
  143. end;
  144. if not Assigned(f) then begin
  145. SetLastError(ltsErrInvalidOperation, 'GetFontByStream is not supported by this font creator');
  146. exit;
  147. end;
  148. AddReference(ltsObjTypeFont, f);
  149. result := f;
  150. except
  151. on ex: Exception do begin
  152. SetLastError(ex);
  153. result := nil;
  154. end;
  155. end;
  156. end;
  157. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  158. function ltsFontCreatorDestroy(aHandle: TltsFontCreatorHandle): TltsErrorCode; stdcall;
  159. var
  160. fc: TtsFontCreator;
  161. begin
  162. try
  163. result := ltsErrNone;
  164. if CheckFontCreatorHandle(aHandle, fc) then begin
  165. DelReference(ltsObjTypeFontCreator, fc);
  166. FreeAndNil(fc);
  167. end else
  168. result := LastErrorCode;
  169. except
  170. on ex: Exception do begin
  171. SetLastError(ex);
  172. result := LastErrorCode;
  173. end;
  174. end;
  175. end;
  176. end.