Skip to content

Commit

Permalink
Fixing rendering.
Browse files Browse the repository at this point in the history
This also introduces ability to create tags in the mapping file.
  • Loading branch information
navaneeth committed Jul 28, 2012
1 parent 794283a commit 335a327
Show file tree
Hide file tree
Showing 16 changed files with 341 additions and 385 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ list (APPEND VARNAM_SOURCES
vword.c
learn.c
rendering.c
rendering/ml_unicode.c
renderer/ml_unicode.c
varnam.c
)

Expand Down
73 changes: 73 additions & 0 deletions renderer/ml_unicode.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* ml_unicode.c
Copyright (C) 2010 Navaneeth.K.N
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <string.h>

#include "renderers.h"
#include "../varnam-util.h"
#include "../varnam-types.h"
#include "../varnam-result-codes.h"
#include "../varnam-symbol-table.h"

#define RENDER_VALUE2_TAG "render_value2"
#define CHIL_TAG "chill"

int
ml_unicode_renderer(varnam *handle,
vtoken *previous,
vtoken *current,
strbuf *output)
{
int rc;
vtoken *virama;

rc = vst_get_virama (handle, &virama);
if (rc) return rc;

/* Not sure about this */
/* if(strcmp(match->pattern, "r") == 0 || strcmp(match->pattern, "R") == 0) { */
/* if(handle->internal->last_token_available && !strbuf_endswith(output, virama->value1)) { */
/* strbuf_add(output, "ര്‍"); */
/* return VARNAM_SUCCESS; */
/* } */
/* } */

if (strcmp(current->tag, RENDER_VALUE2_TAG) == 0 && previous != NULL)
{
strbuf_add(output, current->value2);
return VARNAM_SUCCESS;
}

return VARNAM_PARTIAL_RENDERING;
}

int
ml_unicode_rtl_renderer(varnam *handle,
vtoken *previous,
vtoken *current,
strbuf *output)
{
if (strcmp(current->tag, CHIL_TAG) == 0) {
strbuf_add (output, current->pattern);
strbuf_add (output, "_");
return VARNAM_SUCCESS;
}

return VARNAM_PARTIAL_RENDERING;
}
18 changes: 14 additions & 4 deletions rendering/renderers.h → renderer/renderers.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,22 @@ License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "../varnam-types.h"

#ifndef RENDERING_RENDERERS_H_INCLUDED_105305
#define RENDERING_RENDERERS_H_INCLUDED_105305

int ml_unicode_renderer(varnam*, struct token*, struct strbuf*);
int ml_unicode_rtl_renderer(varnam*, struct token*, struct strbuf*);
#include "../varnam-types.h"
#include "../varnam-util.h"

int
ml_unicode_renderer(varnam *handle,
vtoken *previous,
vtoken *current,
strbuf *output);

int
ml_unicode_rtl_renderer(varnam *handle,
vtoken *previous,
vtoken *current,
strbuf *output);

#endif
132 changes: 98 additions & 34 deletions rendering.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,48 +17,51 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */

#include <assert.h>
#include <string.h>

#include "varnam-symbol-table.h"
#include "varnam-result-codes.h"
#include "varnam-array.h"
#include "varnam-types.h"
#include "rendering.h"
#include "vword.h"
#include "varnam-api.h"
#include "varnam-array.h"

struct varnam_token_rendering*
get_additional_rendering_rule(varnam *handle)
static vtoken_renderer*
get_renderer(varnam *handle)
{
/* struct varnam_token_rendering *tr; */
/* int i; */

if(handle->internal->renderers == NULL) return NULL;

/* Will be fixed when this module is touched */
/* if(handle->internal->scheme_identifier[0] == '\0') { */
/* fill_general_values(handle, handle->internal->scheme_identifier, "scheme_identifier"); */
/* } */

/* Will be fixed when this module is touched */
/* for(i = 0; i < 1; i++) */
/* { */
/* tr = &(handle->internal->renderers[i]); */
/* if(strcmp(tr->scheme_identifier, handle->internal->scheme_identifier) == 0) */
/* return tr; */
/* } */
vtoken_renderer *r;
int i;

const char *scheme_id = varnam_get_scheme_identifier (handle);
if (scheme_id == NULL) {
varnam_log (handle, "Scheme id is not set. Custom rendering will not be processed");
return NULL;
}

for (i = 0; i < varray_length (v_->renderers); i++)
{
r = varray_get (v_->renderers, i);
assert (r);
if (strcmp(r->scheme_id, scheme_id) == 0)
return r;
}

return NULL;
}

