📄 fontcachechromiumwin.cpp
字号:
/* * Copyright (C) 2006, 2007 Apple Computer, Inc. * Copyright (c) 2006, 2007, 2008, 2009 Google Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following disclaimer * in the documentation and/or other materials provided with the * distribution. * * Neither the name of Google Inc. nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */#include "config.h"#include "FontCache.h"#include "ChromiumBridge.h"#include "Font.h"#include "FontUtilsChromiumWin.h"#include "HashMap.h"#include "HashSet.h"#include "SimpleFontData.h"#include "StringHash.h"#include <unicode/uniset.h>#include <windows.h>#include <objidl.h>#include <mlang.h>using std::min;namespace WebCore{void FontCache::platformInit(){ // Not needed on Windows.}// FIXME: consider adding to WebKit String classstatic bool charactersAreAllASCII(const String& s){ return charactersAreAllASCII(s.characters(), s.length());}// When asked for a CJK font with a native name under a non-CJK locale or// asked for a CJK font with a Romanized name under a CJK locale,// |GetTextFace| (after |CreateFont*|) returns a 'bogus' value (e.g. Arial).// This is not consistent with what MSDN says !!// Therefore, before we call |CreateFont*|, we have to map a Romanized name to // the corresponding native name under a CJK locale and vice versa // under a non-CJK locale.// See the corresponding gecko bugs at // https://bugzilla.mozilla.org/show_bug.cgi?id=373952// https://bugzilla.mozilla.org/show_bug.cgi?id=231426static bool LookupAltName(const String& name, String& altName){ struct FontCodepage { WCHAR* name; int codePage; }; struct NamePair { WCHAR* name; FontCodepage altNameCodepage; }; const int japaneseCodepage = 932; const int simplifiedChineseCodepage = 936; const int koreanCodepage = 949; const int traditionalChineseCodepage = 950; // FIXME(jungshik) : This list probably covers 99% of cases. // To cover the remaining 1% and cut down the file size, // consider accessing 'NAME' table of a truetype font // using |GetFontData| and caching the mapping. // In the table below, the ASCII keys are all lower-cased for // case-insensitive matching. static const NamePair namePairs[] = { // MS Pゴシック, MS PGothic {L"\xFF2D\xFF33 \xFF30\x30B4\x30B7\x30C3\x30AF", {L"MS PGothic", japaneseCodepage}}, {L"ms pgothic", {L"\xFF2D\xFF33 \xFF30\x30B4\x30B7\x30C3\x30AF", japaneseCodepage}}, // MS P明朝, MS PMincho {L"\xFF2D\xFF33 \xFF30\x660E\x671D", {L"MS PMincho", japaneseCodepage}}, {L"ms pmincho", {L"\xFF2D\xFF33 \xFF30\x660E\x671D", japaneseCodepage}}, // MSゴシック, MS Gothic {L"\xFF2D\xFF33 \x30B4\x30B7\x30C3\x30AF", {L"MS Gothic", japaneseCodepage}}, {L"ms gothic", {L"\xFF2D\xFF33 \x30B4\x30B7\x30C3\x30AF", japaneseCodepage}}, // MS 明朝, MS Mincho {L"\xFF2D\xFF33 \x660E\x671D", {L"MS Mincho", japaneseCodepage}}, {L"ms mincho", {L"\xFF2D\xFF33 \x660E\x671D", japaneseCodepage}}, // メイリオ, Meiryo {L"\x30E1\x30A4\x30EA\x30AA", {L"Meiryo", japaneseCodepage}}, {L"meiryo", {L"\x30E1\x30A4\x30EA\x30AA", japaneseCodepage}}, // 바탕, Batang {L"\xBC14\xD0D5", {L"Batang", koreanCodepage}}, {L"batang", {L"\xBC14\xD0D5", koreanCodepage}}, // 바탕체, Batangche {L"\xBC14\xD0D5\xCCB4", {L"Batangche", koreanCodepage}}, {L"batangche", {L"\xBC14\xD0D5\xCCB4", koreanCodepage}}, // 굴림, Gulim {L"\xAD74\xB9BC", {L"Gulim", koreanCodepage}}, {L"gulim", {L"\xAD74\xB9BC", koreanCodepage}}, // 굴림체, Gulimche {L"\xAD74\xB9BC\xCCB4", {L"Gulimche", koreanCodepage}}, {L"gulimche", {L"\xAD74\xB9BC\xCCB4", koreanCodepage}}, // 돋움, Dotum {L"\xB3CB\xC6C0", {L"Dotum", koreanCodepage}}, {L"dotum", {L"\xB3CB\xC6C0", koreanCodepage}}, // 돋움체, Dotumche {L"\xB3CB\xC6C0\xCCB4", {L"Dotumche", koreanCodepage}}, {L"dotumche", {L"\xB3CB\xC6C0\xCCB4", koreanCodepage}}, // 궁서, Gungsuh {L"\xAD81\xC11C", {L"Gungsuh", koreanCodepage}}, {L"gungsuh", {L"\xAD81\xC11C", koreanCodepage}}, // 궁서체, Gungsuhche {L"\xAD81\xC11C\xCCB4", {L"Gungsuhche", koreanCodepage}}, {L"gungsuhche", {L"\xAD81\xC11C\xCCB4", koreanCodepage}}, // 맑은 고딕, Malgun Gothic {L"\xB9D1\xC740 \xACE0\xB515", {L"Malgun Gothic", koreanCodepage}}, {L"malgun gothic", {L"\xB9D1\xC740 \xACE0\xB515", koreanCodepage}}, // 宋体, SimSun {L"\x5B8B\x4F53", {L"SimSun", simplifiedChineseCodepage}}, {L"simsun", {L"\x5B8B\x4F53", simplifiedChineseCodepage}}, // 黑体, SimHei {L"\x9ED1\x4F53", {L"SimHei", simplifiedChineseCodepage}}, {L"simhei", {L"\x9ED1\x4F53", simplifiedChineseCodepage}}, // 新宋体, NSimSun {L"\x65B0\x5B8B\x4F53", {L"NSimSun", simplifiedChineseCodepage}}, {L"nsimsun", {L"\x65B0\x5B8B\x4F53", simplifiedChineseCodepage}}, // 微软雅黑, Microsoft Yahei {L"\x5FAE\x8F6F\x96C5\x9ED1", {L"Microsoft Yahei", simplifiedChineseCodepage}}, {L"microsoft yahei", {L"\x5FAE\x8F6F\x96C5\x9ED1", simplifiedChineseCodepage}}, // 仿宋, FangSong {L"\x4EFF\x5B8B", {L"FangSong", simplifiedChineseCodepage}}, {L"fangsong", {L"\x4EFF\x5B8B", simplifiedChineseCodepage}}, // 楷体, KaiTi {L"\x6977\x4F53", {L"KaiTi", simplifiedChineseCodepage}}, {L"kaiti", {L"\x6977\x4F53", simplifiedChineseCodepage}}, // 仿宋_GB2312, FangSong_GB2312 {L"\x4EFF\x5B8B_GB2312", {L"FangSong_GB2312", simplifiedChineseCodepage}}, {L"fangsong_gb2312", {L"\x4EFF\x5B8B_gb2312", simplifiedChineseCodepage}}, // 楷体_GB2312, KaiTi_GB2312 {L"\x6977\x4F53", {L"KaiTi_GB2312", simplifiedChineseCodepage}}, {L"kaiti_gb2312", {L"\x6977\x4F53_gb2312", simplifiedChineseCodepage}}, // 新細明體, PMingLiu {L"\x65B0\x7D30\x660E\x9AD4", {L"PMingLiu", traditionalChineseCodepage}}, {L"pmingliu", {L"\x65B0\x7D30\x660E\x9AD4", traditionalChineseCodepage}}, // 細明體, MingLiu {L"\x7D30\x660E\x9AD4", {L"MingLiu", traditionalChineseCodepage}}, {L"mingliu", {L"\x7D30\x660E\x9AD4", traditionalChineseCodepage}}, // 微軟正黑體, Microsoft JhengHei {L"\x5FAE\x8EDF\x6B63\x9ED1\x9AD4", {L"Microsoft JhengHei", traditionalChineseCodepage}}, {L"microsoft jhengHei", {L"\x5FAE\x8EDF\x6B63\x9ED1\x9AD4", traditionalChineseCodepage}}, // 標楷體, DFKai-SB {L"\x6A19\x6977\x9AD4", {L"DFKai-SB", traditionalChineseCodepage}}, {L"dfkai-sb", {L"\x6A19\x6977\x9AD4", traditionalChineseCodepage}}, // WenQuanYi Zen Hei {L"\x6587\x6cc9\x9a5b\x6b63\x9ed1", {L"WenQuanYi Zen Hei", traditionalChineseCodepage}}, {L"wenquanyi zen hei", {L"\x6587\x6cc9\x9a5b\x6b63\x9ed1", traditionalChineseCodepage}}, // WenQuanYi Zen Hei {L"\x6587\x6cc9\x9a7f\x6b63\x9ed1", {L"WenQuanYi Zen Hei", simplifiedChineseCodepage}}, {L"wenquanyi zen hei", {L"\x6587\x6cc9\x9a7f\x6b63\x9ed1", simplifiedChineseCodepage}}, // AR PL ShanHeiSun Uni, {L"\x6587\x9f0e\x0050\x004c\x7d30\x4e0a\x6d77\x5b8b\x0055\x006e\x0069", {L"AR PL ShanHeiSun Uni", traditionalChineseCodepage}}, {L"ar pl shanheisun uni", {L"\x6587\x9f0e\x0050\x004c\x7d30\x4e0a\x6d77\x5b8b\x0055\x006e\x0069", traditionalChineseCodepage}}, // AR PL ShanHeiSun Uni, {L"\x6587\x9f0e\x0050\x004c\x7ec6\x4e0a\x6d77\x5b8b\x0055\x006e\x0069", {L"AR PL ShanHeiSun Uni", simplifiedChineseCodepage}}, {L"ar pl shanheisun uni", {L"\x6587\x9f0e\x0050\x004c\x7ec6\x4e0a\x6d77\x5b8b\x0055\x006e\x0069", simplifiedChineseCodepage}}, // AR PL ZenKai Uni // Traditional Chinese and Simplified Chinese names are // identical. {L"\x6587\x0050\x004C\x4E2D\x6977\x0055\x006E\x0069", {L"AR PL ZenKai Uni", traditionalChineseCodepage}}, {L"ar pl zenkai uni", {L"\x6587\x0050\x004C\x4E2D\x6977\x0055\x006E\x0069", traditionalChineseCodepage}}, {L"\x6587\x0050\x004C\x4E2D\x6977\x0055\x006E\x0069", {L"AR PL ZenKai Uni", simplifiedChineseCodepage}}, {L"ar pl zenkai uni", {L"\x6587\x0050\x004C\x4E2D\x6977\x0055\x006E\x0069", simplifiedChineseCodepage}}, }; typedef HashMap<String, const FontCodepage*> NameMap; static NameMap* fontNameMap = 0; if (!fontNameMap) { size_t numElements = sizeof(namePairs) / sizeof(NamePair); fontNameMap = new NameMap; for (size_t i = 0; i < numElements; ++i) fontNameMap->set(String(namePairs[i].name), &(namePairs[i].altNameCodepage)); } bool isAscii = false; String n; // use |lower| only for ASCII names // For non-ASCII names, we don't want to invoke an expensive // and unnecessary |lower|. if (charactersAreAllASCII(name)) { isAscii = true; n = name.lower(); } else n = name; NameMap::iterator iter = fontNameMap->find(n); if (iter == fontNameMap->end()) return false; static int systemCp = ::GetACP(); int fontCp = iter->second->codePage; if ((isAscii && systemCp == fontCp) || (!isAscii && systemCp != fontCp)) { altName = String(iter->second->name); return true; } return false;}static HFONT createFontIndirectAndGetWinName(const String& family, LOGFONT* winfont, String* winName){ int len = min(static_cast<int>(family.length()), LF_FACESIZE - 1); memcpy(winfont->lfFaceName, family.characters(), len * sizeof(WORD)); winfont->lfFaceName[len] = '\0'; HFONT hfont = CreateFontIndirect(winfont); if (!hfont) return 0; HDC dc = GetDC(0); HGDIOBJ oldFont = static_cast<HFONT>(SelectObject(dc, hfont)); WCHAR name[LF_FACESIZE]; unsigned resultLength = GetTextFace(dc, LF_FACESIZE, name); if (resultLength > 0) resultLength--; // ignore the null terminator SelectObject(dc, oldFont); ReleaseDC(0, dc); *winName = String(name, resultLength); return hfont;}// This maps font family names to their repertoires of supported Unicode// characters. Because it's family names rather than font faces we use// as keys, there might be edge cases where one face of a font family// has a different repertoire from another face of the same family. typedef HashMap<const wchar_t*, UnicodeSet*> FontCmapCache;static bool fontContainsCharacter(const FontPlatformData* fontData, const wchar_t* family, UChar32 character){ // FIXME: For non-BMP characters, GetFontUnicodeRanges is of // no use. We have to read directly from the cmap table of a font. // Return true for now. if (character > 0xFFFF) return true; // This cache is just leaked on shutdown. static FontCmapCache* fontCmapCache = 0; if (!fontCmapCache) fontCmapCache = new FontCmapCache; HashMap<const wchar_t*, UnicodeSet*>::iterator it = fontCmapCache->find(family); if (it != fontCmapCache->end()) return it->second->contains(character); HFONT hfont = fontData->hfont();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -