Transforming text into it's appropriate RTL representation - right-to-left

I'm looking for any java library which would help in transforming a piece of text into it's appropriate RTL representation.
For e.g. the hebrew/arabic text when read from a file would be in the LTR representation, however after the transformation it ought to be shown in it's RTL format.
The requirement comes up since in the case of html's we can specify the dir as rtl or ltr. However when we want to have a text variant or a sms variant of the text, the stylesheets don't come into play at all.

There are two issues that come into play with RTL content. The first is the right alignment of the display. There's nothing you can do about that unless you have control of the output routines that are being called.
The second issue, which is arguably more important, is the base directionality of the text. This you can control to a certain extent by adding bidirectional formatting characters to your content.
For example, given a string in s, you could set the base direction to RTL, with something like this:
String[] lines = s.split("\n");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < lines.length; i++) {
sb.append("\u202B"); // right-to-left embedding
sb.append(lines[i]);
sb.append("\u202C\n"); // pop directional formatting
}
s = sb.toString();
What this is doing is splitting the text into multiple lines, then adding a right-to-left embedding character at the start of each line, and a pop directional formatting character at the end of each line.
The reason it is done line by line is because the embedding override only lasts for the length of the paragraph.
Example code on Ideone.com
Markup example for comparison

Related

iText7 MoveText vs SetFixedPosition different results

Why do I get different results when I use MoveText vs SetFixedPosition? I use the same x and y value but they print on the page in different location. If I use canvasPage.BeginText can it be formatted to use a width and word wrap
canvasPage.BeginText()
.SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD), 21)
.MoveText(X,Y)
Paragraph p = new Paragraph()
.Add(_disclaimer)
.SetFixedPosition(X,Y, canvasPage.W)
.SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA))
.SetFontColor(ColorConstants.BLACK)
.SetFontSize(12)
.SetTextAlignment(TextAlignment.LEFT);
The locations between the cases when you add text onto the canvas manually with low-level operations versus the case when you use high-level layout Paragraph objects will be different because Paragraph is responsible for multi-line layout and adds necessary margins / spacings for the text to look nice and not overlap with other content. The locations are still different only by a minor offset.
You cannot combine low-level beginText operation with high-level layout requirements (wrapping text across lines etc) - this is the responsibility of layout's Paragraph.

AMCharts bullets converted to gibberish on small charts

I've searched through the entire AMCharts 4 bullet documentation as well as numerous Google pages, but I am yet to find the answer. When chart is squeezed to a mobile screen, bullet labels above bars get updated to some complete gibberish (as in the example below left). Those are suppose to be various numeric values (example on the right), including some with a decimal point. No percentage or other symbols.
The text actually gets swapped from normal numbers to the 'processed food' as the screen is being resized. Logic dictates that I must be missing some setting that prevents this sort of undesirable behaviour.
Any help is highly appreciated!
The "gibberish" is supposed to be an ellipsis. It's likely that your page isn't encoded to UTF-8 or the font you're using does not have the Unicode character for an ellipsis.
You can either double-check your encoding, or, if you don't want the ellipsis as at all, disable truncate on your labels. Assuming you're using a LabelBullet for your column labels:
var valueLabel = series.bullets.push(new am4charts.LabelBullet());
// ...
valueLabel.label.truncate = false;
For me the only way to get it show properly was setting value as workingValue
var bullet = series.bullets.push(new am4charts.LabelBullet());
bullet.label.text = "{valueY.workingValue.formatNumber('#')}";
bullet.label.truncate = false;

What is the process to create a mirror-image of a JPG file without creating or using a decoder?

