| OLD | NEW |
| 1 // Copyright 2013 Google Inc. | 1 // Copyright 2013 Google Inc. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not | 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 // use this file except in compliance with the License. You may obtain a copy of | 4 // use this file except in compliance with the License. You may obtain a copy of |
| 5 // the License at | 5 // the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 } | 102 } |
| 103 | 103 |
| 104 LibLouisWrapper::~LibLouisWrapper() { | 104 LibLouisWrapper::~LibLouisWrapper() { |
| 105 lou_free(); | 105 lou_free(); |
| 106 } | 106 } |
| 107 | 107 |
| 108 const char* LibLouisWrapper::tables_dir() const { | 108 const char* LibLouisWrapper::tables_dir() const { |
| 109 return "/liblouis/tables"; | 109 return "/liblouis/tables"; |
| 110 } | 110 } |
| 111 | 111 |
| 112 bool LibLouisWrapper::CheckTable(const std::string& table_name) { | 112 bool LibLouisWrapper::CheckTable(const std::string& table_names) { |
| 113 return lou_getTable(table_name.c_str()) != NULL; | 113 return lou_getTable(table_names.c_str()) != NULL; |
| 114 } | 114 } |
| 115 | 115 |
| 116 bool LibLouisWrapper::Translate(const TranslationParams& params, | 116 bool LibLouisWrapper::Translate(const TranslationParams& params, |
| 117 TranslationResult* out) { | 117 TranslationResult* out) { |
| 118 // Convert the character set of the input text. | 118 // Convert the character set of the input text. |
| 119 std::vector<widechar> inbuf; | 119 std::vector<widechar> inbuf; |
| 120 if (!DecodeUtf8(params.text, &inbuf)) { | 120 if (!DecodeUtf8(params.text, &inbuf)) { |
| 121 // TODO(jbroman): log this | 121 // TODO(jbroman): log this |
| 122 return false; | 122 return false; |
| 123 } | 123 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 145 // common cases like single digits or capital letters won't always trigger | 145 // common cases like single digits or capital letters won't always trigger |
| 146 // retranslations (see the comments above the second exit condition inside | 146 // retranslations (see the comments above the second exit condition inside |
| 147 // the loop). We also set an arbitrary upper bound for the allocation | 147 // the loop). We also set an arbitrary upper bound for the allocation |
| 148 // to make sure the loop exits without running out of memory. | 148 // to make sure the loop exits without running out of memory. |
| 149 for (int outalloc = (inbufsize + 1) * 2, maxoutalloc = (inbufsize + 1) * 8; | 149 for (int outalloc = (inbufsize + 1) * 2, maxoutalloc = (inbufsize + 1) * 8; |
| 150 outalloc <= maxoutalloc; outalloc *= 2) { | 150 outalloc <= maxoutalloc; outalloc *= 2) { |
| 151 int inlen = inbufsize; | 151 int inlen = inbufsize; |
| 152 outlen = outalloc; | 152 outlen = outalloc; |
| 153 outbuf.resize(outalloc); | 153 outbuf.resize(outalloc); |
| 154 braille_to_text.resize(outalloc); | 154 braille_to_text.resize(outalloc); |
| 155 int result = lou_translate(params.table_name.c_str(), | 155 int result = lou_translate(params.table_names.c_str(), |
| 156 &inbuf[0], &inlen, &outbuf[0], &outlen, | 156 &inbuf[0], &inlen, &outbuf[0], &outlen, |
| 157 NULL /* typeform */, NULL /* spacing */, | 157 NULL /* typeform */, NULL /* spacing */, |
| 158 &text_to_braille[0], &braille_to_text[0], | 158 &text_to_braille[0], &braille_to_text[0], |
| 159 out_cursor_position_ptr, dotsIO /* mode */); | 159 out_cursor_position_ptr, dotsIO /* mode */); |
| 160 if (result == 0) { | 160 if (result == 0) { |
| 161 // TODO(jbroman): log this | 161 // TODO(jbroman): log this |
| 162 return false; | 162 return false; |
| 163 } | 163 } |
| 164 // If all of inbuf was not consumed, the output buffer must be too small | 164 // If all of inbuf was not consumed, the output buffer must be too small |
| 165 // and we have to retry with a larger buffer. | 165 // and we have to retry with a larger buffer. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 181 braille_to_text.resize(outlen); | 181 braille_to_text.resize(outlen); |
| 182 | 182 |
| 183 // Return the translation result. | 183 // Return the translation result. |
| 184 out->cells.swap(cells); | 184 out->cells.swap(cells); |
| 185 out->text_to_braille.swap(text_to_braille); | 185 out->text_to_braille.swap(text_to_braille); |
| 186 out->braille_to_text.swap(braille_to_text); | 186 out->braille_to_text.swap(braille_to_text); |
| 187 out->cursor_position = out_cursor_position; | 187 out->cursor_position = out_cursor_position; |
| 188 return true; | 188 return true; |
| 189 } | 189 } |
| 190 | 190 |
| 191 bool LibLouisWrapper::BackTranslate(const std::string& table_name, | 191 bool LibLouisWrapper::BackTranslate(const std::string& table_names, |
| 192 const std::vector<unsigned char>& cells, std::string* out) { | 192 const std::vector<unsigned char>& cells, std::string* out) { |
| 193 std::vector<widechar> inbuf; | 193 std::vector<widechar> inbuf; |
| 194 inbuf.reserve(cells.size()); | 194 inbuf.reserve(cells.size()); |
| 195 for (std::vector<unsigned char>::const_iterator it = cells.begin(); | 195 for (std::vector<unsigned char>::const_iterator it = cells.begin(); |
| 196 it != cells.end(); ++it) { | 196 it != cells.end(); ++it) { |
| 197 // Set the high-order bit to prevent liblouis from dropping empty cells. | 197 // Set the high-order bit to prevent liblouis from dropping empty cells. |
| 198 inbuf.push_back(*it | 0x8000); | 198 inbuf.push_back(*it | 0x8000); |
| 199 } | 199 } |
| 200 // To avoid unsigned/signed comparison warnings. | 200 // To avoid unsigned/signed comparison warnings. |
| 201 int inbufsize = inbuf.size(); | 201 int inbufsize = inbuf.size(); |
| 202 std::vector<widechar> outbuf; | 202 std::vector<widechar> outbuf; |
| 203 int outlen; | 203 int outlen; |
| 204 | 204 |
| 205 // Invoke liblouis. Do this in a loop since we can't precalculate the | 205 // Invoke liblouis. Do this in a loop since we can't precalculate the |
| 206 // translated size. We add an extra slot in the output buffer so that | 206 // translated size. We add an extra slot in the output buffer so that |
| 207 // common cases like single digits or capital letters won't always trigger | 207 // common cases like single digits or capital letters won't always trigger |
| 208 // retranslations (see the comments above the second exit condition inside | 208 // retranslations (see the comments above the second exit condition inside |
| 209 // the loop). We also set an arbitrary upper bound for the allocation | 209 // the loop). We also set an arbitrary upper bound for the allocation |
| 210 // to make sure the loop exits without running out of memory. | 210 // to make sure the loop exits without running out of memory. |
| 211 for (int outalloc = (inbufsize + 1) * 2, maxoutalloc = (inbufsize + 1) * 8; | 211 for (int outalloc = (inbufsize + 1) * 2, maxoutalloc = (inbufsize + 1) * 8; |
| 212 outalloc <= maxoutalloc; outalloc *= 2) { | 212 outalloc <= maxoutalloc; outalloc *= 2) { |
| 213 int inlen = inbufsize; | 213 int inlen = inbufsize; |
| 214 outlen = outalloc; | 214 outlen = outalloc; |
| 215 outbuf.resize(outalloc); | 215 outbuf.resize(outalloc); |
| 216 | 216 |
| 217 int result = lou_backTranslateString( | 217 int result = lou_backTranslateString( |
| 218 table_name.c_str(), &inbuf[0], &inlen, &outbuf[0], &outlen, | 218 table_names.c_str(), &inbuf[0], &inlen, &outbuf[0], &outlen, |
| 219 NULL /* typeform */, NULL /* spacing */, dotsIO /* mode */); | 219 NULL /* typeform */, NULL /* spacing */, dotsIO /* mode */); |
| 220 if (result == 0) { | 220 if (result == 0) { |
| 221 // TODO(jbroman): log this | 221 // TODO(jbroman): log this |
| 222 return false; | 222 return false; |
| 223 } | 223 } |
| 224 | 224 |
| 225 // If all of inbuf was not consumed, the output buffer must be too small | 225 // If all of inbuf was not consumed, the output buffer must be too small |
| 226 // and we have to retry with a larger buffer. | 226 // and we have to retry with a larger buffer. |
| 227 // In addition, if all of outbuf was exhausted, there's no way to know if | 227 // In addition, if all of outbuf was exhausted, there's no way to know if |
| 228 // more space was needed, so we'll have to retry the translation in that | 228 // more space was needed, so we'll have to retry the translation in that |
| (...skipping 10 matching lines...) Expand all Loading... |
| 239 // TODO(jbroman): log this | 239 // TODO(jbroman): log this |
| 240 return false; | 240 return false; |
| 241 } | 241 } |
| 242 | 242 |
| 243 // Return the back translation result. | 243 // Return the back translation result. |
| 244 out->swap(text); | 244 out->swap(text); |
| 245 return true; | 245 return true; |
| 246 } | 246 } |
| 247 | 247 |
| 248 } // namespace liblouis_nacl | 248 } // namespace liblouis_nacl |
| OLD | NEW |