forked from mozilla/pdf.js
-
Notifications
You must be signed in to change notification settings - Fork 4
Return list of fonts #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Elizavetaseluykovich
wants to merge
1
commit into
customizations-for-tagged-pdf
Choose a base branch
from
fonts-pop-up
base: customizations-for-tagged-pdf
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1990,6 +1990,200 @@ class ExtendedCatalog extends Catalog { | |
| } | ||
| return shadow(this, "structureTree", structureTree); | ||
| } | ||
|
|
||
| resolveFontInfo(fontObj, fontName) { | ||
| let actualFont = fontObj; | ||
| let isComposite = false; | ||
| let cidFontType = null; | ||
| let normalizedSubtype = null; | ||
|
|
||
| try { | ||
| const subtype = fontObj.get("Subtype"); | ||
| const subtypeStr = | ||
| // eslint-disable-next-line no-nested-ternary | ||
| subtype instanceof Name | ||
| ? subtype.name | ||
| : typeof subtype === "string" | ||
| ? subtype | ||
| : null; | ||
|
|
||
| if (subtypeStr === "Type0") { | ||
| isComposite = true; | ||
|
|
||
| const descendantFonts = fontObj.get("DescendantFonts"); | ||
|
|
||
| if (Array.isArray(descendantFonts) && descendantFonts.length > 0) { | ||
| const cidFont = this.xref.fetchIfRef(descendantFonts[0]); | ||
|
|
||
| if (cidFont instanceof Dict) { | ||
| actualFont = cidFont; | ||
|
|
||
| const cidSubtype = cidFont.get("Subtype"); | ||
| const cidSubtypeStr = | ||
| // eslint-disable-next-line no-nested-ternary | ||
| cidSubtype instanceof Name | ||
| ? cidSubtype.name | ||
| : typeof cidSubtype === "string" | ||
| ? cidSubtype | ||
| : null; | ||
|
|
||
| if (cidSubtypeStr) { | ||
| cidFontType = cidSubtypeStr; | ||
|
|
||
| if (cidFontType === "CIDFontType0") { | ||
| normalizedSubtype = "Type1 (CID)"; | ||
| } else if (cidFontType === "CIDFontType2") { | ||
| normalizedSubtype = "TrueType (CID)"; | ||
| } else { | ||
| normalizedSubtype = cidFontType; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (!normalizedSubtype) { | ||
| normalizedSubtype = subtypeStr; | ||
| } | ||
| } else if (subtypeStr) { | ||
| normalizedSubtype = subtypeStr; | ||
| } | ||
|
|
||
| let descriptor = actualFont.get("FontDescriptor"); | ||
|
|
||
| if (!(descriptor instanceof Dict) && !isComposite) { | ||
| descriptor = fontObj.get("FontDescriptor"); | ||
| } | ||
|
|
||
| return { | ||
| descriptor, | ||
| isComposite, | ||
| cidFontType, | ||
| normalizedSubtype, | ||
| }; | ||
| } catch (e) { | ||
| console.error(`Error resolving font info for ${fontName}: ${e.message}`); | ||
|
|
||
| return { | ||
| descriptor: fontObj.get("FontDescriptor"), | ||
| isComposite: false, | ||
| cidFontType: null, | ||
| normalizedSubtype: null, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| collectFonts() { | ||
| const fontsList = []; | ||
| const seenRefs = new Set(); | ||
|
|
||
| try { | ||
| for (let pageIndex = 0; pageIndex < this.pages.length; pageIndex++) { | ||
| const pageRef = this.pages[pageIndex]; | ||
| const pageObj = this.xref.fetch(pageRef); | ||
|
|
||
| if (!(pageObj instanceof Dict)) { | ||
| continue; | ||
| } | ||
|
|
||
| let resources = pageObj.get("Resources"); | ||
| if (!resources) { | ||
| let parent = pageObj.get("Parent"); | ||
| while (parent instanceof Dict && !resources) { | ||
| resources = parent.get("Resources"); | ||
| parent = parent.get("Parent"); | ||
| } | ||
| } | ||
|
|
||
| if (!(resources instanceof Dict)) { | ||
| continue; | ||
| } | ||
|
|
||
| const fontDict = resources.get("Font"); | ||
| if (!(fontDict instanceof Dict)) { | ||
| continue; | ||
| } | ||
|
|
||
| for (const [fontName, fontVal] of fontDict) { | ||
| try { | ||
| const fontRef = fontDict.getRaw(fontName); | ||
| const refKey = | ||
| fontRef instanceof Ref ? fontRef.toString() : fontName; | ||
|
|
||
| if (seenRefs.has(refKey)) { | ||
| continue; | ||
| } | ||
| seenRefs.add(refKey); | ||
|
|
||
| const fontObj = this.xref.fetchIfRef(fontVal); | ||
| if (!(fontObj instanceof Dict)) { | ||
| continue; | ||
| } | ||
|
|
||
| // Resolve composite fonts and get the actual font info | ||
| const { descriptor, isComposite, cidFontType, normalizedSubtype } = | ||
| this.resolveFontInfo(fontObj, fontName); | ||
|
|
||
| const fontInfo = { | ||
| name: fontName, | ||
| pageIndex, | ||
| ref: fontRef instanceof Ref ? fontRef : null, | ||
| type: isComposite ? "Type0" : normalizedSubtype, | ||
| subtype: normalizedSubtype, | ||
| cidFontType, | ||
| baseFont: null, | ||
| encoding: null, | ||
| isSubset: false, | ||
| isEmbedded: false, | ||
| isComposite, | ||
| }; | ||
|
|
||
| if (fontObj.has("BaseFont")) { | ||
| const baseFont = fontObj.get("BaseFont"); | ||
| if (baseFont instanceof Name) { | ||
| fontInfo.baseFont = baseFont.name; | ||
| } else if (typeof baseFont === "string") { | ||
| fontInfo.baseFont = baseFont; | ||
| } | ||
| } | ||
|
|
||
| if (fontObj.has("Encoding")) { | ||
| const encoding = fontObj.get("Encoding"); | ||
| if (encoding instanceof Name) { | ||
| fontInfo.encoding = encoding.name; | ||
| } else if (typeof encoding === "string") { | ||
| fontInfo.encoding = encoding; | ||
| } | ||
| } | ||
|
|
||
| if (descriptor instanceof Dict) { | ||
| const fontFile = | ||
| descriptor.get("FontFile") || | ||
| descriptor.get("FontFile2") || | ||
| descriptor.get("FontFile3"); | ||
| fontInfo.isEmbedded = !!fontFile; | ||
| } | ||
|
|
||
| fontInfo.isSubset = fontInfo.baseFont | ||
| ? /^[A-Z0-9]{1,6}\+/.test(fontInfo.baseFont) | ||
| : false; | ||
|
Comment on lines
+2140
to
+2168
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any reason why this can't be calculated directly in |
||
|
|
||
| fontsList.push(fontInfo); | ||
| } catch { | ||
| continue; | ||
| } | ||
| } | ||
| } | ||
| } catch (e) { | ||
| console.error(`Failed to collect fonts: ${e.message}`); | ||
| } | ||
|
|
||
| return fontsList; | ||
| } | ||
|
|
||
| get fonts() { | ||
| const fonts = this.collectFonts(); | ||
| return shadow(this, "fonts", fonts && fonts.length > 0 ? fonts : null); | ||
| } | ||
| } | ||
|
|
||
| export { ExtendedCatalog as Catalog }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar getters for names or strings are used in several other places, so it's better to move this logic into a separate function.