So, I'm kind of experimenting with image-manipulation at the byte-level (No 'Image' wrapper or libraries), so the language doesn't matter (I'm using C#) as much as the byte-manipulations, themselves.
I'm now trying to flip a a .jpg image (minus the magic number) over the x- and y-axis, but realized after some trial and error that I think the encoding is getting in the way. This is the code I'm using on a byte[] without the FF D8 or FFD9 includes:
//No magic number included
public class MirrorImgOverXAndYAxes : IFunction
{
//No magic number included
public byte[] Exec(byte[] jpgImage)
{
byte[] resultingImage = new byte[jpgImage.Length];
for (var i = jpgImage.Length - 1; i >= 0; i--)
{
var indexToInsert = jpgImage.Length - 1 - i;
resultingImage[indexToInsert] = jpgImage[i];
}
return resultingImage;
}
}
Right now, my assumption is that it's nowhere near as easy as this, and that I would have to build a full-blown decoder to be able to manipulate the image bytes like this.
Is it possible to create this mirror image without a decoder and would something like what I'm doing work?
Hi I guess just flipping a jpeg by switching the bytes position will not really work here, due to how jpg files are actually structured. if you want to learn more about that, you can look here enter link description here
But you could try to convert the image to a format that actually saves it's pixels within a single byte. For example bitmaps (.bmp).
There is no simple way to do a mirror image that will work in general without decoding.
One problem is that JPEG images are encoded in MCUs that are in turn composed of blocks of eight by eight pixels. For simplicity assume the MCU is an 8x8 block the image width is 15 pixels. That means there will be a dummy column off the edge. If you just flip that, the dummy column will be visible and an image column will be off the edge.

Getting the rect/height of the text inside a static control

Is there a way to achieve something similar to SysLink's LM_GETIDEALHEIGHT for static controls, i.e. getting the size (or at least the height) of the actual text?
I have a multilingual program, and I want to position the controls accordingly, otherwise there's blank space left for some of the languages, which is not great.
You can use DrawTextEx() with the DT_CALCRECT flag (thanks to Jonathan Potter for that addition). Then find the difference between top and bottom of the output RECT object.

Change color of specific text only in text box of BlackBerry

How to change color of specific text only in textbox for Blackberry applications?
Unfortunately the answer is roll your own - there's no editable text field control that lets you change the font color at all. #Mark Novakowski's answer is a standard way of working around that, but will change all the text to one color.
If you're not worried about supporting the Pearl-style SureType keyboard, then you can override keyChar on ActiveRichTextField and manually add characters to the text in ActiveRichTextField.
For SureType fields, the answer is even worse - you don't have low level access to the SureType APIs (for correctly dealing with the predictive text popup) for non-qwerty phones (the Pearl series) so you have to resort to some real trickery to get a fully custom text field to function correctly on those devices. It may come down to having to have multiple EditFields arranged in a manager, with e.g. one EditField overridden to show read (as in Marks' answer). The trick there will be dynamically creating and adding text fields as necessary.
Yes, sometimes the RIM API makes easy things easy, and hard things nearly impossible.
As Fostah said in his comment, the TextBox class isn't very flexible in terms of changing the look and feel. Even the look and feel of many of the "Field" classes are pretty hard to change, too.
However, if you have some flexibility and can use a Field such as EditField (or variant thereof) instead of TextBox, then it should be just case of overriding the paint method. Something like:
protected void paint(Graphics graphics) {
graphics.setColor(Color.RED);
super.paint(graphics);
}
Have you checked out ActiveRichTextField? I've only used RichTextField myself but it looks like ActiveRichTextField lets you specify colors for the text regions in addition to fonts & formatting. It takes a little bit of set up to get these fields going but check out the javadoc for RichTextField, it gives a pretty good explanation.
Here's some sample code:
int offsets[] = new int[]{0, 5, 11};
Font[] fonts = new Font[]{Font.getDefault(), Font.getDefault()};
int bg[] = new int[]{Color.WHITE, Color.WHITE};
int fg[] = new int[]{Color.BLACK, Color.GREEN};
byte attributes[] = new byte[]{0, 1};
add(new ActiveRichTextField("Hello world", offsets, attributes, fonts, fg, bg, 0));

Resources