static Variant HHVM_FUNCTION( fribidi_log2vis, const String& logical_str, int64_t direction, int64_t charset ) { char * visual_str; int logical_str_len, visual_str_len, ustr_len; FriBidiParType base_direction; FriBidiLevel status; FriBidiChar *logical_ustr, *visual_ustr; logical_str_len = logical_str.length(); if (!_direction_is_valid(direction)) { raise_warning("Uknown direction."); return false; } if (!_charset_is_valid(charset)) { raise_warning("Uknown charset."); return false; } // Convert input string to internal Unicode logical_ustr = (FriBidiChar*) emalloc(sizeof(FriBidiChar) * logical_str_len); ustr_len = fribidi_charset_to_unicode( (FriBidiCharSet)charset, logical_str.c_str(), logical_str_len, logical_ustr ); // Visualize the Unicode string base_direction = direction; visual_ustr = (FriBidiChar*) emalloc(sizeof(FriBidiChar) * ustr_len); status = fribidi_log2vis( logical_ustr, ustr_len, &base_direction, visual_ustr, nullptr, nullptr, nullptr); efree(logical_ustr); // Return false if FriBidi failed if (status == 0) { efree(visual_ustr); return false; } // Convert back from internal Unicode to original character set visual_str_len = 4 * ustr_len; visual_str = (char *) emalloc(sizeof(char) * visual_str_len); visual_str_len = fribidi_unicode_to_charset( (FriBidiCharSet)charset, visual_ustr, ustr_len, visual_str); efree(visual_ustr); String result(visual_str, visual_str_len, CopyString); efree(visual_str); return result; }
int main ( int argc, char **argv) { int options; char s[BUFLEN]; unichar us[BUFLEN], cus[BUFLEN]; int len; FriBidiCharSet utf8; options = argc >= 2 ? atoi (argv[1]) : 0; utf8 = fribidi_parse_charset ("UTF-8"); while (fgets (s, BUFLEN, stdin)) { char *newline; len = strlen (s); if (s[len - 1] == '\n') { newline = "\n"; s[--len] = '\0'; } else newline = ""; len = fribidi_charset_to_unicode (utf8, s, len, us); bconsole_log2con (us, len, cus, &len, options); len = fribidi_unicode_to_charset (utf8, cus, len, s); *(s + len) = '\0'; printf ("%s%s", s, newline); } return 0; }
/** Get the width of a string. */ int GetStringWidth(FontType ft, const char *str) { #ifdef USE_XFT XGlyphInfo extents; #endif #ifdef USE_FRIBIDI FriBidiChar *temp_i; FriBidiChar *temp_o; FriBidiParType type = FRIBIDI_PAR_ON; int unicodeLength; #endif int len; char *output; int result; char *utf8String; /* Convert to UTF-8 if necessary. */ utf8String = GetUTF8String(str); /* Length of the UTF-8 string. */ len = strlen(utf8String); /* Apply the bidi algorithm if requested. */ #ifdef USE_FRIBIDI temp_i = AllocateStack((len + 1) * sizeof(FriBidiChar)); temp_o = AllocateStack((len + 1) * sizeof(FriBidiChar)); unicodeLength = fribidi_charset_to_unicode(FRIBIDI_CHAR_SET_UTF8, utf8String, len, temp_i); fribidi_log2vis(temp_i, unicodeLength, &type, temp_o, NULL, NULL, NULL); output = AllocateStack(4 * len + 1); fribidi_unicode_to_charset(FRIBIDI_CHAR_SET_UTF8, temp_o, unicodeLength, (char*)output); len = strlen(output); #else output = utf8String; #endif /* Get the width of the string. */ #ifdef USE_XFT JXftTextExtentsUtf8(display, fonts[ft], (const unsigned char*)output, len, &extents); result = extents.xOff; #else result = XTextWidth(fonts[ft], output, len); #endif /* Clean up. */ #ifdef USE_FRIBIDI ReleaseStack(temp_i); ReleaseStack(temp_o); ReleaseStack(output); #endif ReleaseUTF8String(utf8String); return result; }
std::wstring getVisualLine(const std::string&instring) { std::wstring outstring; const FriBidiCharSet enc= FRIBIDI_CHAR_SET_UTF8; FriBidiParType direction=FRIBIDI_PAR_ON; size_t len2=instring.size()*2; FriBidiChar*str_in = new FriBidiChar[len2]; FriBidiChar*str_out = new FriBidiChar[len2]; /* convert UTF8 to UTF32 */ FriBidiStrIndex ulen = fribidi_charset_to_unicode(enc, instring.c_str(), instring.size(), str_in); /* reshape the UTF32 string */ FriBidiLevel lvl = fribidi_log2vis(str_in, ulen, &direction, str_out, 0, 0, 0); if(lvl) { outstring=std::wstring((wchar_t*)str_out); } else { //outstring=instring; } delete[]str_in; delete[]str_out; return outstring; }
/** * raqm_set_text_utf8: * @rq: a #raqm_t. * @text: a UTF-8 encoded text string. * @len: the length of @text. * * Same as raqm_set_text(), but for text encoded in UTF-8 encoding. * * Return value: * %true if no errors happened, %false otherwise. * * Since: 0.1 */ bool raqm_set_text_utf8 (raqm_t *rq, const char *text, size_t len) { #ifdef _MSC_VER uint32_t *unicode = _alloca (len); #else uint32_t unicode[len]; #endif size_t ulen; if (!rq || !text || !len) return false; RAQM_TEST ("Text is: %s\n", text); rq->flags |= RAQM_FLAG_UTF8; ulen = fribidi_charset_to_unicode (FRIBIDI_CHAR_SET_UTF8, text, len, unicode); return raqm_set_text (rq, unicode, ulen); }
static int shape_text(AVFilterContext *ctx) { DrawTextContext *s = ctx->priv; uint8_t *tmp; int ret = AVERROR(ENOMEM); static const FriBidiFlags flags = FRIBIDI_FLAGS_DEFAULT | FRIBIDI_FLAGS_ARABIC; FriBidiChar *unicodestr = NULL; FriBidiStrIndex len; FriBidiParType direction = FRIBIDI_PAR_LTR; FriBidiStrIndex line_start = 0; FriBidiStrIndex line_end = 0; FriBidiLevel *embedding_levels = NULL; FriBidiArabicProp *ar_props = NULL; FriBidiCharType *bidi_types = NULL; FriBidiStrIndex i,j; len = strlen(s->text); if (!(unicodestr = av_malloc_array(len, sizeof(*unicodestr)))) { goto out; } len = fribidi_charset_to_unicode(FRIBIDI_CHAR_SET_UTF8, s->text, len, unicodestr); bidi_types = av_malloc_array(len, sizeof(*bidi_types)); if (!bidi_types) { goto out; } fribidi_get_bidi_types(unicodestr, len, bidi_types); embedding_levels = av_malloc_array(len, sizeof(*embedding_levels)); if (!embedding_levels) { goto out; } if (!fribidi_get_par_embedding_levels(bidi_types, len, &direction, embedding_levels)) { goto out; } ar_props = av_malloc_array(len, sizeof(*ar_props)); if (!ar_props) { goto out; } fribidi_get_joining_types(unicodestr, len, ar_props); fribidi_join_arabic(bidi_types, len, embedding_levels, ar_props); fribidi_shape(flags, embedding_levels, len, ar_props, unicodestr); for (line_end = 0, line_start = 0; line_end < len; line_end++) { if (is_newline(unicodestr[line_end]) || line_end == len - 1) { if (!fribidi_reorder_line(flags, bidi_types, line_end - line_start + 1, line_start, direction, embedding_levels, unicodestr, NULL)) { goto out; } line_start = line_end + 1; } } /* Remove zero-width fill chars put in by libfribidi */ for (i = 0, j = 0; i < len; i++) if (unicodestr[i] != FRIBIDI_CHAR_FILL) unicodestr[j++] = unicodestr[i]; len = j; if (!(tmp = av_realloc(s->text, (len * 4 + 1) * sizeof(*s->text)))) { /* Use len * 4, as a unicode character can be up to 4 bytes in UTF-8 */ goto out; } s->text = tmp; len = fribidi_unicode_to_charset(FRIBIDI_CHAR_SET_UTF8, unicodestr, len, s->text); ret = 0; out: av_free(unicodestr); av_free(embedding_levels); av_free(ar_props); av_free(bidi_types); return ret; }
/** Display a string. */ void RenderString(Drawable d, FontType font, ColorType color, int x, int y, int width, const char *str) { #ifdef USE_ICONV static char isUTF8 = -1; #endif XRectangle rect; Region renderRegion; int len; char *output; #ifdef USE_FRIBIDI FriBidiChar *temp_i; FriBidiChar *temp_o; FriBidiParType type = FRIBIDI_PAR_ON; int unicodeLength; #endif #ifdef USE_XFT XGlyphInfo extents; #endif char *utf8String; /* Early return for empty strings. */ if(!str || !str[0]) { return; } /* Convert to UTF-8 if necessary. */ utf8String = GetUTF8String(str); /* Get the length of the UTF-8 string. */ len = strlen(utf8String); /* Apply the bidi algorithm if requested. */ #ifdef USE_FRIBIDI temp_i = AllocateStack((len + 1) * sizeof(FriBidiChar)); temp_o = AllocateStack((len + 1) * sizeof(FriBidiChar)); unicodeLength = fribidi_charset_to_unicode(FRIBIDI_CHAR_SET_UTF8, utf8String, len, temp_i); fribidi_log2vis(temp_i, unicodeLength, &type, temp_o, NULL, NULL, NULL); output = AllocateStack(4 * len + 1); fribidi_unicode_to_charset(FRIBIDI_CHAR_SET_UTF8, temp_o, unicodeLength, (char*)output); len = strlen(output); #else output = utf8String; #endif /* Get the bounds for the string based on the specified width. */ rect.x = x; rect.y = y; rect.height = GetStringHeight(font); #ifdef USE_XFT JXftTextExtentsUtf8(display, fonts[font], (const unsigned char*)output, len, &extents); rect.width = extents.xOff; #else rect.width = XTextWidth(fonts[font], output, len); #endif rect.width = Min(rect.width, width) + 2; /* Combine the width bounds with the region to use. */ renderRegion = XCreateRegion(); XUnionRectWithRegion(&rect, renderRegion, renderRegion); /* Display the string. */ #ifdef USE_XFT JXftDrawChange(xd, d); JXftDrawSetClip(xd, renderRegion); JXftDrawStringUtf8(xd, GetXftColor(color), fonts[font], x, y + fonts[font]->ascent, (const unsigned char*)output, len); JXftDrawChange(xd, rootWindow); #else JXSetForeground(display, fontGC, colors[color]); JXSetRegion(display, fontGC, renderRegion); JXSetFont(display, fontGC, fonts[font]->fid); JXDrawString(display, d, fontGC, x, y + fonts[font]->ascent, output, len); #endif /* Free any memory used for UTF conversion. */ #ifdef USE_FRIBIDI ReleaseStack(temp_i); ReleaseStack(temp_o); ReleaseStack(output); #endif ReleaseUTF8String(utf8String); XDestroyRegion(renderRegion); }
int main (int argc, char *argv[]) { int exit_val; fribidi_boolean file_found; char *s; FILE *IN; text_width = 80; do_break = FRIBIDI_TRUE; do_pad = FRIBIDI_TRUE; do_mirror = FRIBIDI_TRUE; do_clean = FRIBIDI_FALSE; do_reorder_nsm = FRIBIDI_FALSE; show_input = FRIBIDI_FALSE; show_visual = FRIBIDI_TRUE; show_basedir = FRIBIDI_FALSE; show_ltov = FRIBIDI_FALSE; show_vtol = FRIBIDI_FALSE; show_levels = FRIBIDI_FALSE; show_changes = FRIBIDI_FALSE; char_set = "UTF-8"; bol_text = NULL; eol_text = NULL; input_base_direction = FRIBIDI_TYPE_ON; if ((s = getenv ("COLUMNS"))) { int i; i = atoi (s); if (i > 0) text_width = i; } #define CHARSETDESC 257 #define CAPRTL 258 /* Parse the command line with getopt library */ /* Must set argv[0], getopt uses it to generate error messages */ argv[0] = appname; while (1) { int option_index = 0, c; static struct option long_options[] = { {"help", 0, 0, 'h'}, {"version", 0, 0, 'V'}, {"verbose", 0, 0, 'v'}, {"debug", 0, 0, 'd'}, {"test", 0, 0, 't'}, {"charset", 1, 0, 'c'}, #ifndef FRIBIDI_NO_CHARSETS {"charsetdesc", 1, 0, CHARSETDESC}, {"caprtl", 0, 0, CAPRTL}, #endif {"showinput", 0, &show_input, FRIBIDI_TRUE}, {"nopad", 0, &do_pad, FRIBIDI_FALSE}, {"nobreak", 0, &do_break, FRIBIDI_FALSE}, {"width", 1, 0, 'w'}, {"bol", 1, 0, 'B'}, {"eol", 1, 0, 'E'}, {"nomirror", 0, &do_mirror, FRIBIDI_FALSE}, {"reordernsm", 0, &do_reorder_nsm, FRIBIDI_TRUE}, {"clean", 0, &do_clean, FRIBIDI_TRUE}, {"ltr", 0, (int *) &input_base_direction, FRIBIDI_TYPE_L}, {"rtl", 0, (int *) &input_base_direction, FRIBIDI_TYPE_R}, {"wltr", 0, (int *) &input_base_direction, FRIBIDI_TYPE_WL}, {"wrtl", 0, (int *) &input_base_direction, FRIBIDI_TYPE_WR}, {"basedir", 0, &show_basedir, FRIBIDI_TRUE}, {"ltov", 0, &show_ltov, FRIBIDI_TRUE}, {"vtol", 0, &show_vtol, FRIBIDI_TRUE}, {"levels", 0, &show_levels, FRIBIDI_TRUE}, {"changes", 0, &show_changes, FRIBIDI_TRUE}, {"novisual", 0, &show_visual, FRIBIDI_FALSE}, {0, 0, 0, 0} }; c = getopt_long (argc, argv, "hVvdtc:w:B:E:", long_options, &option_index); if (c == -1) break; switch (c) { case 0: break; case 'h': help (); break; case 'V': version (); break; case 'v': show_basedir = FRIBIDI_TRUE; show_ltov = FRIBIDI_TRUE; show_vtol = FRIBIDI_TRUE; show_levels = FRIBIDI_TRUE; show_changes = FRIBIDI_TRUE; break; case 'w': text_width = atoi (optarg); if (text_width <= 0) die ("invalid screen width `%s'\n", optarg); break; case 'B': bol_text = optarg; break; case 'E': eol_text = optarg; break; case 'd': if (!fribidi_set_debug (FRIBIDI_TRUE)) die ("%s lib must be compiled with DEBUG option to enable\nturn debug info on.\n", FRIBIDI_PACKAGE); break; case 't': do_clean = FRIBIDI_TRUE; show_input = FRIBIDI_TRUE; do_break = FRIBIDI_FALSE; do_reorder_nsm = FRIBIDI_TRUE; break; case 'c': char_set = strdup (optarg); break; #ifndef FRIBIDI_NO_CHARSETS case CAPRTL: char_set = "CapRTL"; break; case CHARSETDESC: char_set = strdup (optarg); char_set_num = fribidi_parse_charset (char_set); if (!char_set_num) die ("unrecognized character set `%s'\n", char_set); if (!fribidi_char_set_desc (char_set_num)) die ("no description available for character set `%s'\n", fribidi_char_set_name (char_set_num)); else printf ("Descriptions for character set %s:\n" "\n" "%s", fribidi_char_set_title (char_set_num), fribidi_char_set_desc (char_set_num)); exit (0); break; #endif case ':': case '?': die (NULL); break; default: break; } } #ifdef FRIBIDI_NO_CHARSETS to_ucs4 = iconv_open ("WCHAR_T", char_set); from_ucs4 = iconv_open (char_set, "WCHAR_T"); #else char_set_num = fribidi_parse_charset (char_set); #endif #ifdef FRIBIDI_NO_CHARSETS if (to_ucs4 == (iconv_t) (-1) || from_ucs4 == (iconv_t) (-1)) #else if (!char_set_num) #endif die ("unrecognized character set `%s'\n", char_set); fribidi_set_mirroring (do_mirror); fribidi_set_reorder_nsm (do_reorder_nsm); exit_val = 0; file_found = FRIBIDI_FALSE; while (optind < argc || !file_found) { char *S_; S_ = optind < argc ? argv[optind++] : "-"; file_found = FRIBIDI_TRUE; /* Open the infile for reading */ if (S_[0] == '-' && !S_[1]) { IN = stdin; } else { IN = fopen (S_, "r"); if (!IN) { fprintf (stderr, "%s: %s: no such file or directory\n", appname, S_); exit_val = 1; continue; } } /* Read and process input one line at a time */ { char S_[MAX_STR_LEN]; int padding_width, break_width; padding_width = show_input ? (text_width - 10) / 2 : text_width; break_width = do_break ? padding_width : 3 * MAX_STR_LEN; while (fgets (S_, sizeof (S_) - 1, IN)) { char *new_line, *nl_found; FriBidiChar logical[MAX_STR_LEN]; char outstring[MAX_STR_LEN]; FriBidiCharType base; FriBidiStrIndex len; nl_found = ""; S_[sizeof (S_) - 1] = 0; len = strlen (S_); /* chop */ if (S_[len - 1] == '\n') { len--; S_[len] = '\0'; new_line = "\n"; } else new_line = ""; #ifdef FRIBIDI_NO_CHARSETS { char *st = S_, *ust = (char *) logical; int in_len = (int) len; len = sizeof logical; iconv (to_ucs4, &st, &in_len, &ust, (int *) &len); len = (FriBidiChar *) ust - logical; } #else len = fribidi_charset_to_unicode (char_set_num, S_, len, logical); #endif { FriBidiChar *visual; FriBidiStrIndex *ltov, *vtol; FriBidiLevel *levels; FriBidiStrIndex new_len; fribidi_boolean log2vis; visual = show_visual ? ALLOCATE (FriBidiChar, len + 1) : NULL; ltov = show_ltov ? ALLOCATE (FriBidiStrIndex, len + 1) : NULL; vtol = show_vtol ? ALLOCATE (FriBidiStrIndex, len + 1) : NULL; levels = show_levels ? ALLOCATE (FriBidiLevel, len + 1) : NULL; /* Create a bidi string. */ base = input_base_direction; log2vis = fribidi_log2vis (logical, len, &base, /* output */ visual, ltov, vtol, levels); if (log2vis) { if (show_input) printf ("%-*s => ", padding_width, S_); new_len = len; /* Remove explicit marks, if asked for. */ if (do_clean) len = fribidi_remove_bidi_marks (visual, len, ltov, vtol, levels); if (show_visual) { printf (nl_found); if (bol_text) printf ("%s", bol_text); /* Convert it to input charset and print. */ { FriBidiStrIndex idx, st; for (idx = 0; idx < len;) { FriBidiStrIndex wid, inlen; wid = break_width; st = idx; #ifndef FRIBIDI_NO_CHARSETS if (char_set_num != FRIBIDI_CHAR_SET_CAP_RTL) #endif while (wid > 0 && idx < len) wid -= fribidi_wcwidth (visual[idx++]); #ifndef FRIBIDI_NO_CHARSETS else while (wid > 0 && idx < len) { wid--; idx++; } #endif if (wid < 0 && idx > st + 1) idx--; inlen = idx - st; #ifdef FRIBIDI_NO_CHARSETS { char *str = outstring, *ust = (char *) (visual + st); int in_len = inlen * sizeof visual[0]; new_len = sizeof outstring; iconv (from_ucs4, &ust, &in_len, &str, (int *) &new_len); *str = '\0'; new_len = str - outstring; } #else new_len = fribidi_unicode_to_charset (char_set_num, visual + st, inlen, outstring); #endif if (FRIBIDI_IS_RTL (base)) printf ("%*s", (int) (do_pad ? (padding_width + strlen (outstring) - (break_width - wid)) : 0), outstring); else printf ("%s", outstring); if (idx < len) printf ("\n"); } } if (eol_text) printf ("%s", eol_text); nl_found = "\n"; } if (show_basedir) { printf (nl_found); printf ("Base direction: %s", (FRIBIDI_DIR_TO_LEVEL (base) ? "R" : "L")); nl_found = "\n"; } if (show_ltov) { FriBidiStrIndex i; printf (nl_found); for (i = 0; i < len; i++) printf ("%ld ", (long) ltov[i]); nl_found = "\n"; } if (show_vtol) { FriBidiStrIndex i; printf (nl_found); for (i = 0; i < len; i++) printf ("%ld ", (long) vtol[i]); nl_found = "\n"; } if (show_levels) { FriBidiStrIndex i; printf (nl_found); for (i = 0; i < len; i++) printf ("%d ", (int) levels[i]); nl_found = "\n"; } if (show_changes) { FriBidiStrIndex change_start, change_len; fribidi_find_string_changes (logical, len, visual, new_len, &change_start, &change_len); printf ("%sChange start[length] = %d[%d]", nl_found, change_start, change_len); nl_found = "\n"; } } else { exit_val = 2; } if (show_visual) free (visual); if (show_ltov) free (ltov); if (show_vtol) free (vtol); if (show_levels) free (levels); } if (*nl_found) printf (new_line); } } } return exit_val; }
char *FBidiConvert( const char *logical_str, const char *charset, int str_len, Bool *is_rtl, int *out_len, superimpose_char_t *comb_chars, int *l_to_v) { char *visual_str; FriBidiCharSet fribidi_charset; FriBidiChar *logical_unicode_str; FriBidiChar *visual_unicode_str; FriBidiParType pbase_dir = FRIBIDI_TYPE_ON; FriBidiStrIndex *pos_l_to_v; int i; if (logical_str == NULL || charset == NULL) { return NULL; } if (str_len < 0) { str_len = strlen(logical_str); } if (is_rtl != NULL) { *is_rtl = False; } fribidi_charset = fribidi_parse_charset((char *)charset); if (fribidi_charset == FRIBIDI_CHAR_SET_NOT_FOUND) { return NULL; } /* it is possible that we allocate a bit more here, if utf-8 */ logical_unicode_str = (FriBidiChar *)safemalloc((str_len + 1) * sizeof(FriBidiChar)); /* convert to unicode first */ str_len = fribidi_charset_to_unicode( fribidi_charset, (char *)logical_str, str_len, logical_unicode_str); visual_unicode_str = (FriBidiChar *)safemalloc((str_len + 1) * sizeof(FriBidiChar)); /* apply bidi algorithm, convert logical string to visual string */ /* also keep track of how characters are reordered here, to reorder combing characters accordingly */ pos_l_to_v = (FriBidiStrIndex *)safemalloc((str_len + 1) * sizeof(FriBidiStrIndex)); fribidi_log2vis( logical_unicode_str, str_len, &pbase_dir, visual_unicode_str, pos_l_to_v, NULL, NULL); /* remap mapping from logical to visual to "compensate" for BIDI */ if (comb_chars != NULL) { for (i = 0; comb_chars[i].c.byte1 != 0 || comb_chars[i].c.byte2 != 0; i++) { /* if input string is zero characters => only combining chars, set position to zero */ comb_chars[i].position = str_len != 0 ? pos_l_to_v[comb_chars[i].position] : 0; } } if (l_to_v != NULL) { /* values in the previuos mapping gives the position of input characters after combining step */ /* mapping from BIDI conversion maps from the positions in the output from combining */ int orig_len; int *l_to_v_temp; for (i = 0; l_to_v[i] != -1; i++) { } orig_len = i; l_to_v_temp = (int *)safemalloc(orig_len * sizeof(int)); for (i = 0; i < orig_len; i++) { l_to_v_temp[i] = pos_l_to_v[l_to_v[i]]; } for (i = 0; i < orig_len; i++) { l_to_v[i] = l_to_v_temp[i]; } free(l_to_v_temp); } free(pos_l_to_v); /* character shape/join - will get pulled into fribidi with time */ str_len = shape_n_join(visual_unicode_str, str_len); visual_str = (char *)safemalloc((4 * str_len + 1) * sizeof(char)); /* convert from unicode finally */ *out_len = fribidi_unicode_to_charset( fribidi_charset, visual_unicode_str, str_len, visual_str); if (is_rtl != NULL && fribidi_get_bidi_type(*visual_unicode_str) == FRIBIDI_TYPE_RTL) { *is_rtl = True; } free(logical_unicode_str); free(visual_unicode_str); return visual_str; }