googlewebfonts.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. unit googlewebfonts;
  2. {$MODE objfpc}
  3. {$H+}
  4. interface
  5. uses sysutils, classes, googleservice, restbase, googlebase;
  6. type
  7. //Top-level schema types
  8. TWebfont = Class;
  9. TWebfontList = Class;
  10. TWebfontArray = Array of TWebfont;
  11. TWebfontListArray = Array of TWebfontList;
  12. //Anonymous types, using auto-generated names
  13. TWebfontTypefiles = Class;
  14. TWebfontListTypeitemsArray = Array of TWebfont;
  15. { --------------------------------------------------------------------
  16. TWebfontTypefiles
  17. --------------------------------------------------------------------}
  18. TWebfontTypefiles = Class(TGoogleBaseObject)
  19. Private
  20. Protected
  21. //Property setters
  22. Public
  23. Class Function AllowAdditionalProperties : Boolean; override;
  24. Published
  25. end;
  26. TWebfontTypefilesClass = Class of TWebfontTypefiles;
  27. { --------------------------------------------------------------------
  28. TWebfont
  29. --------------------------------------------------------------------}
  30. TWebfont = Class(TGoogleBaseObject)
  31. Private
  32. Fcategory : String;
  33. Ffamily : String;
  34. Ffiles : TWebfontTypefiles;
  35. Fkind : String;
  36. FlastModified : TDate;
  37. Fsubsets : TStringArray;
  38. Fvariants : TStringArray;
  39. Fversion : String;
  40. Protected
  41. //Property setters
  42. Procedure Setcategory(AIndex : Integer; const AValue : String); virtual;
  43. Procedure Setfamily(AIndex : Integer; const AValue : String); virtual;
  44. Procedure Setfiles(AIndex : Integer; const AValue : TWebfontTypefiles); virtual;
  45. Procedure Setkind(AIndex : Integer; const AValue : String); virtual;
  46. Procedure SetlastModified(AIndex : Integer; const AValue : TDate); virtual;
  47. Procedure Setsubsets(AIndex : Integer; const AValue : TStringArray); virtual;
  48. Procedure Setvariants(AIndex : Integer; const AValue : TStringArray); virtual;
  49. Procedure Setversion(AIndex : Integer; const AValue : String); virtual;
  50. //2.6.4. bug workaround
  51. {$IFDEF VER2_6}
  52. Procedure SetArrayLength(Const AName : String; ALength : Longint); override;
  53. {$ENDIF VER2_6}
  54. Public
  55. Published
  56. Property category : String Index 0 Read Fcategory Write Setcategory;
  57. Property family : String Index 8 Read Ffamily Write Setfamily;
  58. Property files : TWebfontTypefiles Index 16 Read Ffiles Write Setfiles;
  59. Property kind : String Index 24 Read Fkind Write Setkind;
  60. Property lastModified : TDate Index 32 Read FlastModified Write SetlastModified;
  61. Property subsets : TStringArray Index 40 Read Fsubsets Write Setsubsets;
  62. Property variants : TStringArray Index 48 Read Fvariants Write Setvariants;
  63. Property version : String Index 56 Read Fversion Write Setversion;
  64. end;
  65. TWebfontClass = Class of TWebfont;
  66. { --------------------------------------------------------------------
  67. TWebfontList
  68. --------------------------------------------------------------------}
  69. TWebfontList = Class(TGoogleBaseObject)
  70. Private
  71. Fitems : TWebfontListTypeitemsArray;
  72. Fkind : String;
  73. Protected
  74. //Property setters
  75. Procedure Setitems(AIndex : Integer; const AValue : TWebfontListTypeitemsArray); virtual;
  76. Procedure Setkind(AIndex : Integer; const AValue : String); virtual;
  77. //2.6.4. bug workaround
  78. {$IFDEF VER2_6}
  79. Procedure SetArrayLength(Const AName : String; ALength : Longint); override;
  80. {$ENDIF VER2_6}
  81. Public
  82. Published
  83. Property items : TWebfontListTypeitemsArray Index 0 Read Fitems Write Setitems;
  84. Property kind : String Index 8 Read Fkind Write Setkind;
  85. end;
  86. TWebfontListClass = Class of TWebfontList;
  87. { --------------------------------------------------------------------
  88. TWebfontsResource
  89. --------------------------------------------------------------------}
  90. //Optional query Options for TWebfontsResource, method List
  91. TWebfontsListOptions = Record
  92. sort : String;
  93. end;
  94. TWebfontsResource = Class(TGoogleResource)
  95. Public
  96. Class Function ResourceName : String; override;
  97. Class Function DefaultAPI : TGoogleAPIClass; override;
  98. Function List(AQuery : string = '') : TWebfontList;
  99. Function List(AQuery : TWebfontslistOptions) : TWebfontList;
  100. end;
  101. { --------------------------------------------------------------------
  102. TWebfontsAPI
  103. --------------------------------------------------------------------}
  104. TWebfontsAPI = Class(TGoogleAPI)
  105. Private
  106. FWebfontsInstance : TWebfontsResource;
  107. Function GetWebfontsInstance : TWebfontsResource;virtual;
  108. Public
  109. //Override class functions with API info
  110. Class Function APIName : String; override;
  111. Class Function APIVersion : String; override;
  112. Class Function APIRevision : String; override;
  113. Class Function APIID : String; override;
  114. Class Function APITitle : String; override;
  115. Class Function APIDescription : String; override;
  116. Class Function APIOwnerDomain : String; override;
  117. Class Function APIOwnerName : String; override;
  118. Class Function APIIcon16 : String; override;
  119. Class Function APIIcon32 : String; override;
  120. Class Function APIdocumentationLink : String; override;
  121. Class Function APIrootUrl : string; override;
  122. Class Function APIbasePath : string;override;
  123. Class Function APIbaseURL : String;override;
  124. Class Function APIProtocol : string;override;
  125. Class Function APIservicePath : string;override;
  126. Class Function APIbatchPath : String;override;
  127. Class Function APIAuthScopes : TScopeInfoArray;override;
  128. Class Function APINeedsAuth : Boolean;override;
  129. Class Procedure RegisterAPIResources; override;
  130. //Add create function for resources
  131. Function CreateWebfontsResource(AOwner : TComponent) : TWebfontsResource;virtual;overload;
  132. Function CreateWebfontsResource : TWebfontsResource;virtual;overload;
  133. //Add default on-demand instances for resources
  134. Property WebfontsResource : TWebfontsResource Read GetWebfontsInstance;
  135. end;
  136. implementation
  137. { --------------------------------------------------------------------
  138. TWebfontTypefiles
  139. --------------------------------------------------------------------}
  140. Class Function TWebfontTypefiles.AllowAdditionalProperties : Boolean;
  141. begin
  142. Result:=True;
  143. end;
  144. { --------------------------------------------------------------------
  145. TWebfont
  146. --------------------------------------------------------------------}
  147. Procedure TWebfont.Setcategory(AIndex : Integer; const AValue : String);
  148. begin
  149. If (Fcategory=AValue) then exit;
  150. Fcategory:=AValue;
  151. MarkPropertyChanged(AIndex);
  152. end;
  153. Procedure TWebfont.Setfamily(AIndex : Integer; const AValue : String);
  154. begin
  155. If (Ffamily=AValue) then exit;
  156. Ffamily:=AValue;
  157. MarkPropertyChanged(AIndex);
  158. end;
  159. Procedure TWebfont.Setfiles(AIndex : Integer; const AValue : TWebfontTypefiles);
  160. begin
  161. If (Ffiles=AValue) then exit;
  162. Ffiles:=AValue;
  163. MarkPropertyChanged(AIndex);
  164. end;
  165. Procedure TWebfont.Setkind(AIndex : Integer; const AValue : String);
  166. begin
  167. If (Fkind=AValue) then exit;
  168. Fkind:=AValue;
  169. MarkPropertyChanged(AIndex);
  170. end;
  171. Procedure TWebfont.SetlastModified(AIndex : Integer; const AValue : TDate);
  172. begin
  173. If (FlastModified=AValue) then exit;
  174. FlastModified:=AValue;
  175. MarkPropertyChanged(AIndex);
  176. end;
  177. Procedure TWebfont.Setsubsets(AIndex : Integer; const AValue : TStringArray);
  178. begin
  179. If (Fsubsets=AValue) then exit;
  180. Fsubsets:=AValue;
  181. MarkPropertyChanged(AIndex);
  182. end;
  183. Procedure TWebfont.Setvariants(AIndex : Integer; const AValue : TStringArray);
  184. begin
  185. If (Fvariants=AValue) then exit;
  186. Fvariants:=AValue;
  187. MarkPropertyChanged(AIndex);
  188. end;
  189. Procedure TWebfont.Setversion(AIndex : Integer; const AValue : String);
  190. begin
  191. If (Fversion=AValue) then exit;
  192. Fversion:=AValue;
  193. MarkPropertyChanged(AIndex);
  194. end;
  195. //2.6.4. bug workaround
  196. {$IFDEF VER2_6}
  197. Procedure TWebfont.SetArrayLength(Const AName : String; ALength : Longint);
  198. begin
  199. Case AName of
  200. 'subsets' : SetLength(Fsubsets,ALength);
  201. 'variants' : SetLength(Fvariants,ALength);
  202. else
  203. Inherited SetArrayLength(AName,ALength);
  204. end;
  205. end;
  206. {$ENDIF VER2_6}
  207. { --------------------------------------------------------------------
  208. TWebfontList
  209. --------------------------------------------------------------------}
  210. Procedure TWebfontList.Setitems(AIndex : Integer; const AValue : TWebfontListTypeitemsArray);
  211. begin
  212. If (Fitems=AValue) then exit;
  213. Fitems:=AValue;
  214. MarkPropertyChanged(AIndex);
  215. end;
  216. Procedure TWebfontList.Setkind(AIndex : Integer; const AValue : String);
  217. begin
  218. If (Fkind=AValue) then exit;
  219. Fkind:=AValue;
  220. MarkPropertyChanged(AIndex);
  221. end;
  222. //2.6.4. bug workaround
  223. {$IFDEF VER2_6}
  224. Procedure TWebfontList.SetArrayLength(Const AName : String; ALength : Longint);
  225. begin
  226. Case AName of
  227. 'items' : SetLength(Fitems,ALength);
  228. else
  229. Inherited SetArrayLength(AName,ALength);
  230. end;
  231. end;
  232. {$ENDIF VER2_6}
  233. { --------------------------------------------------------------------
  234. TWebfontsResource
  235. --------------------------------------------------------------------}
  236. Class Function TWebfontsResource.ResourceName : String;
  237. begin
  238. Result:='webfonts';
  239. end;
  240. Class Function TWebfontsResource.DefaultAPI : TGoogleAPIClass;
  241. begin
  242. Result:=TwebfontsAPI;
  243. end;
  244. Function TWebfontsResource.List(AQuery : string = '') : TWebfontList;
  245. Const
  246. _HTTPMethod = 'GET';
  247. _Path = 'webfonts';
  248. _Methodid = 'webfonts.webfonts.list';
  249. begin
  250. Result:=ServiceCall(_HTTPMethod,_Path,AQuery,Nil,TWebfontList) as TWebfontList;
  251. end;
  252. Function TWebfontsResource.List(AQuery : TWebfontslistOptions) : TWebfontList;
  253. Var
  254. _Q : String;
  255. begin
  256. _Q:='';
  257. AddToQuery(_Q,'sort',AQuery.sort);
  258. Result:=List(_Q);
  259. end;
  260. { --------------------------------------------------------------------
  261. TWebfontsAPI
  262. --------------------------------------------------------------------}
  263. Class Function TWebfontsAPI.APIName : String;
  264. begin
  265. Result:='webfonts';
  266. end;
  267. Class Function TWebfontsAPI.APIVersion : String;
  268. begin
  269. Result:='v1';
  270. end;
  271. Class Function TWebfontsAPI.APIRevision : String;
  272. begin
  273. Result:='20160302';
  274. end;
  275. Class Function TWebfontsAPI.APIID : String;
  276. begin
  277. Result:='webfonts:v1';
  278. end;
  279. Class Function TWebfontsAPI.APITitle : String;
  280. begin
  281. Result:='Google Fonts Developer API';
  282. end;
  283. Class Function TWebfontsAPI.APIDescription : String;
  284. begin
  285. Result:='Accesses the metadata for all families served by Google Fonts, providing a list of families currently available (including available styles and a list of supported script subsets).';
  286. end;
  287. Class Function TWebfontsAPI.APIOwnerDomain : String;
  288. begin
  289. Result:='google.com';
  290. end;
  291. Class Function TWebfontsAPI.APIOwnerName : String;
  292. begin
  293. Result:='Google';
  294. end;
  295. Class Function TWebfontsAPI.APIIcon16 : String;
  296. begin
  297. Result:='https://www.google.com/images/icons/feature/font_api-16.png';
  298. end;
  299. Class Function TWebfontsAPI.APIIcon32 : String;
  300. begin
  301. Result:='https://www.google.com/images/icons/feature/font_api-32.gif';
  302. end;
  303. Class Function TWebfontsAPI.APIdocumentationLink : String;
  304. begin
  305. Result:='https://developers.google.com/fonts/docs/developer_api';
  306. end;
  307. Class Function TWebfontsAPI.APIrootUrl : string;
  308. begin
  309. Result:='https://www.googleapis.com/';
  310. end;
  311. Class Function TWebfontsAPI.APIbasePath : string;
  312. begin
  313. Result:='/webfonts/v1/';
  314. end;
  315. Class Function TWebfontsAPI.APIbaseURL : String;
  316. begin
  317. Result:='https://www.googleapis.com/webfonts/v1/';
  318. end;
  319. Class Function TWebfontsAPI.APIProtocol : string;
  320. begin
  321. Result:='rest';
  322. end;
  323. Class Function TWebfontsAPI.APIservicePath : string;
  324. begin
  325. Result:='webfonts/v1/';
  326. end;
  327. Class Function TWebfontsAPI.APIbatchPath : String;
  328. begin
  329. Result:='batch';
  330. end;
  331. Class Function TWebfontsAPI.APIAuthScopes : TScopeInfoArray;
  332. begin
  333. SetLength(Result,0);
  334. end;
  335. Class Function TWebfontsAPI.APINeedsAuth : Boolean;
  336. begin
  337. Result:=False;
  338. end;
  339. Class Procedure TWebfontsAPI.RegisterAPIResources;
  340. begin
  341. TWebfontTypefiles.RegisterObject;
  342. TWebfont.RegisterObject;
  343. TWebfontList.RegisterObject;
  344. end;
  345. Function TWebfontsAPI.GetWebfontsInstance : TWebfontsResource;
  346. begin
  347. if (FWebfontsInstance=Nil) then
  348. FWebfontsInstance:=CreateWebfontsResource;
  349. Result:=FWebfontsInstance;
  350. end;
  351. Function TWebfontsAPI.CreateWebfontsResource : TWebfontsResource;
  352. begin
  353. Result:=CreateWebfontsResource(Self);
  354. end;
  355. Function TWebfontsAPI.CreateWebfontsResource(AOwner : TComponent) : TWebfontsResource;
  356. begin
  357. Result:=TWebfontsResource.Create(AOwner);
  358. Result.API:=Self.API;
  359. end;
  360. initialization
  361. TWebfontsAPI.RegisterAPI;
  362. end.