Recent

Author Topic: [SOLVED] How can I inject an emoji code point into a TMemo?  (Read 796 times)

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 757
  • Professional amateur ;-P
[SOLVED] How can I inject an emoji code point into a TMemo?
« on: September 16, 2021, 05:32:57 pm »
Hey Y'all,

I'm thinking about doing an emoji map app like the ones we have for regular characters, the well known and now forgotten char map application.

I've found out about these sites that list the emoji's code points:

And I've also found out about the Unicode CLDR Project that has JSON formatted lists of Unicode stuff, including emojis.

My question is: How do I inject, programmatically, one of these code points into a TMemo?

Many thanks in advance!!

Cheers,
Gus

PS: Don't really know if this is the right forum section. If not, I more than welcome our great team of moderators to move it to the appropriate section. THANKS!!
« Last Edit: September 16, 2021, 07:00:56 pm by Gustavo 'Gus' Carreno »
Lazarus 2.3.0(trunk) FPC 3.3.1(trunk) Ubuntu 21.04 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.2(stable) Ubuntu 21.04 64b Dark Theme
http://github.com/gcarreno

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 757
  • Professional amateur ;-P
Re: [SOLVED] How can I inject an emoji code point into a TMemo?
« Reply #1 on: September 16, 2021, 06:59:20 pm »
Hey Y'all,

After a bit of Googling I've found my answer.

There 2 ways of doing it:
  • Without using any unit related to UTF8
  • Using LazUTF8

Without using any unit related to UTF8

Code: Pascal  [Select][+][-]
  1. Memo1.Text := Memo1.Text + char(240)+ char(159) + char(152) + char(128);
This will append the Grinning Face Emoji (😀) to the memo.
But this requires you to translate the code point U+1F600 to the 4 individual chars.

I've found that it's not that easy when I found the UnicodeToUTF8Inline function on the LazUTF8 unit:
Code: Pascal  [Select][+][-]
  1. function UnicodeToUTF8Inline(CodePoint: cardinal; Buf: PChar): integer;
  2. begin
  3.   case CodePoint of
  4.     0..$7f:
  5.       begin
  6.         Result:=1;
  7.         Buf[0]:=char(byte(CodePoint));
  8.       end;
  9.     $80..$7ff:
  10.       begin
  11.         Result:=2;
  12.         Buf[0]:=char(byte($c0 or (CodePoint shr 6)));
  13.         Buf[1]:=char(byte($80 or (CodePoint and $3f)));
  14.       end;
  15.     $800..$ffff:
  16.       begin
  17.         Result:=3;
  18.         Buf[0]:=char(byte($e0 or (CodePoint shr 12)));
  19.         Buf[1]:=char(byte((CodePoint shr 6) and $3f) or $80);
  20.         Buf[2]:=char(byte(CodePoint and $3f) or $80);
  21.       end;
  22.     $10000..$10ffff:
  23.       begin
  24.         Result:=4;
  25.         Buf[0]:=char(byte($f0 or (CodePoint shr 18)));
  26.         Buf[1]:=char(byte((CodePoint shr 12) and $3f) or $80);
  27.         Buf[2]:=char(byte((CodePoint shr 6) and $3f) or $80);
  28.         Buf[3]:=char(byte(CodePoint and $3f) or $80);
  29.       end;
  30.   else
  31.     Result:=0;
  32.   end;
  33. end;
There is A LOT of bit shifting and ORing in there, LOL!!!

Using LazzUTF8

Code: Pascal  [Select][+][-]
  1. uses
  2.   LazUTF8
  3. , StrUtils
  4. ;
  5.  
  6. Memo1.Text := Memo1.Text + UnicodeToUTF8(Hex2Dec('1F600'));
  7. // OR
  8. Memo1.Text := Memo1.Text + UnicodeToUTF8($1F600);

So I guess I have my answer and I'll be doing some more Googling to see if I can find a neat little list of the code points with the names and categories.

If not, I guess I can always scrape https://emojipedia.org, right? LOL !! 🤣 🤪

Cheers,
Gus
« Last Edit: September 16, 2021, 07:06:05 pm by Gustavo 'Gus' Carreno »
Lazarus 2.3.0(trunk) FPC 3.3.1(trunk) Ubuntu 21.04 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.2(stable) Ubuntu 21.04 64b Dark Theme
http://github.com/gcarreno

winni

  • Hero Member
  • *****
  • Posts: 2702
Re: [SOLVED] How can I inject an emoji code point into a TMemo?
« Reply #2 on: September 16, 2021, 08:08:21 pm »
Hi!

There is an easier solution but the syntax  for that is not nice:

The rocket emoticon has an hex  value of  $F09F9A80

Code: Pascal  [Select][+][-]
  1. const rocket = #$F0#$9F#$9A#$80;  
  2. ....
  3. Edit1.text := Edit1.text + #32 + rocket;
  4.  

Have a pleasant flight!

Winni

winni

  • Hero Member
  • *****
  • Posts: 2702
Re: [SOLVED] How can I inject an emoji code point into a TMemo?
« Reply #3 on: September 16, 2021, 08:17:45 pm »
Hi!

The page with all UTF8 code ,points with a bad design:

https://www.utf8-chartable.de/unicode-utf8-table.pl

The third line in the box is a ComboBox with all code blocks
The emoticons are nearly at the end of the list.

Winni

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 757
  • Professional amateur ;-P
Re: [SOLVED] How can I inject an emoji code point into a TMemo?
« Reply #4 on: September 16, 2021, 08:19:46 pm »
Hey winni,

Code: Pascal  [Select][+][-]
  1. const rocket = #$F0#$9F#$9A#$80;

Hummmm, I was unaware that we could use non base 10 numbers after the hash(#) to define a character, so I'm very thankful for this hint!!!

Thanks, Winni, you gave me another morsel of knowledge I was missing and for that I'm quite grateful!!

Cheers,
Gus
Lazarus 2.3.0(trunk) FPC 3.3.1(trunk) Ubuntu 21.04 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.2(stable) Ubuntu 21.04 64b Dark Theme
http://github.com/gcarreno

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 757
  • Professional amateur ;-P
Re: [SOLVED] How can I inject an emoji code point into a TMemo?
« Reply #5 on: September 16, 2021, 08:27:35 pm »
Hey winni,

The page with all UTF8 code ,points with a bad design:
https://www.utf8-chartable.de/unicode-utf8-table.pl

Thanks for this link!!

I prefer the better formatted and more up to date tables from the Unicode site:

The first is around 5MB and the second is about 33MB in size.
Both have the emojis and the second one even has the diff emojis by platform(Apple, Samnsung, etc) in base64 format. So scrapping from them is a bit better.
Combining the info on both will give me quite a nice dataset of emojis.
And because that's the official site for Unicode, every time they update the specs, I'll come back and download it again.

I've downloaded version 14.0 of both and will try and scrape the info latter.

All these lists are made from the CLDR repository and I'm still curious on how I can use it.
Need to dig a bit more on the Unicode site and also the CLDR part of the Unicode site.

Cheers,
Gus
« Last Edit: September 16, 2021, 08:29:31 pm by Gustavo 'Gus' Carreno »
Lazarus 2.3.0(trunk) FPC 3.3.1(trunk) Ubuntu 21.04 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.2(stable) Ubuntu 21.04 64b Dark Theme
http://github.com/gcarreno

 

TinyPortal © 2005-2018