os/textandloc/fontservices/textshaperplugin/test/letest/letest.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2  *******************************************************************************
     3  *
     4  *   Copyright (C) 1999-2003, International Business Machines
     5  *   Corporation and others.  All Rights Reserved.
     6  *
     7  *******************************************************************************
     8  *   file name:  letest.cpp
     9  *
    10  *   created on: 11/06/2000
    11  *   created by: Eric R. Mader
    12  */
    13 
    14 #include "math.h"
    15 
    16 #include "LETypes.h"
    17 #include "LayoutEngine.h"
    18 #include "PortableFontInstance.h"
    19 
    20 #include "letest.h"
    21 
    22 U_NAMESPACE_USE
    23 
    24 le_bool compareResults(le_int32 testNumber, TestResult *expected, TestResult *actual)
    25 {
    26     /* NOTE: we'll stop on the first failure 'cause once there's one error, it may cascade... */
    27     if (actual->glyphCount != expected->glyphCount) {
    28         printf("incorrect glyph count: exptected %d, got %d\n", expected->glyphCount, actual->glyphCount);
    29         return FALSE;
    30     }
    31 
    32     le_int32 i;
    33 
    34     for (i = 0; i < actual->glyphCount; i += 1) {
    35         if (actual->glyphs[i] != expected->glyphs[i]) {
    36             printf("incorrect id for glyph %d: expected %4X, got %4X\n", i, expected->glyphs[i], actual->glyphs[i]);
    37             return FALSE;
    38         }
    39     }
    40 
    41     for (i = 0; i < actual->glyphCount; i += 1) {
    42         if (actual->indices[i] != expected->indices[i]) {
    43             printf("incorrect index for glyph %d: expected %8X, got %8X\n", i, expected->indices[i], actual->indices[i]);
    44             return FALSE;
    45         }
    46     }
    47 
    48     for (i = 0; i <= actual->glyphCount; i += 1) {
    49         double xError = fabs(actual->positions[i * 2] - expected->positions[i * 2]);
    50 
    51         if (xError > 0.0001) {
    52             printf("incorrect x position for glyph %d: expected %f, got %f\n", i, expected->positions[i * 2], actual->positions[i * 2]);
    53             return FALSE;
    54         }
    55 
    56         double yError = fabs(actual->positions[i * 2 + 1] - expected->positions[i * 2 + 1]);
    57 
    58         if (yError < 0) {
    59             yError = -yError;
    60         }
    61 
    62         if (yError > 0.0001) {
    63             printf("incorrect y position for glyph %d: expected %f, got %f\n", i, expected->positions[i * 2 + 1], actual->positions[i * 2 + 1]);
    64             return FALSE;
    65         }
    66     }
    67 
    68     return TRUE;
    69 }
    70 
    71 int main_entry()
    72 {
    73     le_int32 failures = 0;
    74 
    75     for (le_int32 test = 0; test < testCount; test += 1) {
    76         LEErrorCode fontStatus = LE_NO_ERROR;
    77 
    78         printf("Test %d, font = %s... ", test, testInputs[test].fontName);
    79 
    80         PortableFontInstance fontInstance(testInputs[test].fontName, 12, fontStatus);
    81 
    82         if (LE_FAILURE(fontStatus)) {
    83             printf("could not open font.\n");
    84             continue;
    85         }
    86 
    87         LEErrorCode success = LE_NO_ERROR;
    88         LayoutEngine *engine = LayoutEngine::layoutEngineFactory(&fontInstance, testInputs[test].scriptCode, -1, success);
    89         le_int32 textLength = testInputs[test].textLength;
    90         le_bool result;
    91         TestResult actual;
    92 
    93         if (LE_FAILURE(success)) {
    94             // would be nice to print the script name here, but
    95             // don't want to maintain a table, and don't want to
    96             // require ICU just for the script name...
    97             printf("could not create a LayoutEngine.\n");
    98             continue;
    99         }
   100 
   101         actual.glyphCount = engine->layoutChars(testInputs[test].text, 0, textLength, textLength, testInputs[test].rightToLeft, 0, 0, success);
   102 
   103         actual.glyphs    = new LEGlyphID[actual.glyphCount];
   104         actual.indices   = new le_int32[actual.glyphCount];
   105         actual.positions = new float[actual.glyphCount * 2 + 2];
   106 
   107         engine->getGlyphs(actual.glyphs, success);
   108         engine->getCharIndices(actual.indices, success);
   109         engine->getGlyphPositions(actual.positions, success);
   110 
   111         result = compareResults(test, &testResults[test], &actual);
   112 
   113         if (result) {
   114             printf("passed.\n");
   115         } else {
   116             failures += 1;
   117             printf("failed.\n");
   118         }
   119 
   120         delete[] actual.positions;
   121         delete[] actual.indices;
   122         delete[] actual.glyphs;
   123         delete   engine;
   124     }
   125 	
   126     return failures;
   127 }