MagicFoundation / Alcinoe

Alcinoe Component Library For Delphi. Full opengl video player, WebRTC delphi wrapper, native ios/android TEdit, Improuved firemonkey controls, Firebase cloud messaging, Android/ios facebook sdk login, Json/Bson Parser, ImageMagick wrapper, MongoDb client And much more
Apache License 2.0
966 stars 211 forks source link

Android custom fonts for Alcinoe Controls #139

Open bravesoftdz opened 4 years ago

bravesoftdz commented 4 years ago

hello Mr: Zeus64 May i know please the right way to work with custom fonts inside android.? i have Patch this unit here: (FMX.FontGlyphs.Android), but no effect.... i know that your controls are native device controls and the paint in fmx android is ( FPaint: JPaint; ) please guide me and if there is any demo or sample will be more helpful.. i have watch in youtube how can do that in android studio and i follow the same steps but the controls didn't change their font as i expected ... i deploy a folder inside the res remote path named as (font) an inside that folder i deploy two xml files( attr.xml and myfont_familly.xml) and also deploy my fonts there .. after that i go to my custom styles.xml and i add this line here: <item name="android:fontFamily">@font/myfont_familly</item> but when i compile to run in android no effect happen ??? please any reply will be so usefull

cfusari2A commented 3 years ago

Hi, in order to have the custom fonts working with TALText, I had to make two modifications inside ALFmxCommon.pas, in the method ALDrawMultiLineText, I replaced the following fragment: //init aPaint {$IF defined(ANDROID)} aPaint.setColor(integer(aFontColor)); JStr1 := StringToJString(aOptions.FontName); // << https://quality.embarcadero.com/browse/RSP-14187 aTypeface := TJTypeface.JavaClass.create(JStr1, aStyle); aPaint.setTypeface(aTypeface); aTypeface := nil; JStr1 := nil; {$ENDIF} with: //init aPaint {$IF defined(ANDROID)} aPaint.setColor(integer(aFontColor)); if FileExists(TPath.GetDocumentsPath + PathDelim + aOptions.FontName + '.ttf') then begin JStr1 := StringToJString(TPath.GetDocumentsPath + PathDelim + aOptions.FontName + '.ttf'); aTypeface := TJTypeface.JavaClass.createFromFile(JStr1); end else begin JStr1 := StringToJString(aOptions.FontName); // << https://quality.embarcadero.com/browse/RSP-14187 aTypeface := TJTypeface.JavaClass.create(JStr1, aStyle); end; aPaint.setTypeface(aTypeface); aTypeface := nil; JStr1 := nil; {$ENDIF}

and the following fragment (in the same method): else begin aPaint.setColor(integer(aBreakedTextItem.fontColor)); //----- JStr1 := StringToJString(aOptions.FontName); // << https://quality.embarcadero.com/browse/RSP-14187 aTypeface := TJTypeface.JavaClass.create(JStr1, aBreakedTextItem.fontStyle); aPaint.setTypeface(aTypeface); aTypeface := nil; JStr1 := nil; //----- aCanvas.drawText(aBreakedTextItem.line{text}, aBreakedTextItem.pos.x {x}, aBreakedTextItem.pos.y {y}, apaint {paint}); end; //----- end; with: else begin aPaint.setColor(integer(aBreakedTextItem.fontColor)); //----- if FileExists(TPath.GetDocumentsPath + PathDelim + aOptions.FontName + '.ttf') then begin JStr1 := StringToJString(TPath.GetDocumentsPath + PathDelim + aOptions.FontName + '.ttf'); aTypeface := TJTypeface.JavaClass.createFromFile(JStr1); end else begin JStr1 := StringToJString(aOptions.FontName); // << https://quality.embarcadero.com/browse/RSP-14187 aTypeface := TJTypeface.JavaClass.create(JStr1, aBreakedTextItem.fontStyle); end; aPaint.setTypeface(aTypeface); aTypeface := nil; JStr1 := nil; //----- aCanvas.drawText(aBreakedTextItem.line{text}, aBreakedTextItem.pos.x {x}, aBreakedTextItem.pos.y {y}, apaint {paint}); end; //----- end; Basically, when using aOptions.FontName, testing first if a file with .ttf exists in the document path and using createFromFile instead of simple create. Then, I included the font file (AnyFontName.ttf) inside '.\assets\internal' in the deploy section. In delphi, I use AnyFontName as Family Name of the TALText. (note that changing style is not working, you have to put the corresponding ttf, like AnyFontName-Bold or AnyFontName-Italic). (I am not using the xml declaration you talk about). Hope this will help. Regards.

bravesoftdz commented 3 years ago

Many & infinity of Many thanks @cfusari2A i will try it ASAP

cfusari2A commented 3 years ago

Hi, bravesoftdz, While testing on Friday, we found out that FileExists and createFromFile produce some performances loss. To correct it, we used a dictionnary to store the already created fonts. I attach both versions we have (for Delphi 10.3 and 10.4). Those version contain ALFmxCommon.zip

the gradient paint we made for IOS, so there is a little difference with original Alcinoe (the parameter names).

bravesoftdz commented 3 years ago

i will check all of them @cfusari2A a huge thanks in advance...

bravesoftdz commented 3 years ago

the right to left languages didn't work !!!! like arabic it's work just with left to right languages