bool GDI2FT_OS2_METRICS::init( HDC hdc ) /* -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- */ { DWORD font_data_size; font_data_size = GetFontData( hdc, OS2_TABLE_TAG, offsetof( TT_OS2, xAvgCharWidth ), &_xAvgCharWidth, sizeof( _xAvgCharWidth ) ); if( font_data_size == GDI_ERROR ) return false; font_data_size = GetFontData( hdc, OS2_TABLE_TAG, offsetof( TT_OS2, usWeightClass ), &_usWeightClass, sizeof( _usWeightClass ) ); if( font_data_size == GDI_ERROR ) return false; font_data_size = GetFontData( hdc, OS2_TABLE_TAG, offsetof( TT_OS2, usWidthClass ), &_usWidthClass, sizeof( _usWidthClass ) ); if( font_data_size == GDI_ERROR ) return false; font_data_size = GetFontData( hdc, OS2_TABLE_TAG, offsetof( TT_OS2, fsSelection ), &_fsSelection, sizeof( _fsSelection ) ); if( font_data_size == GDI_ERROR ) return false; _xAvgCharWidth = SWAPWORD( _xAvgCharWidth ); _usWeightClass = SWAPWORD( _usWeightClass ); _usWidthClass = SWAPWORD( _usWidthClass ); _fsSelection = SWAPWORD( _fsSelection ); return true; }
bool os2_metrics::init(HDC hdc) { // retrieve needed fields from the OS/2 table DWORD font_data_size; font_data_size = GetFontData(hdc, OS2_TABLE_TAG, offsetof(TT_OS2, xAvgCharWidth), &_xAvgCharWidth, sizeof(_xAvgCharWidth)); if (font_data_size == GDI_ERROR) return false; font_data_size = GetFontData(hdc, OS2_TABLE_TAG, offsetof(TT_OS2, usWeightClass), &_usWeightClass, sizeof(_usWeightClass)); if (font_data_size == GDI_ERROR) return false; font_data_size = GetFontData(hdc, OS2_TABLE_TAG, offsetof(TT_OS2, usWidthClass), &_usWidthClass, sizeof(_usWidthClass)); if (font_data_size == GDI_ERROR) return false; font_data_size = GetFontData(hdc, OS2_TABLE_TAG, offsetof(TT_OS2, fsSelection), &_fsSelection, sizeof(_fsSelection)); if (font_data_size == GDI_ERROR) return false; _xAvgCharWidth = SWAPWORD(_xAvgCharWidth); _usWeightClass = SWAPWORD(_usWeightClass); _usWidthClass = SWAPWORD(_usWidthClass); _fsSelection = SWAPWORD(_fsSelection); return true; }
static void swap_struct(ng_t *ng) { int i; ngram_sz_t j; for (i=1;i<=ng->n-1;i++) { for (j=0;j<=ng->num_kgrams[i];j++){ SWAPWORD(&ng->word_id[i][j]); } if (ng->four_byte_counts) { for (j=0;j<=ng->num_kgrams[i];j++) SWAPWORD(&ng->count4[i][j]); }else { for (j=0;j<=ng->num_kgrams[i];j++) SWAPHALF(&ng->count[i][j]); } } for (i=1;i<=ng->n-2;i++) { for (j=0;j<=ng->num_kgrams[i];j++) { if (ng->four_byte_alphas){ SWAPWORD(&ng->bo_weight4[i][j]); }else{ SWAPHALF(&ng->bo_weight[i][j]); } } for (j=0;j<=ng->num_kgrams[i];j++) { SWAPWORD(&ng->ind[i][j]); } } }
// Read word from stream int readword(FILE * f) { short int d; fread(&d,2,1,f); d = SWAPWORD(d); return d; }
int OSDRegion::PutBox(DWORD x, DWORD y, DWORD w, DWORD h, const void *data, bool sprite, BYTE dataFormat ) { if (dataFormat!= OSD_1555) { LogUnimplemented("PutBox only implemented for 1555 RGB data"); return 0; } CDC* pDC = GetDC(); int dx = x + GetXOffs(); int dy = y + GetYOffs(); word* pData = (word*) data; for (unsigned int j = 0; j<h; j++) { for (unsigned int i = 0; i<w; i++) { if (!sprite || (*pData != 0)) { pDC->SetPixelV(i + dx, j + dy, ConvertRGB(SWAPWORD(*pData))); } pData++; } } ReleaseDC(); return 0; }
int OSDRegion::GetBox(DWORD x, DWORD y, DWORD w, DWORD h, void *data) { CDC* pDC = GetDC(); CBitmap areaBM; areaBM.CreateCompatibleBitmap(pDC, w, h); CDC destDC; destDC.CreateCompatibleDC(pDC); destDC.SelectObject(&areaBM); destDC.BitBlt(0, 0, w, h, pDC, x + GetXOffs(), y + GetYOffs(), SRCCOPY); ReleaseDC(); BITMAPINFO bmInfo; ZeroMemory(&bmInfo, sizeof(BITMAPINFO)); bmInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmInfo.bmiHeader.biWidth = w; bmInfo.bmiHeader.biHeight = -(int)h; bmInfo.bmiHeader.biPlanes = 1; bmInfo.bmiHeader.biBitCount = 16; bmInfo.bmiHeader.biCompression = BI_RGB; int lines = GetDIBits(destDC, (HBITMAP)areaBM.m_hObject, 0, h, data, &bmInfo, DIB_RGB_COLORS); WORD* pixel = (WORD*)data; int dataSize = w * lines; while ( dataSize-- > 0 ) { *pixel = SWAPWORD(*pixel); ++pixel; } return lines != h ? 1 : 0; }
static void mqtts_send_connect() // Send CONNECT { MQ_t * pBuf; pBuf = mqAssert(); if(pBuf == NULL) // no memory return; pBuf->addr = vMQTTS.GatewayID; if(vMQTTS.MsgID == 0) pBuf->mq.m.connect.Flags = MQTTS_FL_CLEANSESSION; else pBuf->mq.m.connect.Flags = 0; pBuf->mq.m.connect.ProtocolId = MQTTS_DEF_PROTOCOLID; pBuf->mq.m.connect.Duration = SWAPWORD(MQTTS_DEF_KEEPALIVE); // Build Name uint8_t * ipBuf; ipBuf = pBuf->mq.m.connect.ClientId; pBuf->mq.Length = MQTTS_SIZEOF_CLIENTID - 3; ReadOD(objNodeName, MQTTS_FL_TOPICID_PREDEF, &pBuf->mq.Length, ipBuf); if(pBuf->mq.Length < 2) // Node Name not defined, use default name { pBuf->mq.Length = OD_DEV_TYP_LEN; ReadOD(objDeviceTyp, MQTTS_FL_TOPICID_PREDEF, &pBuf->mq.Length, ipBuf); ipBuf += pBuf->mq.Length; *(ipBuf++) = '_'; pBuf->mq.Length++; pBuf->mq.Length += PHY_BuildName(ipBuf); } pBuf->mq.Length += MQTTS_SIZEOF_MSG_CONNECT; pBuf->mq.MsgType = MQTTS_MSGTYP_CONNECT; MQTTS_Push(pBuf); }
static uint16_t mqtts_new_msgid(void) { vMQTTS.MsgID++; if(vMQTTS.MsgID > 0xFFFE) vMQTTS.MsgID = 1; return SWAPWORD(vMQTTS.MsgID); }
std::string get_name(const char* memory_ptr) { TT_OFFSET_TABLE ttOffsetTable; TT_TABLE_DIRECTORY tblDir; size_t index = 0; memcpy(&ttOffsetTable, &memory_ptr[index], sizeof(TT_OFFSET_TABLE)); index += sizeof(TT_OFFSET_TABLE); ttOffsetTable.uNumOfTables = SWAPWORD(ttOffsetTable.uNumOfTables); ttOffsetTable.uMajorVersion = SWAPWORD(ttOffsetTable.uMajorVersion); ttOffsetTable.uMinorVersion = SWAPWORD(ttOffsetTable.uMinorVersion); //check is this is a true type font and the version is 1.0 if (ttOffsetTable.uMajorVersion != 1 || ttOffsetTable.uMinorVersion != 0) { return std::move(std::string()); } memset(&tblDir, 0, sizeof(TT_TABLE_DIRECTORY)); for (size_t i = 0; i< ttOffsetTable.uNumOfTables; ++i) { memcpy(&tblDir, &memory_ptr[index], sizeof(TT_TABLE_DIRECTORY)); index += sizeof(TT_TABLE_DIRECTORY); if (tblDir.nTag == 'eman') { tblDir.uLength = SWAPLONG(tblDir.uLength); tblDir.uOffset = SWAPLONG(tblDir.uOffset); return get_name_by_offset(memory_ptr, tblDir.uOffset); } else if (tblDir.nTag == 0) { break; } } return std::move(std::string()); }
static uint8_t mqtts_check_msgid(uint16_t msgid) { uint16_t tmp = SWAPWORD(msgid); if(tmp == vMQTTS.inMsgId) // Dup return 1; // uint8_t delta = vMQTTS.MsgID < msgid ? msgid - vMQTTS.MsgID : vMQTTS.MsgID - msgid; // if(delta > MQTTS_DEF_MSGID_DELTA) // return 2; vMQTTS.inMsgId = tmp; return 0; }
bool os2_metrics::init(void *font_id) { // retrieve needed fields from the OS/2 table DWORD font_data_size; const HDC font_holder = dc_pool_instance.claim(); assert(font_holder != NULL); font_mgr_instance.select_font(font_id, font_holder); font_data_size = GetFontData(font_holder, OS2_TABLE_TAG, offsetof(TT_OS2, xAvgCharWidth), reinterpret_cast<byte *>(&_xAvgCharWidth), sizeof(_xAvgCharWidth)); if (font_data_size == GDI_ERROR) goto failed_init_os2_metrics; font_data_size = GetFontData(font_holder, OS2_TABLE_TAG, offsetof(TT_OS2, usWeightClass), reinterpret_cast<byte *>(&_usWeightClass), sizeof(_usWeightClass)); if (font_data_size == GDI_ERROR) goto failed_init_os2_metrics; font_data_size = GetFontData(font_holder, OS2_TABLE_TAG, offsetof(TT_OS2, usWidthClass), reinterpret_cast<byte *>(&_usWidthClass), sizeof(_usWidthClass)); if (font_data_size == GDI_ERROR) goto failed_init_os2_metrics; font_data_size = GetFontData(font_holder, OS2_TABLE_TAG, offsetof(TT_OS2, fsSelection), reinterpret_cast<byte *>(&_fsSelection), sizeof(_fsSelection)); if (font_data_size == GDI_ERROR) goto failed_init_os2_metrics; _xAvgCharWidth = SWAPWORD(_xAvgCharWidth); _usWeightClass = SWAPWORD(_usWeightClass); _usWidthClass = SWAPWORD(_usWidthClass); _fsSelection = SWAPWORD(_fsSelection); return true; failed_init_os2_metrics: dc_pool_instance.free(font_holder); return false; }
std::string get_name_by_offset(const char* memory_ptr, size_t uOffset) { TT_NAME_TABLE_HEADER ttNTHeader; TT_NAME_RECORD ttRecord; size_t index = uOffset; memcpy(&ttNTHeader, &memory_ptr[index], sizeof(TT_NAME_TABLE_HEADER)); index += sizeof(TT_NAME_TABLE_HEADER); ttNTHeader.uNRCount = SWAPWORD(ttNTHeader.uNRCount); ttNTHeader.uStorageOffset = SWAPWORD(ttNTHeader.uStorageOffset); for (size_t i = 0; i < ttNTHeader.uNRCount; ++i) { memcpy(&ttRecord, &memory_ptr[index], sizeof(TT_NAME_RECORD)); index += sizeof(TT_NAME_RECORD); ttRecord.uPlatformID = SWAPWORD(ttRecord.uPlatformID); ttRecord.uEncodingID = SWAPWORD(ttRecord.uEncodingID); ttRecord.uLanguageID = SWAPWORD(ttRecord.uLanguageID); ttRecord.uNameID = SWAPWORD(ttRecord.uNameID); ttRecord.uStringLength = SWAPWORD(ttRecord.uStringLength); ttRecord.uStringOffset = SWAPWORD(ttRecord.uStringOffset); switch (ttRecord.uNameID) { case 1: { if (ttRecord.uEncodingID == 0) { const char* szString = &memory_ptr[uOffset + ttRecord.uStringOffset + ttNTHeader.uStorageOffset]; return std::move(std::string(szString, ttRecord.uStringLength)); } break; } default: break; } } return std::move(std::string()); }
uint8_t MQTTS_Register(uint16_t TopicID, uint8_t Size, uint8_t * ipBuf) { uint8_t mSize = Size + MQTTS_SIZEOF_MSG_REGISTER + 1; if(mSize > sizeof(MQ_t)) return 0xFF; MQ_t * pBuf = mqAssert(); if(pBuf != NULL) { pBuf->addr = vMQTTS.GatewayID; pBuf->mq.Length = mSize - 1; pBuf->mq.MsgType = MQTTS_MSGTYP_REGISTER; pBuf->mq.m.regist.TopicId = SWAPWORD(TopicID); pBuf->mq.m.regist.MsgId = mqtts_new_msgid(); memcpy(&pBuf->mq.m.regist.TopicName, ipBuf, Size); return MQTTS_ToBuf(pBuf); } return 0xFF; }
uint8_t MQTTS_Publish(uint16_t TopicID, uint8_t Flags, uint8_t Size, uint8_t * ipBuf) { uint8_t mSize = Size + MQTTS_SIZEOF_MSG_PUBLISH + 1; if(mSize > sizeof(MQ_t)) return 0xFF; MQ_t * pBuf = mqAssert(); if(pBuf != NULL) // no memory { pBuf->addr = vMQTTS.GatewayID; pBuf->mq.Length = mSize - 1; pBuf->mq.MsgType = MQTTS_MSGTYP_PUBLISH; pBuf->mq.m.publish.Flags = Flags; pBuf->mq.m.publish.TopicId = SWAPWORD(TopicID); pBuf->mq.m.publish.MsgId = mqtts_new_msgid(); memcpy(&pBuf->mq.m.publish.Data, ipBuf, Size); return MQTTS_ToBuf(pBuf); } return 0xFF; }
void *rr_fread(char *ptr, int elsize, int n_elem, FILE *fp, char *header, int not_more) { int n_read, i; char dummychar; if (n_elem > 0) { n_read = fread(ptr,elsize,n_elem,fp); if (n_read != n_elem) quit(-1, "rr_fread: problems reading %s. Only %d of %d elements were read\n", header, n_read, n_elem); if (elsize == sizeof(int)) { for (i=0; i<n_elem; i++) { SWAPWORD(ptr+(elsize*i)); } } else if (elsize == sizeof(short)) { for (i=0; i<n_elem; i++) { SWAPHALF(ptr+(elsize*i)); } } else if (elsize == sizeof(double)) { for (i=0; i<n_elem; i++) { SWAPDOUBLE(ptr+(elsize*i)); } } } if (not_more) { if (fread(&dummychar,1,1,fp) != 0) quit(-1,"rr_fread: more data after %s - should not be there\n",header); } return(0); /* Not relevant, but stops compilation warnings. */ }
uint8_t MQTTS_Poll(uint8_t wakeup) { if(vMQTTS.pfCnt) { vMQTTS.pfCnt--; return MQTTS_POLL_STAT_NOP; } switch(vMQTTS.Status) { #ifndef GATEWAY case MQTTS_STATUS_SEARCHGW: #ifndef ASLEEP vMQTTS.pfCnt = POLL_TMR_FREQ - 1; if(vMQTTS.Tretry) { vMQTTS.Tretry--; break; } vMQTTS.Tretry = MQTTS_DEF_TSGW - 1; if(vMQTTS.Nretry) vMQTTS.Nretry--; else SystemReset(); #else // ASLEEP if(vMQTTS.Tretry) { vMQTTS.Tretry--; if(vMQTTS.Tretry == (MQTTS_DEF_TSGW - 2)) { return MQTTS_POLL_STAT_ASLEEP; // ASLeep } else if(vMQTTS.Tretry == 0) { if(vMQTTS.Nretry) { vMQTTS.Nretry--; vMQTTS.pfCnt = POLL_TMR_FREQ - 1; return MQTTS_POLL_STAT_AWAKE; // WakeUp } // Not found many times SystemReset(); } break; } vMQTTS.pfCnt = POLL_TMR_FREQ - 1; vMQTTS.Tretry = MQTTS_DEF_TSGW - 1; #endif // ASLEEP mqtts_send_search_gw(); break; #endif // GATEWAY case MQTTS_STATUS_OFFLINE: // Connect to Broker vMQTTS.pfCnt = POLL_TMR_FREQ - 1; if(vMQTTS.Tretry) { vMQTTS.Tretry--; break; } if(vMQTTS.Nretry) vMQTTS.Nretry--; else { #ifdef GATEWAY SystemReset(); #else // NODE vMQTTS.Nretry = MQTTS_DEF_NRETRY; vMQTTS.Status = MQTTS_STATUS_SEARCHGW; break; #endif // GATEWAY } vMQTTS.Tretry = MQTTS_DEF_TCONNECT - 1; mqtts_send_connect(); break; case MQTTS_STATUS_CONNECT: vMQTTS.pfCnt = POLL_TMR_FREQ - 1; if(vMQTTS.Tretry) { vMQTTS.Tretry--; break; } if(vMQTTS.Nretry) vMQTTS.Nretry--; else { MQTTS_Disconnect(); break; } if(vMQTTS.fTail == vMQTTS.fHead) // Send Ping { if(wakeup == 0) mqtts_send_ping(); } else { MQ_t * pBuf; pBuf = mqAssert(); if(pBuf != NULL) // no memory { memcpy(pBuf, vMQTTS.fBuf[vMQTTS.fTail], sizeof(MQ_t)); MQTTS_Push(pBuf); } vMQTTS.pfCnt = POLL_TMR_FREQ_FAST; } break; #ifdef ASLEEP case MQTTS_STATUS_POST_CONNECT: { vMQTTS.pfCnt = (POLL_TMR_FREQ - 1); MQ_t * pBuf; pBuf = mqAssert(); if(pBuf != NULL) // no memory { // Send disconnect pBuf->mq.Length = MQTTS_SIZEOF_MSG_DISCONNECTL; pBuf->mq.MsgType = MQTTS_MSGTYP_DISCONNECT; pBuf->mq.m.disconnect.Duration = SWAPWORD(vMQTTS.Tasleep); MQTTS_Push(pBuf); } } if(vMQTTS.Nretry) { vMQTTS.Nretry--; break; } case MQTTS_STATUS_PRE_ASLEEP: vMQTTS.Status = MQTTS_STATUS_ASLEEP; vMQTTS.Tretry = vMQTTS.Tasleep; return MQTTS_POLL_STAT_ASLEEP; case MQTTS_STATUS_ASLEEP: if(vMQTTS.Tretry) { vMQTTS.Tretry--; break; } vMQTTS.pfCnt = POLL_TMR_FREQ - 1; vMQTTS.Status = MQTTS_STATUS_AWAKE; vMQTTS.Nretry = MQTTS_DEF_NRETRY; return MQTTS_POLL_STAT_AWAKE; case MQTTS_STATUS_AWAKE: vMQTTS.pfCnt = POLL_TMR_FREQ - 1; if(vMQTTS.Nretry) { vMQTTS.Nretry--; mqtts_send_ping(); } else MQTTS_Disconnect(); break; case MQTTS_STATUS_POST_AWAKE: if(wakeup != 0) { vMQTTS.Status = MQTTS_STATUS_OFFLINE; break; } vMQTTS.pfCnt = POLL_TMR_FREQ - 1; vMQTTS.Status = MQTTS_STATUS_PRE_ASLEEP; break; #endif // ASLEEP } return MQTTS_POLL_STAT_NOP; }
ULONG SwapObjectId( ULONG ulObjectId ) { return (MAKELONG(HIWORD(ulObjectId),SWAPWORD(LOWORD(ulObjectId)))) ; }
bool CTrueType::GetProperties(LPCTSTR pszFilePath, TTF_PROPERTIES* pProperties) { CAtlFile FontFile; HRESULT hr = FontFile.Create(pszFilePath, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING); if (FAILED(hr)) return false; TTF_OFFSET_TABLE ttOffsetTable; FontFile.Read(&ttOffsetTable, sizeof(TTF_OFFSET_TABLE)); ttOffsetTable.uNumOfTables = SWAPWORD(ttOffsetTable.uNumOfTables); ttOffsetTable.uMajorVersion = SWAPWORD(ttOffsetTable.uMajorVersion); ttOffsetTable.uMinorVersion = SWAPWORD(ttOffsetTable.uMinorVersion); // See if this is a true type font and the version is 1.0 if (ttOffsetTable.uMajorVersion != 1 || ttOffsetTable.uMinorVersion != 0) return false; TTF_TABLE_DIRECTORY tblDir; TTF_TABLE_DIRECTORY tblName; TTF_TABLE_DIRECTORY tblOS2; bool bFoundNameTable = false; bool bFoundOS2Table = false; for (int i=0; i< ttOffsetTable.uNumOfTables; i++) { FontFile.Read(&tblDir, sizeof(TTF_TABLE_DIRECTORY)); CString strName = CString(tblDir.szTag, 4); if (strName.IsEmpty()) break; if (!strName.CompareNoCase("name")) { bFoundNameTable = true; tblName = tblDir; tblName.uLength = SWAPLONG(tblName.uLength); tblName.uOffset = SWAPLONG(tblName.uOffset); } else if (!strName.CompareNoCase("OS/2")) { bFoundOS2Table = true; tblOS2 = tblDir; tblOS2.uLength = SWAPLONG(tblOS2.uLength); tblOS2.uOffset = SWAPLONG(tblOS2.uOffset); } if (bFoundNameTable && bFoundOS2Table) break; } if (bFoundNameTable) { FontFile.Seek(tblName.uOffset, FILE_BEGIN); TTF_NAME_TABLE_HEADER ttNTHeader; FontFile.Read(&ttNTHeader, sizeof(TTF_NAME_TABLE_HEADER)); ttNTHeader.uFSelector = SWAPWORD(ttNTHeader.uFSelector); ttNTHeader.uNRCount = SWAPWORD(ttNTHeader.uNRCount); ttNTHeader.uStorageOffset = SWAPWORD(ttNTHeader.uStorageOffset); for (int i=0; i<ttNTHeader.uNRCount; i++) { TTF_NAME_RECORD ttRecord; FontFile.Read(&ttRecord, sizeof(TTF_NAME_RECORD)); ttRecord.uPlatformID = SWAPWORD(ttRecord.uPlatformID); ttRecord.uEncodingID = SWAPWORD(ttRecord.uEncodingID); ttRecord.uLanguageID = SWAPWORD(ttRecord.uLanguageID); ttRecord.uNameID = SWAPWORD(ttRecord.uNameID); ttRecord.uStringLength = SWAPWORD(ttRecord.uStringLength); ttRecord.uStringOffset = SWAPWORD(ttRecord.uStringOffset); if (ttRecord.uPlatformID != 3) // Microsoft continue; ULONGLONG nPos = 0; FontFile.GetPosition(nPos); FontFile.Seek(tblName.uOffset + ttRecord.uStringOffset + ttNTHeader.uStorageOffset, FILE_BEGIN); int iUnicodeChars = (ttRecord.uStringLength + 1) / 2; WCHAR* pUnicodeBuffer = new WCHAR[iUnicodeChars]; FontFile.Read(pUnicodeBuffer, ttRecord.uStringLength); for (int i = 0; i < iUnicodeChars; i++) pUnicodeBuffer[i] = SWAPWORD(pUnicodeBuffer[i]); CString strName = CString(pUnicodeBuffer, iUnicodeChars); delete [] pUnicodeBuffer; FontFile.Seek(nPos, FILE_BEGIN); if (strName.IsEmpty()) continue; switch (ttRecord.uNameID) { case 1: // Font family pProperties->strFamily.IsEmpty() ? pProperties->strFamily = strName : void(); break; case 0: // Copyright notice pProperties->strCopyright.IsEmpty() ? pProperties->strCopyright = strName : void(); break; case 7: // Trademark notice pProperties->strTrademark.IsEmpty() ? pProperties->strTrademark = strName : void(); break; case 4: // Full Name of the Font pProperties->strName.IsEmpty() ? pProperties->strName = strName : void(); break; case 2: // Font sub family case 3: // Unique Family Identifier case 5: // Version of the name table case 6: // PostScript name of the font default: { break; } } } } if (bFoundOS2Table) { FontFile.Seek(tblOS2.uOffset, FILE_BEGIN); TTF_OS2_TABLE ttNTHeader; FontFile.Read(&ttNTHeader, sizeof(TTF_OS2_TABLE)); ttNTHeader.uVersion = SWAPWORD(ttNTHeader.uVersion); ttNTHeader.uAverageCharWidth = SWAPWORD(ttNTHeader.uAverageCharWidth); ttNTHeader.uWeightClass = SWAPWORD(ttNTHeader.uWeightClass); ttNTHeader.uWidthClass = SWAPWORD(ttNTHeader.uWidthClass); ttNTHeader.uFsType = SWAPWORD(ttNTHeader.uFsType); if (ttNTHeader.uFsType & 0x0001) // 1: reserved - must be zero; if not, turn it off ttNTHeader.uFsType &= ~0x0001; if (ttNTHeader.uFsType == 0x0000) // 0: embedding and permanent installation allowed { pProperties->strEmbed = "Installable"; pProperties->enumEmbed = TTF_Embed_Installable; } else if (ttNTHeader.uFsType & 0x0008) // 8: editable embedding allowed { pProperties->strEmbed = "Editable"; pProperties->enumEmbed = TTF_Embed_Editable; } else if (ttNTHeader.uFsType & 0x0004) // 4: preview and print embedding allowed { pProperties->strEmbed = "Printable"; pProperties->enumEmbed = TTF_Embed_Printable; } else if (ttNTHeader.uFsType & 0x0002) { pProperties->strEmbed = "None"; // 2: no embedding allowed pProperties->enumEmbed = TTF_Embed_None; } else { pProperties->strEmbed.Format("%d", ttNTHeader.uFsType); // unknown pProperties->enumEmbed = TTF_Embed_Unknown; } } FontFile.Close(); return !pProperties->strName.IsEmpty(); }
uint8_t MQTTS_Parser(MQ_t * pBuf) { uint8_t tmp; switch(pBuf->mq.MsgType) { case MQTTS_MSGTYP_SEARCHGW: #ifdef GATEWAY // Local Answer pBuf->mq.Length = MQTTS_SIZEOF_MSG_GWINFO; pBuf->mq.MsgType = MQTTS_MSGTYP_GWINFO; pBuf->mq.m.gwinfo.GwId = rf_GetNodeID(); MQTTS_Push(pBuf); return 1; #else // !GATEWAY if(vMQTTS.Status == MQTTS_STATUS_SEARCHGW) { vMQTTS.Tretry = MQTTS_DEF_TSGW; vMQTTS.Nretry = MQTTS_DEF_NRETRY; } break; case MQTTS_MSGTYP_ADVERTISE: case MQTTS_MSGTYP_GWINFO: if(vMQTTS.Status == MQTTS_STATUS_SEARCHGW) { vMQTTS.GatewayID = pBuf->addr; vMQTTS.Status = MQTTS_STATUS_OFFLINE; vMQTTS.Tretry = 0; } break; #endif // GATEWAY case MQTTS_MSGTYP_CONNACK: if(vMQTTS.Status == MQTTS_STATUS_OFFLINE) { vMQTTS.pfCnt = POLL_TMR_FREQ - 1; if(pBuf->mq.m.connack.ReturnCode == MQTTS_RET_ACCEPTED) { vMQTTS.Status = MQTTS_STATUS_CONNECT; vMQTTS.Tretry = 0; vMQTTS.Nretry = MQTTS_DEF_NRETRY; vMQTTS.inMsgId = 0; // Publish Device Type if((vMQTTS.MsgID == 0) && (rf_GetNodeID() != 0xFF)) { pBuf->mq.Length = MQTTS_SIZEOF_CLIENTID - 1; ReadOD(objDeviceTyp, MQTTS_FL_TOPICID_PREDEF, &pBuf->mq.Length, (uint8_t *)&pBuf->mq.m.raw); MQTTS_Publish(objDeviceTyp, MQTTS_FL_QOS1 | MQTTS_FL_TOPICID_PREDEF, pBuf->mq.Length, (uint8_t *)&pBuf->mq.m.raw); } } else { MQTTS_Disconnect(); return 1; } } break; case MQTTS_MSGTYP_REGISTER: if(vMQTTS.Status < MQTTS_STATUS_CONNECT) break; tmp = mqtts_check_msgid(pBuf->mq.m.regist.MsgId); if(tmp == 0) // New message vMQTTS.ReturnCode = RegisterOD(pBuf); else if(tmp > 1) break; // Build RegAck Message pBuf->mq.Length = MQTTS_SIZEOF_MSG_REGACK; pBuf->mq.MsgType = MQTTS_MSGTYP_REGACK; pBuf->mq.m.regack.ReturnCode = vMQTTS.ReturnCode; MQTTS_Push(pBuf); return 1; case MQTTS_MSGTYP_REGACK: if((vMQTTS.Status == MQTTS_STATUS_CONNECT) && (vMQTTS.fBuf[vMQTTS.fTail]->mq.MsgType == MQTTS_MSGTYP_REGISTER)) { uint16_t index = 0; if(pBuf->mq.m.regack.ReturnCode == MQTTS_RET_ACCEPTED) index = SWAPWORD(pBuf->mq.m.regack.TopicId); RegAckOD(index); mqtts_inc_tail(); } break; case MQTTS_MSGTYP_PUBLISH: if((vMQTTS.Status != MQTTS_STATUS_CONNECT) #ifdef ASLEEP && (vMQTTS.Status !=MQTTS_STATUS_AWAKE) #endif // ASLEEP ) break; vMQTTS.pfCnt = (POLL_TMR_FREQ - 1); vMQTTS.Nretry = MQTTS_DEF_NRETRY; tmp = mqtts_check_msgid(pBuf->mq.m.publish.MsgId); if(tmp == 0) // New message { vMQTTS.ReturnCode = WriteOD(SWAPWORD(pBuf->mq.m.publish.TopicId), pBuf->mq.m.publish.Flags | 0x80, pBuf->mq.Length - MQTTS_SIZEOF_MSG_PUBLISH, (uint8_t *)&pBuf->mq.m.publish.Data); } else if((!(pBuf->mq.m.publish.Flags & MQTTS_FL_DUP)) || (tmp > 1)) break; uint8_t qos = pBuf->mq.m.publish.Flags & MQTTS_FL_QOS_MASK; if(qos == MQTTS_FL_QOS1) // Need Ack { pBuf->mq.Length = MQTTS_SIZEOF_MSG_PUBACK; pBuf->mq.MsgType = MQTTS_MSGTYP_PUBACK; pBuf->mq.m.puback.TopicId = pBuf->mq.m.publish.TopicId; pBuf->mq.m.puback.MsgId = pBuf->mq.m.publish.MsgId; pBuf->mq.m.puback.ReturnCode = vMQTTS.ReturnCode; MQTTS_Push(pBuf); return 1; } break; case MQTTS_MSGTYP_PUBACK: if((vMQTTS.Status == MQTTS_STATUS_CONNECT) && (vMQTTS.fBuf[vMQTTS.fTail]->mq.MsgType == MQTTS_MSGTYP_PUBLISH) && (vMQTTS.fBuf[vMQTTS.fTail]->mq.m.publish.MsgId == pBuf->mq.m.puback.MsgId)) mqtts_inc_tail(); break; case MQTTS_MSGTYP_SUBACK: if((vMQTTS.Status == MQTTS_STATUS_CONNECT) && (vMQTTS.fBuf[vMQTTS.fTail]->mq.MsgType == MQTTS_MSGTYP_SUBSCRIBE) && (vMQTTS.fBuf[vMQTTS.fTail]->mq.m.subscribe.MsgId == pBuf->mq.m.suback.MsgId)) mqtts_inc_tail(); break; case MQTTS_MSGTYP_PINGRESP: #ifdef ASLEEP if(vMQTTS.Status == MQTTS_STATUS_AWAKE) { vMQTTS.Status = MQTTS_STATUS_POST_AWAKE; } else #endif // ASLEEP if((vMQTTS.Status == MQTTS_STATUS_CONNECT) && (vMQTTS.tail == vMQTTS.head)) { vMQTTS.Tretry = MQTTS_DEF_KEEPALIVE; #ifdef ASLEEP if(vMQTTS.Tasleep != 0) { vMQTTS.Status = MQTTS_STATUS_POST_CONNECT; vMQTTS.pfCnt = 0; } #endif // ASLEEP } else break; vMQTTS.Nretry = MQTTS_DEF_NRETRY; vMQTTS.pfCnt = POLL_TMR_FREQ - 1; break; case MQTTS_MSGTYP_DISCONNECT: #ifdef ASLEEP if(vMQTTS.Status == MQTTS_STATUS_POST_CONNECT) { vMQTTS.Status = MQTTS_STATUS_PRE_ASLEEP; vMQTTS.pfCnt = POLL_TMR_FREQ - 1; break; } #endif // ASLEEP MQTTS_Disconnect(); return 1; } return 0; }
void ahi_pci_outw(UWORD value, ULONG addr, APTR dev) { pci_outw(SWAPWORD(value), addr); }
static fz_error * parseTTF(fz_stream *file, int offset, int index, char *path) { fz_error *err = nil; int byteread; TT_OFFSET_TABLE ttOffsetTable; TT_TABLE_DIRECTORY tblDir; TT_NAME_TABLE_HEADER ttNTHeader; TT_NAME_RECORD ttRecord; char szTemp[4096]; int found; int i; fz_seek(file,offset,0); SAFE_FZ_READ(file, &ttOffsetTable, sizeof(TT_OFFSET_TABLE)); ttOffsetTable.uNumOfTables = SWAPWORD(ttOffsetTable.uNumOfTables); ttOffsetTable.uMajorVersion = SWAPWORD(ttOffsetTable.uMajorVersion); ttOffsetTable.uMinorVersion = SWAPWORD(ttOffsetTable.uMinorVersion); //check is this is a true type font and the version is 1.0 if(ttOffsetTable.uMajorVersion != 1 || ttOffsetTable.uMinorVersion != 0) return fz_throw("fonterror : invalid font version"); found = 0; for(i = 0; i< ttOffsetTable.uNumOfTables; i++) { SAFE_FZ_READ(file,&tblDir,sizeof(TT_TABLE_DIRECTORY)); memcpy(szTemp, tblDir.szTag, 4); szTemp[4] = 0; if (stricmp(szTemp, "name") == 0) { found = 1; tblDir.uLength = SWAPLONG(tblDir.uLength); tblDir.uOffset = SWAPLONG(tblDir.uOffset); break; } else if (szTemp[0] == 0) { break; } } if (found) { fz_seek(file,tblDir.uOffset,0); SAFE_FZ_READ(file,&ttNTHeader,sizeof(TT_NAME_TABLE_HEADER)); ttNTHeader.uNRCount = SWAPWORD(ttNTHeader.uNRCount); ttNTHeader.uStorageOffset = SWAPWORD(ttNTHeader.uStorageOffset); offset = tblDir.uOffset + sizeof(TT_NAME_TABLE_HEADER); for(i = 0; i < ttNTHeader.uNRCount && err == nil; ++i) { fz_seek(file, offset + sizeof(TT_NAME_RECORD)*i, 0); SAFE_FZ_READ(file,&ttRecord,sizeof(TT_NAME_RECORD)); ttRecord.uNameID = SWAPWORD(ttRecord.uNameID); ttRecord.uLanguageID = SWAPWORD(ttRecord.uLanguageID); // Full Name if(ttRecord.uNameID == 6) { ttRecord.uPlatformID = SWAPWORD(ttRecord.uPlatformID); ttRecord.uEncodingID = SWAPWORD(ttRecord.uEncodingID); ttRecord.uStringLength = SWAPWORD(ttRecord.uStringLength); ttRecord.uStringOffset = SWAPWORD(ttRecord.uStringOffset); fz_seek(file, tblDir.uOffset + ttRecord.uStringOffset + ttNTHeader.uStorageOffset, 0); SAFE_FZ_READ(file, szTemp, ttRecord.uStringLength); switch(ttRecord.uPlatformID) { case PLATFORM_UNICODE: err = decodeunicodeplatform(szTemp, ttRecord.uStringLength, szTemp, sizeof(szTemp), ttRecord.uEncodingID); break; case PLATFORM_MACINTOSH: err = decodemacintoshplatform(szTemp, ttRecord.uStringLength, szTemp, sizeof(szTemp), ttRecord.uEncodingID); break; case PLATFORM_ISO: err = fz_throw("fonterror : unsupported platform"); break; case PLATFORM_MICROSOFT: err = decodemicrosoftplatform(szTemp, ttRecord.uStringLength, szTemp, sizeof(szTemp), ttRecord.uEncodingID); break; } if(err == nil) err = insertmapping(&fontlistMS, szTemp, path, index); } } } cleanup: return err; }
std::wstring getNameFromTTFFile(const std::wstring& filename) { FONT_PROPERTIES_ANSI fp; FONT_PROPERTIES_ANSI * lpFontProps = &fp; memset(lpFontProps, 0, sizeof(FONT_PROPERTIES_ANSI)); HANDLE hFile = INVALID_HANDLE_VALUE; hFile = ::CreateFile(filename.c_str(), GENERIC_READ,// | GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL); if (hFile == INVALID_HANDLE_VALUE) { TRACE(_T("ERROR: failed to open '%s'\n"), Gosu::narrow(filename).c_str()); TRACE(_T("ERROR: %s failed\n"), _T("CreateFile")); return filename; } // get the file size DWORD dwFileSize = ::GetFileSize(hFile, NULL); if (dwFileSize == INVALID_FILE_SIZE) { TRACE(_T("ERROR: %s failed\n"), _T("GetFileSize")); ::CloseHandle(hFile); return filename; } //TRACE(_T("dwFileSize = %d\n"), dwFileSize); // Create a file mapping object that is the current size of the file HANDLE hMappedFile = NULL; hMappedFile = ::CreateFileMapping(hFile, NULL, PAGE_READONLY, //PAGE_READWRITE, 0, dwFileSize, NULL); if (hMappedFile == NULL) { TRACE(_T("ERROR: %s failed\n"), _T("CreateFileMapping")); ::CloseHandle(hFile); return filename; } LPBYTE lpMapAddress = (LPBYTE) ::MapViewOfFile(hMappedFile, // handle to file-mapping object FILE_MAP_READ,//FILE_MAP_WRITE, // access mode 0, // high-order DWORD of offset 0, // low-order DWORD of offset 0); // number of bytes to map if (lpMapAddress == NULL) { TRACE(_T("ERROR: %s failed\n"), _T("MapViewOfFile")); ::CloseHandle(hMappedFile); ::CloseHandle(hFile); return filename; } BOOL bRetVal = FALSE; int index = 0; TT_OFFSET_TABLE ttOffsetTable; memcpy(&ttOffsetTable, &lpMapAddress[index], sizeof(TT_OFFSET_TABLE)); index += sizeof(TT_OFFSET_TABLE); ttOffsetTable.uNumOfTables = SWAPWORD(ttOffsetTable.uNumOfTables); ttOffsetTable.uMajorVersion = SWAPWORD(ttOffsetTable.uMajorVersion); ttOffsetTable.uMinorVersion = SWAPWORD(ttOffsetTable.uMinorVersion); //check is this is a true type font and the version is 1.0 if (ttOffsetTable.uMajorVersion != 1 || ttOffsetTable.uMinorVersion != 0) return L""; TT_TABLE_DIRECTORY tblDir; memset(&tblDir, 0, sizeof(TT_TABLE_DIRECTORY)); BOOL bFound = FALSE; char szTemp[4096]; memset(szTemp, 0, sizeof(szTemp)); for (int i = 0; i< ttOffsetTable.uNumOfTables; i++) { //f.Read(&tblDir, sizeof(TT_TABLE_DIRECTORY)); memcpy(&tblDir, &lpMapAddress[index], sizeof(TT_TABLE_DIRECTORY)); index += sizeof(TT_TABLE_DIRECTORY); strncpy(szTemp, tblDir.szTag, 4); if (stricmp(szTemp, "name") == 0) { bFound = TRUE; tblDir.uLength = SWAPLONG(tblDir.uLength); tblDir.uOffset = SWAPLONG(tblDir.uOffset); break; } else if (szTemp[0] == 0) { break; } } if (bFound) { index = tblDir.uOffset; TT_NAME_TABLE_HEADER ttNTHeader; memcpy(&ttNTHeader, &lpMapAddress[index], sizeof(TT_NAME_TABLE_HEADER)); index += sizeof(TT_NAME_TABLE_HEADER); ttNTHeader.uNRCount = SWAPWORD(ttNTHeader.uNRCount); ttNTHeader.uStorageOffset = SWAPWORD(ttNTHeader.uStorageOffset); TT_NAME_RECORD ttRecord; bFound = FALSE; for (int i = 0; i < ttNTHeader.uNRCount && (lpFontProps->csCopyright[0] == 0 || lpFontProps->csName[0] == 0 || lpFontProps->csTrademark[0] == 0 || lpFontProps->csFamily[0] == 0); i++) { memcpy(&ttRecord, &lpMapAddress[index], sizeof(TT_NAME_RECORD)); index += sizeof(TT_NAME_RECORD); ttRecord.uNameID = SWAPWORD(ttRecord.uNameID); ttRecord.uStringLength = SWAPWORD(ttRecord.uStringLength); ttRecord.uStringOffset = SWAPWORD(ttRecord.uStringOffset); if (ttRecord.uNameID == 1 || ttRecord.uNameID == 0 || ttRecord.uNameID == 7) { int nPos = index; //f.GetPosition(); index = tblDir.uOffset + ttRecord.uStringOffset + ttNTHeader.uStorageOffset; memset(szTemp, 0, sizeof(szTemp)); memcpy(szTemp, &lpMapAddress[index], ttRecord.uStringLength); index += ttRecord.uStringLength; if (szTemp[0] != 0) { assert (strlen(szTemp) < sizeof(lpFontProps->csName)); switch (ttRecord.uNameID) { case 0: if (lpFontProps->csCopyright[0] == 0) strncpy(lpFontProps->csCopyright, szTemp, sizeof(lpFontProps->csCopyright)-1); break; case 1: if (lpFontProps->csFamily[0] == 0) strncpy(lpFontProps->csFamily, szTemp, sizeof(lpFontProps->csFamily)-1); bRetVal = TRUE; break; case 4: if (lpFontProps->csName[0] == 0) strncpy(lpFontProps->csName, szTemp, sizeof(lpFontProps->csName)-1); break; case 7: if (lpFontProps->csTrademark[0] == 0) strncpy(lpFontProps->csTrademark, szTemp, sizeof(lpFontProps->csTrademark)-1); break; default: break; } } index = nPos; } } } ::UnmapViewOfFile(lpMapAddress); ::CloseHandle(hMappedFile); ::CloseHandle(hFile); if (lpFontProps->csName[0] == 0) strcpy(lpFontProps->csName, lpFontProps->csFamily); return Gosu::widen(lpFontProps->csName); }
UWORD ahi_pci_inw(ULONG addr, APTR dev) { return SWAPWORD(pci_inw(addr)); }
/* * Code based off of code located here * http://www.codeproject.com/gdi/fontnamefromfile.asp */ static WCHAR *load_ttf_name_id( const WCHAR *filename, DWORD id ) { TT_TABLE_DIRECTORY tblDir; BOOL bFound = FALSE; TT_OFFSET_TABLE ttOffsetTable; TT_NAME_TABLE_HEADER ttNTHeader; TT_NAME_RECORD ttRecord; DWORD dwRead; HANDLE handle; LPWSTR ret = NULL; int i; handle = CreateFileW(filename ,GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 ); if (handle == INVALID_HANDLE_VALUE) { ERR("Unable to open font file %s\n", debugstr_w(filename)); return NULL; } if (!ReadFile(handle,&ttOffsetTable, sizeof(TT_OFFSET_TABLE),&dwRead,NULL)) goto end; ttOffsetTable.uNumOfTables = SWAPWORD(ttOffsetTable.uNumOfTables); ttOffsetTable.uMajorVersion = SWAPWORD(ttOffsetTable.uMajorVersion); ttOffsetTable.uMinorVersion = SWAPWORD(ttOffsetTable.uMinorVersion); if ((ttOffsetTable.uMajorVersion != 1 || ttOffsetTable.uMinorVersion != 0) && (ttOffsetTable.uMajorVersion != 0x4f54 || ttOffsetTable.uMinorVersion != 0x544f)) goto end; for (i=0; i< ttOffsetTable.uNumOfTables; i++) { if (!ReadFile(handle,&tblDir, sizeof(TT_TABLE_DIRECTORY),&dwRead,NULL)) break; if (memcmp(tblDir.szTag,"name",4)==0) { bFound = TRUE; tblDir.uLength = SWAPLONG(tblDir.uLength); tblDir.uOffset = SWAPLONG(tblDir.uOffset); break; } } if (!bFound) goto end; SetFilePointer(handle, tblDir.uOffset, NULL, FILE_BEGIN); if (!ReadFile(handle,&ttNTHeader, sizeof(TT_NAME_TABLE_HEADER), &dwRead,NULL)) goto end; ttNTHeader.uNRCount = SWAPWORD(ttNTHeader.uNRCount); ttNTHeader.uStorageOffset = SWAPWORD(ttNTHeader.uStorageOffset); for(i=0; i<ttNTHeader.uNRCount; i++) { if (!ReadFile(handle,&ttRecord, sizeof(TT_NAME_RECORD),&dwRead,NULL)) break; ttRecord.uNameID = SWAPWORD(ttRecord.uNameID); ttRecord.uPlatformID = SWAPWORD(ttRecord.uPlatformID); ttRecord.uEncodingID = SWAPWORD(ttRecord.uEncodingID); if (ttRecord.uNameID == id && ttRecord.uPlatformID == 3 && (ttRecord.uEncodingID == 0 || ttRecord.uEncodingID == 1)) { WCHAR *buf; unsigned int i; ttRecord.uStringLength = SWAPWORD(ttRecord.uStringLength); ttRecord.uStringOffset = SWAPWORD(ttRecord.uStringOffset); SetFilePointer(handle, tblDir.uOffset + ttRecord.uStringOffset + ttNTHeader.uStorageOffset, NULL, FILE_BEGIN); if (!(buf = msi_alloc_zero( ttRecord.uStringLength + sizeof(WCHAR) ))) goto end; dwRead = 0; ReadFile(handle, buf, ttRecord.uStringLength, &dwRead, NULL); if (dwRead % sizeof(WCHAR)) { msi_free(buf); goto end; } for (i = 0; i < dwRead / sizeof(WCHAR); i++) buf[i] = SWAPWORD(buf[i]); ret = strdupW(buf); msi_free(buf); break; } } end: CloseHandle(handle); TRACE("Returning %s\n", debugstr_w(ret)); return ret; }
bool GetTTFNameString(TCHAR* ttfFileName, int field, TCHAR* buf, int bufChars) { bool rval = false; TT_OFFSET_TABLE ttfTable; FILE* hFile = _tfopen(ttfFileName, _T("rb")); if (hFile == NULL) { // ERROR_MSG(_T("%sFile: failed opening file \"%s\"\n"),generatecode?_T(""):_T("Reserve"),newfn); // _tprintf(_T("%s: 打开文件时出错(\"%s\")\n"), _T("GetTTFVersion"), ttfFileName); } else { DWORD bytesRead = 0; fread(&ttfTable, sizeof(TT_OFFSET_TABLE), 1, hFile); ttfTable.uNumOfTables = SWAPWORD(ttfTable.uNumOfTables); ttfTable.uMajorVersion = SWAPWORD(ttfTable.uMajorVersion); ttfTable.uMinorVersion = SWAPWORD(ttfTable.uMinorVersion); if (ttfTable.uMajorVersion != 1 || ttfTable.uMinorVersion != 0) { // Cannot parse further! // ERROR_MSG(_T("%File: the file \"%s\" is in unsupported TTF format.\n")); return rval; } TT_TABLE_DIRECTORY tblDir; bool bFound = false; char tblName[5]; for (int i = 0; i < ttfTable.uNumOfTables; i++) { fread(&tblDir, sizeof(TT_TABLE_DIRECTORY), 1, hFile); //table's tag cannot exceed 4 characters strncpy(tblName, tblDir.szTag, sizeof(tblName)); tblName[4] = 0; if (strcmp("name", tblName) == 0) { //we found our table. Rearrange order and quit the loop bFound = true; tblDir.uLength = SWAPLONG(tblDir.uLength); tblDir.uOffset = SWAPLONG(tblDir.uOffset); break; } } if (bFound) { //move to offset we got from Offsets Table fseek(hFile, tblDir.uOffset, 0); TT_NAME_TABLE_HEADER ttNTHeader; fread(&ttNTHeader, sizeof(TT_NAME_TABLE_HEADER), 1, hFile); //again, don't forget to swap bytes! ttNTHeader.uNRCount = SWAPWORD(ttNTHeader.uNRCount); ttNTHeader.uStorageOffset = SWAPWORD(ttNTHeader.uStorageOffset); TT_NAME_RECORD ttRecord; bFound = false; for (int i = 0; i < ttNTHeader.uNRCount; i++) { fread(&ttRecord, sizeof(TT_NAME_RECORD), 1, hFile); ttRecord.uNameID = SWAPWORD(ttRecord.uNameID); ttRecord.uPlatformID = SWAPWORD(ttRecord.uPlatformID); ttRecord.uLanguageID = SWAPWORD(ttRecord.uLanguageID); ttRecord.uStringLength = SWAPWORD(ttRecord.uStringLength); // bytes ttRecord.uStringOffset = SWAPWORD(ttRecord.uStringOffset); // bytes //1 says that this is font name. 0 for example determines copyright info if (ttRecord.uNameID == field && ttRecord.uPlatformID == 3 && ttRecord.uLanguageID == 0x409) { unsigned int numChars = ttRecord.uStringLength / sizeof(wchar_t); fseek(hFile, tblDir.uOffset + ttRecord.uStringOffset + ttNTHeader.uStorageOffset, 0); std::vector<wchar_t> strBuf(numChars + 1, 0); fread(&strBuf[0], sizeof(wchar_t), numChars, hFile); for (unsigned int i = 0; i < strBuf.size(); ++i) { // Swap the bytes to make it UTF-16LE. strBuf[i] = SWAPWORD(strBuf[i]); } strBuf[numChars] = 0; #ifdef _UNICODE wcsncpy(buf, &strBuf[0], bufChars); #else wcstombs(buf, &strBuf[0], bufChars); #endif rval = true; break; } } } fclose(hFile); } return rval; }
SPString SPFontHelper::GetFontNameFromFile(SPString path) { SPFilePtr file; SPString result; file = SPFileManager::GetSingleton()->OpenFile(path); if (file) { TT_OFFSET_TABLE ttOffsetTable; file->Read(&ttOffsetTable, sizeof(TT_OFFSET_TABLE)); ttOffsetTable.uNumOfTables = SWAPWORD(ttOffsetTable.uNumOfTables); ttOffsetTable.uMajorVersion = SWAPWORD(ttOffsetTable.uMajorVersion); ttOffsetTable.uMinorVersion = SWAPWORD(ttOffsetTable.uMinorVersion); //check is this is a true type font and the version is 1.0 if(ttOffsetTable.uMajorVersion != 1 || ttOffsetTable.uMinorVersion != 0) { return result; } TT_TABLE_DIRECTORY tblDir; BOOL bFound = FALSE; SPString tempString; for(int i = 0; i < ttOffsetTable.uNumOfTables; i++) { file->Read(&tblDir, sizeof(TT_TABLE_DIRECTORY)); char* buffer = new char[5]; strncpy_s(buffer, 5, tblDir.szTag, 4); string s = buffer; delete [] buffer; buffer = NULL; if(s == "name") { bFound = TRUE; tblDir.uLength = SWAPLONG(tblDir.uLength); tblDir.uOffset = SWAPLONG(tblDir.uOffset); break; } } if(bFound) { file->SetPosition(tblDir.uOffset); TT_NAME_TABLE_HEADER ttNTHeader; file->Read(&ttNTHeader, sizeof(TT_NAME_TABLE_HEADER)); ttNTHeader.uNRCount = SWAPWORD(ttNTHeader.uNRCount); ttNTHeader.uStorageOffset = SWAPWORD(ttNTHeader.uStorageOffset); TT_NAME_RECORD ttRecord; bFound = FALSE; for(int i=0; i<ttNTHeader.uNRCount; i++) { file->Read(&ttRecord, sizeof(TT_NAME_RECORD)); ttRecord.uNameID = SWAPWORD(ttRecord.uNameID); if(ttRecord.uNameID == 1) { ttRecord.uStringLength = SWAPWORD(ttRecord.uStringLength); ttRecord.uStringOffset = SWAPWORD(ttRecord.uStringOffset); int nPos = file->GetPosition(); file->SetPosition(tblDir.uOffset + ttRecord.uStringOffset + ttNTHeader.uStorageOffset); char* buffer = new char[ttRecord.uStringLength + 1]; file->Read(buffer, ttRecord.uStringLength); string s = buffer; delete [] buffer; buffer = NULL; if(s.size() > 0) { result = SPStringHelper::ToWString(s); break; } file->SetPosition(nPos); } } } SPFileManager::GetSingleton()->CloseFile(path); } return result; }
/* * Code based off of code located here * http://www.codeproject.com/gdi/fontnamefromfile.asp */ static WCHAR *load_ttf_name_id( const char *mem, DWORD_PTR size, DWORD id, WCHAR *ret, DWORD len ) { const TT_TABLE_DIRECTORY *tblDir; TT_OFFSET_TABLE ttOffsetTable; TT_NAME_TABLE_HEADER ttNTHeader; TT_NAME_RECORD ttRecord; DWORD ofs, pos; int i; if (sizeof(TT_OFFSET_TABLE) > size) return NULL; ttOffsetTable = *(TT_OFFSET_TABLE*)mem; ttOffsetTable.uNumOfTables = SWAPWORD(ttOffsetTable.uNumOfTables); ttOffsetTable.uMajorVersion = SWAPWORD(ttOffsetTable.uMajorVersion); ttOffsetTable.uMinorVersion = SWAPWORD(ttOffsetTable.uMinorVersion); if (ttOffsetTable.uMajorVersion != 1 || ttOffsetTable.uMinorVersion != 0) return NULL; pos = sizeof(ttOffsetTable); for (i = 0; i < ttOffsetTable.uNumOfTables; i++) { tblDir = (const TT_TABLE_DIRECTORY*)&mem[pos]; pos += sizeof(*tblDir); if (memcmp(tblDir->szTag,"name",4)==0) { ofs = SWAPLONG(tblDir->uOffset); break; } } if (i >= ttOffsetTable.uNumOfTables) return NULL; pos = ofs + sizeof(ttNTHeader); if (pos > size) return NULL; ttNTHeader = *(TT_NAME_TABLE_HEADER*)&mem[ofs]; ttNTHeader.uNRCount = SWAPWORD(ttNTHeader.uNRCount); ttNTHeader.uStorageOffset = SWAPWORD(ttNTHeader.uStorageOffset); for(i=0; i<ttNTHeader.uNRCount; i++) { ttRecord = *(TT_NAME_RECORD*)&mem[pos]; pos += sizeof(ttRecord); if (pos > size) return NULL; ttRecord.uNameID = SWAPWORD(ttRecord.uNameID); if (ttRecord.uNameID == id) { const char *buf; ttRecord.uStringLength = SWAPWORD(ttRecord.uStringLength); ttRecord.uStringOffset = SWAPWORD(ttRecord.uStringOffset); if (ofs + ttRecord.uStringOffset + ttNTHeader.uStorageOffset + ttRecord.uStringLength > size) return NULL; buf = mem + ofs + ttRecord.uStringOffset + ttNTHeader.uStorageOffset; len = MultiByteToWideChar(CP_ACP, 0, buf, ttRecord.uStringLength, ret, len-1); ret[len] = 0; return ret; } } return NULL; }
/* * Code based off of code located here * http://www.codeproject.com/gdi/fontnamefromfile.asp */ WCHAR *load_ttf_name_id( const WCHAR *filename, DWORD id ) { TT_TABLE_DIRECTORY tblDir; BOOL bFound = FALSE; TT_OFFSET_TABLE ttOffsetTable; TT_NAME_TABLE_HEADER ttNTHeader; TT_NAME_RECORD ttRecord; DWORD dwRead; HANDLE handle; LPWSTR ret = NULL; int i; handle = CreateFileW(filename ,GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 ); if (handle == INVALID_HANDLE_VALUE) { ERR("Unable to open font file %s\n", debugstr_w(filename)); return NULL; } if (!ReadFile(handle,&ttOffsetTable, sizeof(TT_OFFSET_TABLE),&dwRead,NULL)) goto end; ttOffsetTable.uNumOfTables = SWAPWORD(ttOffsetTable.uNumOfTables); ttOffsetTable.uMajorVersion = SWAPWORD(ttOffsetTable.uMajorVersion); ttOffsetTable.uMinorVersion = SWAPWORD(ttOffsetTable.uMinorVersion); if (ttOffsetTable.uMajorVersion != 1 || ttOffsetTable.uMinorVersion != 0) goto end; for (i=0; i< ttOffsetTable.uNumOfTables; i++) { if (!ReadFile(handle,&tblDir, sizeof(TT_TABLE_DIRECTORY),&dwRead,NULL)) break; if (memcmp(tblDir.szTag,"name",4)==0) { bFound = TRUE; tblDir.uLength = SWAPLONG(tblDir.uLength); tblDir.uOffset = SWAPLONG(tblDir.uOffset); break; } } if (!bFound) goto end; SetFilePointer(handle, tblDir.uOffset, NULL, FILE_BEGIN); if (!ReadFile(handle,&ttNTHeader, sizeof(TT_NAME_TABLE_HEADER), &dwRead,NULL)) goto end; ttNTHeader.uNRCount = SWAPWORD(ttNTHeader.uNRCount); ttNTHeader.uStorageOffset = SWAPWORD(ttNTHeader.uStorageOffset); bFound = FALSE; for(i=0; i<ttNTHeader.uNRCount; i++) { if (!ReadFile(handle,&ttRecord, sizeof(TT_NAME_RECORD),&dwRead,NULL)) break; ttRecord.uNameID = SWAPWORD(ttRecord.uNameID); if (ttRecord.uNameID == id) { int nPos; LPSTR buf; ttRecord.uStringLength = SWAPWORD(ttRecord.uStringLength); ttRecord.uStringOffset = SWAPWORD(ttRecord.uStringOffset); nPos = SetFilePointer(handle, 0, NULL, FILE_CURRENT); SetFilePointer(handle, tblDir.uOffset + ttRecord.uStringOffset + ttNTHeader.uStorageOffset, NULL, FILE_BEGIN); buf = msi_alloc_zero( ttRecord.uStringLength + 1 ); ReadFile(handle, buf, ttRecord.uStringLength, &dwRead, NULL); if (strlen(buf) > 0) { ret = strdupAtoW(buf); msi_free(buf); break; } msi_free(buf); SetFilePointer(handle,nPos, NULL, FILE_BEGIN); } } end: CloseHandle(handle); TRACE("Returning %s\n", debugstr_w(ret)); return ret; }