Currently, I am in the process of enhancing the python word cloud library available here by adding an HTML export option alongside the existing image export feature.
The main challenge I'm facing right now is related to how the underlying python image library handles text rendering. It defines the "box" around a word based on the upper edge of the largest character within the word. As a result, the box fits tightly around the text.
However, web browsers interpret HTML and CSS differently. Instead of creating the tightest possible box like the python library does, they adjust the box to accommodate all possible characters that could be present. For example, if a word only has lowercase characters, the browser will still leave some space at the top for uppercase characters.
My query now is how to replicate this behavior of having a "tight" box using CSS.
To provide a visual representation of what I mean, take a look at this image https://i.sstatic.net/WXQNQ.png
In the background, you can see the PNG output generated by the python library, with the HTML+CSS version overlaid on top. The small white boxes highlight the starting point of the box in the PNG file. I've added a 50% opacity background around the HTML words to illustrate their box model.
Here's the HTML code snippet used in this example:
<html>
<head>
<link href="https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/html5resetcss/html5reset-1.6.1.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Droid+Sans+Mono" rel="stylesheet">
</head>
<body>
<ul style="width:400px; height: 200px; background-image: url(test.png); position: absolute; top:0;left:0;">
<li style="background-color:rgba(255, 255, 255, 0.5); padding-left: 5.5em; display: inline; list-style: none; margin: 0; padding: 0; font-family: 'Droid Sans Mono', monospace;position: absolute; color: rgb(119, 209, 83); top: 64px; left: 15px; font-size: 102px">hallo</li>
<li style="background-color:rgba(255, 255, 255, 0.5); padding-left: 5.5em; display: inline; list-style: none; margin: 0; padding: 0; font-family: 'Droid Sans Mono', monospace;transform: rotate(270deg); transform-origin: 50% 90% 0;position: absolute; color: rgb(47, 180, 124); top: 51px; left: 321px; font-size: 73px">ich</li>
<li style="background-color:rgba(255, 255, 255, 0.5); padding-left: 5.5em; display: inline; list-style: none; margin: 0; padding: 0; font-family: 'Droid Sans Mono', monospace;position: absolute; color: rgb(108, 205, 90); top: 3px; left: 99px; font-size: 61px">test</li>
<li style="background-color:rgba(255, 255, 255, 0.5); padding-left: 5.5em; display: inline; list-style: none; margin: 0; padding: 0; font-family: 'Droid Sans Mono', monospace;position: absolute; color: rgb(189, 223, 38); top: 142px; left: 114px; font-size: 59px">sie</li>
<li style="background-color:rgba(255, 255, 255, 0.5); padding-left: 5.5em; display: inline; list-style: none; margin: 0; padding: 0; font-family: 'Droid Sans Mono', monospace;posit...
</ul>
</body>
</html>