/* Resolves the tokens.
/* Resolves the tokens.
* tokens will be a single dimensional array where each item is vtoken instances
*/
int
resolve_tokens(varnam *handle,
varray *tokens,
vword **word)
{
vtoken *virama, *token, *previous;
vtoken *virama, *token = NULL, *previous = NULL;
strbuf *string;
struct varnam_token_rendering *rule;
vtoken_renderer *r;
int rc, i;

assert(handle);
Expand All @@ -67,28 +70,30 @@ resolve_tokens(varnam *handle,
if (rc)
return rc;

/* will be fixed after implementing register_renderer() */
/* rule = get_additional_rendering_rule(handle); */
/* if(rule != NULL) { */
/* rc = rule->render(handle, match, string); */
/* if(rc == VARNAM_SUCCESS) { */
/* return; */
/* } */
/* } */

string = get_pooled_string (handle);
for(i = 0; i < varray_length(tokens); i++)
{
token = varray_get (tokens, i);
if (token->type == VARNAM_TOKEN_VIRAMA)

r = get_renderer (handle);
if (r != NULL)
{
/* we are resolving a virama. If the output ends with a virama already, add a
rc = r->rtl (handle, previous, token, string);
if (rc == VARNAM_ERROR)
return rc;
if (rc == VARNAM_SUCCESS)
continue;
}

if (token->type == VARNAM_TOKEN_VIRAMA)
{
/* we are resolving a virama. If the output ends with a virama already, add a
ZWNJ to it, so that following character will not be combined.
if output not ends with virama, add a virama and ZWNJ */
if(strbuf_endswith (string, virama->value1)) {
strbuf_add (string, ZWNJ());
}
else {
else {
strbuf_add (string, virama->value1);
strbuf_add (string, ZWNJ());
}
Expand Down Expand Up @@ -119,3 +124,62 @@ resolve_tokens(varnam *handle,
*word = get_pooled_word (handle, strbuf_to_s (string), 1);
return VARNAM_SUCCESS;
}

/*
* Resolve tokens for reverse transliteration. tokens will be multidimensional array
*/
int
resolve_rtl_tokens(varnam *handle,
varray *all_tokens,
char **output)
{
int rc, i, j;
vtoken_renderer *r;
strbuf *rtl;
vtoken *token = NULL, *previous = NULL;
varray *tokens;

assert (handle);
assert (all_tokens);

rtl = get_pooled_string (handle);
assert (rtl);
for (i = 0; i < varray_length (all_tokens); i++)
{
tokens = varray_get (all_tokens, i);
assert (tokens);
for (j = 0; j < varray_length (tokens); j++)
{
token = varray_get (tokens, j);
assert (token);

r = get_renderer (handle);
if (r != NULL)
{
rc = r->rtl (handle, previous, token, rtl);
if (rc == VARNAM_ERROR)
return rc;
if (rc == VARNAM_SUCCESS) {
previous = token;
break;
}
}

strbuf_add (rtl, token->pattern);

/* vowel is standing in it's full form in between a word. need to prefix _
to avoid unnecessary conjunctions */
if (token->type == VARNAM_TOKEN_VOWEL && previous != NULL)
{
if (strcmp(token->value1, previous->value1) == 0)
strbuf_add (rtl, "_");
}

previous = token;
break; /* We only care about first element in each array */
}
}

*output = rtl->buffer;
return VARNAM_SUCCESS;
}
6 changes: 4 additions & 2 deletions rendering.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ resolve_tokens(varnam *handle,
varray *tokens,
vword **word);

struct varnam_token_rendering*
get_additional_rendering_rule(varnam *handle);
int
resolve_rtl_tokens(varnam *handle,
varray *tokens,
char **output);

#endif
73 changes: 0 additions & 73 deletions rendering/ml_unicode.c

This file was deleted.

Loading

0 comments on commit 335a327

Please sign in to comment.