[ 
https://issues.apache.org/jira/browse/PDFBOX-6306?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18149144#comment-18149144
 ] 

Tilman Hausherr commented on PDFBOX-6306:
-----------------------------------------

Personally I wouldn't mind making that method {{PDType0Font}} protected but it 
would be important to hear from the others in the team.
 * The class itself is public
 * The fields are all private
 * So it would be only usable by wrappers like described in the OpenHtmlToPdf 
link

To give enough time for feedback I'm setting myself a todo for Wednesday 
December 2nd. This has also the advantage that I can ask copilot (my AI credits 
have run out)

I also asked Claude Sonnet 5.5, IMHO the arguments are weak except (4):
h3. Why making it protected is a bad idea
 # *It has a fragile ownership contract.* Its own Javadoc says {{closeTTF}} 
"must be true" when {{load()}} created the TTF and false when the caller passed 
it in. If a subclass gets this wrong, it either leaks the font or closes a TTF 
the caller still owns. Behind a private constructor, only the factories can get 
this right.
 # *It leaks internals.* The constructor mutates the caller's TTF with 
{{{}enableVerticalSubstitutions(){}}}. It also creates the package-private 
{{final}} class {{{}PDCIDFontType2Embedder{}}}, which writes into the document. 
It then registers the TTF with 
{{{}document.registerTrueTypeFontForClosing(ttf){}}}. A protected constructor 
would make this behaviour part of the subclassing contract.
 # *It creates a compatibility burden.* A protected constructor is public API 
for subclassers. PDFBox could no longer change the signature, such as the two 
boolean flags or the embedder wiring, without breaking them. The two flags 
already look like they want refactoring.
 # *The class is not designed for subclassing in the embedding path.* All state 
is private: {{{}embedder{}}}, {{{}ttf{}}}, {{{}descendantFont{}}}, 
{{{}cmapLookup{}}}. {{readEncoding()}} and {{fetchCMapUCS2()}} are also 
private. A subclass could construct the object but could not influence or even 
inspect the embedding state. {{{}PDType3Font{}}}, by comparison, declares its 
{{readEncoding()}} as {{{}protected final{}}}, an intentional extension point. 
{{PDType0Font}} has nothing like that.
 # *It is inconsistent with the sibling classes.* {{PDTrueTypeFont}} also keeps 
its embedding constructor private and exposes {{load(...)}} factories. 
{{PDType1Font}} is the exception: it has public embedding constructors.
 # *It sits in a sensitive area.* The repo's {{AGENTS.md}} lists font parsing 
and substitution among the areas where changes need extra care. This is a 
design question rather than a security one. By {{{}SECURITY.md{}}}'s model, it 
is not a vulnerability.

h3. What I'd suggest instead
 * *Ask for the use case.* The usual reasons for wanting this are a custom 
subclass that overrides encoding or glyph behaviour, or wanting to wrap or 
decorate a loaded font.
 * *Subclassing for reading already works* via the public dictionary 
constructor.
 * *For embedding, add a factory method.* If a real gap exists, such as a 
missing option, add or extend a {{load(...)}} overload. That keeps the 
ownership rules internal.
 * *Prefer composition.* Wrapping the result of {{PDType0Font.load(...)}} 
avoids the inheritance problems above.
 * {*}If a maintainer agrees anyway{*}, the minimal safe version would document 
the {{closeTTF}} contract and add a protected constructor that hides the flag, 
for example always {{false}} with the caller owning the TTF. Open a JIRA issue 
first, since the project tracks changes there.

Caveat: I checked only the {{trunk}} checkout, a shallow clone, so I couldn't 
look at the history or at the maintenance branches ({{{}3.0{}}}, {{{}2.0{}}}). 
I also didn't search JIRA for earlier discussion of this request.

 

I had some further discussion which can be seen here, however I suspect it 
requires a login

https://claude.ai/share/23299288-6f8f-45fb-a193-7967389ef897

> Make PDTYpe0Font and GlyphLayout* extensible
> --------------------------------------------
>
>                 Key: PDFBOX-6306
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-6306
>             Project: PDFBox
>          Issue Type: Improvement
>    Affects Versions: 3.0.8 PDFBox
>            Reporter: Volker Kunert
>            Priority: Major
>             Fix For: 3.0.9 PDFBox, 4.0.0
>
>
> See [https://github.com/apache/pdfbox/pull/561]
>  
> This change adds extension points to the PDFBox glyph-layout processors and 
> font loaders, allowing subclasses to customize font creation without 
> replacing the existing loading and layout flow.
> This is meant to help the integration in OpenHtmlToPDF.
> h2. Changes
>  * Add overridable font-loader factory methods to {{GlyphLayoutProcessorAwt}} 
> and {{{}GlyphLayoutProcessorFop{}}}.
>  * Add overridable {{loadPDType0Font(...)}} methods to the AWT and FOP font 
> loaders.
>  * Make the {{PDType0Font}} constructor that accepts a {{TrueTypeFont}} 
> {{{}protected{}}}, enabling subclasses to create specialized Type 0 fonts.
>  * Make {{supportsFont(...)}} verify that the supplied font is a 
> {{PDType0Font}} before checking loader support.
>  *  



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to