| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "name.h" | 5 #include "name.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cstring> | 8 #include <cstring> |
| 9 | 9 |
| 10 #include "cff.h" | 10 #include "cff.h" |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 } | 191 } |
| 192 | 192 |
| 193 // check existence of required name strings (synthesize if necessary) | 193 // check existence of required name strings (synthesize if necessary) |
| 194 // [0 - copyright - skip] | 194 // [0 - copyright - skip] |
| 195 // 1 - family | 195 // 1 - family |
| 196 // 2 - subfamily | 196 // 2 - subfamily |
| 197 // [3 - unique ID - skip] | 197 // [3 - unique ID - skip] |
| 198 // 4 - full name | 198 // 4 - full name |
| 199 // 5 - version | 199 // 5 - version |
| 200 // 6 - postscript name | 200 // 6 - postscript name |
| 201 static const unsigned kStdNameCount = 7; | 201 static const uint16_t kStdNameCount = 7; |
| 202 static const char* kStdNames[kStdNameCount] = { | 202 static const char* kStdNames[kStdNameCount] = { |
| 203 NULL, | 203 NULL, |
| 204 "OTS derived font", | 204 "OTS derived font", |
| 205 "Unspecified", | 205 "Unspecified", |
| 206 NULL, | 206 NULL, |
| 207 "OTS derived font", | 207 "OTS derived font", |
| 208 "1.000", | 208 "1.000", |
| 209 "OTS-derived-font" | 209 "OTS-derived-font" |
| 210 }; | 210 }; |
| 211 // The spec says that "In CFF OpenType fonts, these two name strings, when | 211 // The spec says that "In CFF OpenType fonts, these two name strings, when |
| (...skipping 16 matching lines...) Expand all Loading... |
| 228 if (name_iter->platform_id == 1) { | 228 if (name_iter->platform_id == 1) { |
| 229 mac_name[id] = true; | 229 mac_name[id] = true; |
| 230 continue; | 230 continue; |
| 231 } | 231 } |
| 232 if (name_iter->platform_id == 3) { | 232 if (name_iter->platform_id == 3) { |
| 233 win_name[id] = true; | 233 win_name[id] = true; |
| 234 continue; | 234 continue; |
| 235 } | 235 } |
| 236 } | 236 } |
| 237 | 237 |
| 238 for (unsigned i = 0; i < kStdNameCount; ++i) { | 238 for (uint16_t i = 0; i < kStdNameCount; ++i) { |
| 239 if (kStdNames[i] == NULL) { | 239 if (kStdNames[i] == NULL) { |
| 240 continue; | 240 continue; |
| 241 } | 241 } |
| 242 if (!mac_name[i]) { | 242 if (!mac_name[i]) { |
| 243 NameRecord rec(1 /* platform_id */, 0 /* encoding_id */, | 243 NameRecord rec(1 /* platform_id */, 0 /* encoding_id */, |
| 244 0 /* language_id */ , i /* name_id */); | 244 0 /* language_id */ , i /* name_id */); |
| 245 rec.text.assign(kStdNames[i]); | 245 rec.text.assign(kStdNames[i]); |
| 246 name->names.push_back(rec); | 246 name->names.push_back(rec); |
| 247 sort_required = true; | 247 sort_required = true; |
| 248 } | 248 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 262 return true; | 262 return true; |
| 263 } | 263 } |
| 264 | 264 |
| 265 bool ots_name_should_serialise(OpenTypeFile* file) { | 265 bool ots_name_should_serialise(OpenTypeFile* file) { |
| 266 return file->name != NULL; | 266 return file->name != NULL; |
| 267 } | 267 } |
| 268 | 268 |
| 269 bool ots_name_serialise(OTSStream* out, OpenTypeFile* file) { | 269 bool ots_name_serialise(OTSStream* out, OpenTypeFile* file) { |
| 270 const OpenTypeNAME* name = file->name; | 270 const OpenTypeNAME* name = file->name; |
| 271 | 271 |
| 272 uint16_t name_count = name->names.size(); | 272 uint16_t name_count = static_cast<uint16_t>(name->names.size()); |
| 273 uint16_t lang_tag_count = name->lang_tags.size(); | 273 uint16_t lang_tag_count = static_cast<uint16_t>(name->lang_tags.size()); |
| 274 uint16_t format = 0; | 274 uint16_t format = 0; |
| 275 size_t string_offset = 6 + name_count * 12; | 275 size_t string_offset = 6 + name_count * 12; |
| 276 | 276 |
| 277 if (name->lang_tags.size() > 0) { | 277 if (name->lang_tags.size() > 0) { |
| 278 // lang tags require a format-1 name table | 278 // lang tags require a format-1 name table |
| 279 format = 1; | 279 format = 1; |
| 280 string_offset += 2 + lang_tag_count * 4; | 280 string_offset += 2 + lang_tag_count * 4; |
| 281 } | 281 } |
| 282 if (string_offset > 0xffff) { | 282 if (string_offset > 0xffff) { |
| 283 return OTS_FAILURE(); | 283 return OTS_FAILURE(); |
| 284 } | 284 } |
| 285 if (!out->WriteU16(format) || | 285 if (!out->WriteU16(format) || |
| 286 !out->WriteU16(name_count) || | 286 !out->WriteU16(name_count) || |
| 287 !out->WriteU16(string_offset)) { | 287 !out->WriteU16(static_cast<uint16_t>(string_offset))) { |
| 288 return OTS_FAILURE(); | 288 return OTS_FAILURE(); |
| 289 } | 289 } |
| 290 | 290 |
| 291 std::string string_data; | 291 std::string string_data; |
| 292 for (std::vector<NameRecord>::const_iterator name_iter = name->names.begin(); | 292 for (std::vector<NameRecord>::const_iterator name_iter = name->names.begin(); |
| 293 name_iter != name->names.end(); name_iter++) { | 293 name_iter != name->names.end(); name_iter++) { |
| 294 const NameRecord& rec = *name_iter; | 294 const NameRecord& rec = *name_iter; |
| 295 if (!out->WriteU16(rec.platform_id) || | 295 if (string_data.size() + rec.text.size() > |
| 296 std::numeric_limits<uint16_t>::max() || |
| 297 !out->WriteU16(rec.platform_id) || |
| 296 !out->WriteU16(rec.encoding_id) || | 298 !out->WriteU16(rec.encoding_id) || |
| 297 !out->WriteU16(rec.language_id) || | 299 !out->WriteU16(rec.language_id) || |
| 298 !out->WriteU16(rec.name_id) || | 300 !out->WriteU16(rec.name_id) || |
| 299 !out->WriteU16(rec.text.size()) || | 301 !out->WriteU16(static_cast<uint16_t>(rec.text.size())) || |
| 300 !out->WriteU16(string_data.size()) ) { | 302 !out->WriteU16(static_cast<uint16_t>(string_data.size())) ) { |
| 301 return OTS_FAILURE(); | 303 return OTS_FAILURE(); |
| 302 } | 304 } |
| 303 string_data.append(rec.text); | 305 string_data.append(rec.text); |
| 304 } | 306 } |
| 305 | 307 |
| 306 if (format == 1) { | 308 if (format == 1) { |
| 307 if (!out->WriteU16(lang_tag_count)) { | 309 if (!out->WriteU16(lang_tag_count)) { |
| 308 return OTS_FAILURE(); | 310 return OTS_FAILURE(); |
| 309 } | 311 } |
| 310 for (std::vector<std::string>::const_iterator tag_iter = | 312 for (std::vector<std::string>::const_iterator tag_iter = |
| 311 name->lang_tags.begin(); | 313 name->lang_tags.begin(); |
| 312 tag_iter != name->lang_tags.end(); tag_iter++) { | 314 tag_iter != name->lang_tags.end(); tag_iter++) { |
| 313 if (!out->WriteU16(tag_iter->size()) || | 315 if (string_data.size() + tag_iter->size() > |
| 314 !out->WriteU16(string_data.size())) { | 316 std::numeric_limits<uint16_t>::max() || |
| 317 !out->WriteU16(static_cast<uint16_t>(tag_iter->size())) || |
| 318 !out->WriteU16(static_cast<uint16_t>(string_data.size()))) { |
| 315 return OTS_FAILURE(); | 319 return OTS_FAILURE(); |
| 316 } | 320 } |
| 317 string_data.append(*tag_iter); | 321 string_data.append(*tag_iter); |
| 318 } | 322 } |
| 319 } | 323 } |
| 320 | 324 |
| 321 if (!out->Write(string_data.data(), string_data.size())) { | 325 if (!out->Write(string_data.data(), string_data.size())) { |
| 322 return OTS_FAILURE(); | 326 return OTS_FAILURE(); |
| 323 } | 327 } |
| 324 | 328 |
| 325 return true; | 329 return true; |
| 326 } | 330 } |
| 327 | 331 |
| 328 void ots_name_free(OpenTypeFile* file) { | 332 void ots_name_free(OpenTypeFile* file) { |
| 329 delete file->name; | 333 delete file->name; |
| 330 } | 334 } |
| 331 | 335 |
| 332 } // namespace | 336 } // namespace |
| OLD | NEW |