blog/cpp/fonts/core/tables/name/name.cpp

58 lines
1.4 KiB
C++
Raw Normal View History

2024-09-13 15:10:58 +08:00
#include "name.h"
#include "../reader/buffer.h"
#include <iostream>
NameTable::NameTable(reader *rd, uint16_t length){
read(rd, length);
}
void NameTable::readHeader(reader* rd){
header.read(rd);
}
void NameTable::read(reader* rd, uint16_t length){
header.read(rd); //read header
for(auto i = 0; i<header.getRecordCount(); i++){
readRecord(rd);
}
if(!rd->seek(header.getStorageOffset())){
throw "file len too low";
}
auto len = length - header.getStorageOffset();
auto data = rd->readString(len);
buffer storageReader(data);
for(auto it : header.getNameRecords()){
readName(&storageReader, it);
}
}
void NameTable::readRecord(reader* rd){
nameRecord nr;
nr.platformId = rd->readUint16();
nr.encodingId = rd->readUint16();
nr.languageId = rd->readUint16();
nr.nameId = rd->readUint16();
nr.length = rd->readUint16();
nr.offset = rd->readUint16();
nr.display();
header.addNameRecord(nr);
}
void NameTable::readName(reader* rd, nameRecord nr){
if(!rd->seek(nr.offset)){
return;
}
std::string s = rd->readString(nr.length);
names[nr.nameId] = s;
}
void NameTable::display(){
std::cout<<"this is the name infomation of the font:"<<std::endl;
header.display();
for(auto it: names){
std::cout<<"id:"<<it.first<<"\tvalue:"<<it.second<<std::endl;
}
}