PB 6.20: Emojis above 65535?

Just starting out? Need help? Post your questions and find answers here.
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: PB 6.20: Emojis above 65535?

Post by infratec »

Now with a HTMLEntityDecoder();

Code: Select all

EnableExplicit


Procedure.s ChrExtended(CharValue.l)
  
  Protected Result$, UTF8Value.l, ByteLength.i
  
  
  If CharValue <= 128
    UTF8Value = CharValue
    ;Debug Hex(UTF8Value)
    Result$ = PeekS(@UTF8Value, 1, #PB_UTF8|#PB_ByteLength)
  ElseIf CharValue <= 1920
    UTF8Value = ((%11000000 | ((CharValue >> 8) << 2) | ((CharValue & $FF) >> 6)) << 0) | ((%10000000 | (CharValue & $3F)) << 8)
    ;Debug Hex(UTF8Value)
    Result$ = PeekS(@UTF8Value, 2, #PB_UTF8|#PB_ByteLength)
  ElseIf CharValue > 2048 And CharValue <= 63488
    UTF8Value = ((%11100000 | (CharValue >> 12)) << 0) | ((%10000000 | (((CharValue & $FFF) >> 8) << 2) | ((CharValue & $FF) >> 6)) << 8) | ((%10000000 | (CharValue & $3F)) << 16)
    ;Debug Hex(UTF8Value)
    Result$ = PeekS(@UTF8Value, 3, #PB_UTF8|#PB_ByteLength)
  ElseIf CharValue > 65536 And CharValue <= 1048576
    UTF8Value = ((%11110000 | (CharValue >> 18)) << 0) | ((%10000000 | ((CharValue & $3F000) >> 12)) << 8) | ((%10000000 | ((CharValue & $FFF) >> 6)) << 16) | ((%10000000 | (CharValue & $3F)) << 24)
    ;Debug Hex(UTF8Value, #PB_Long)
    Result$ = PeekS(@UTF8Value, 4, #PB_UTF8|#PB_ByteLength)
  EndIf
  
  ProcedureReturn Result$
  
EndProcedure


Debug ChrExtended($79)
Debug ChrExtended($E4)
Debug ChrExtended($3A9)
Debug ChrExtended($20AC)
Debug ChrExtended($1D11E)




Procedure.s HTMLEntityDecoder(html$)
  
  Protected Result$, CharValue.l
  
  
  If Left(html$, 2) = "&#"
    If Mid(html$, 3, 1) = "x"
      CharValue = Val("$" + Mid(html$, 4))
    Else
      CharValue = Val(Mid(html$, 3))
    EndIf
    Result$ = ChrExtended(CharValue)
  EndIf
  
  ProcedureReturn Result$
  
EndProcedure



#zebra$ = "&#x1F993;"
#lzeta$ = "&#x3B6;"
#uZeta$ = "&#x396;"
#zombie$ = "&#x1F9DF;"
#zzz$ = "&#x1F4A4;"

#FontSize = 40

Define LineHeight.i

If LoadFont(0, "Segoe UI Emoji", #FontSize)
;If LoadFont(0, "Noto Color Emoji", #FontSize)
  SetGadgetFont(#PB_Default, FontID(0))
EndIf

If CreateImage(0, 1, 1)
  If StartDrawing(ImageOutput(0))
    DrawingFont(FontID(0))
    LineHeight = TextHeight(" ") + 2
    StopDrawing()
  EndIf
  FreeImage(0)
EndIf
Debug LineHeight

If OpenWindow(0, 0, 0, 300, LineHeight * 6, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  EditorGadget(0, 0, 0, 300, LineHeight * 6)
  AddGadgetItem(0, 1, "zebra: " + HTMLEntityDecoder(#zebra$))
  AddGadgetItem(0, 2, "zeta: " + HTMLEntityDecoder(#lzeta$))
  AddGadgetItem(0, 3, "Zeta: " + HTMLEntityDecoder(#uZeta$))
  AddGadgetItem(0, 4, "zombie: " + HTMLEntityDecoder(#zombie$))
  AddGadgetItem(0, 5, "zzz: " + HTMLEntityDecoder(#zzz$))
  AddGadgetItem(0, 6, "Smily: " + ChrExtended($1F61B))
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Post Reply