Skip to content

Add attribute.stub.php: PHP OO API stubs for Pango text attributes - #8

Draft
MarcelBolten with Copilot wants to merge 13 commits into
modern_phpfrom
copilot/implementation-of-attributes
Draft

Add attribute.stub.php: PHP OO API stubs for Pango text attributes#8
MarcelBolten with Copilot wants to merge 13 commits into
modern_phpfrom
copilot/implementation-of-attributes

Conversation

Copilot AI commented Jul 10, 2026

Copy link
Copy Markdown

Pango text attributes (PangoAttrType, PangoAttribute, PangoAttrList, etc.) have no PHP representation yet. This adds the stub file that defines the object-oriented PHP API surface for all Pango attribute types.

New file: src/attribute.stub.php

Value types

  • Colorfinal readonly class for 16-bit RGB; parse(string $spec): Color + toString(): string
  • AttrType — backed enum: int covering all PANGO_ATTR_* values with #if PANGO_VERSION guards for additions in 1.38 / 1.44 / 1.46 / 1.50

Attribute hierarchy

  • Attribute — abstract base with $startIndex / $endIndex and static factory methods mapping 1:1 to pango_attr_*_new() C functions
  • AttrString, AttrInt, AttrFloat, AttrColor, AttrSize, AttrFontDesc — concrete leaf classes holding the typed payload

Supporting enums / flag classes

Name Added in
Underline base (+ SingleLine/DoubleLine/ErrorLine in 1.50)
Overline 1.46
ShowFlags (abstract constants class) 1.44
BaselineShift, FontScale, TextTransform 1.50

AttrList

Full API: insert(), insertBefore(), change(), getAttributes(), equal(), splice(), update() (1.44), toString() / fromString() (1.50).

Usage example

$list = new Pango\AttrList();

$bold = Pango\Attribute::weight(Pango\Weight::Bold);
$bold->startIndex = 0;
$bold->endIndex = 5;
$list->insert($bold);

$color = Pango\Attribute::foreground(65535, 0, 0); // red
$color->startIndex = 6;
$color->endIndex = PHP_INT_MAX;
$list->insert($color);

Copilot AI linked an issue Jul 10, 2026 that may be closed by this pull request
Copilot AI added 2 commits July 10, 2026 13:45
- Add Color class (readonly, 16-bit RGB with parse/toString)
- Add AttrType enum with all PangoAttrType values and version guards
- Add abstract Attribute base class with startIndex/endIndex and static
  factory methods for all attribute types
- Add AttrString, AttrInt, AttrFloat, AttrColor, AttrSize, AttrFontDesc
  concrete attribute subclasses
- Add Underline enum (with >= 1.50 cases)
- Add Overline enum (>= 1.46)
- Add ShowFlags abstract class with bitfield constants (>= 1.44)
- Add BaselineShift, FontScale, TextTransform enums (>= 1.50)
- Add AttrList class with insert/change/getAttributes/splice and
  update (>= 1.44), toString/fromString (>= 1.50)
- Fix grammar: 'what the display' -> 'the display'
- Fix spelling: 'expect' -> 'except' (word() docblock)
- Fix doc: 'underlined' -> 'overlined' in Overline::Single case
- Clarify splice() documentation to accurately describe behavior
Copilot AI changed the title [WIP] Create stub files for Pango attributes API in PHP Add attribute.stub.php: PHP OO API stubs for Pango text attributes Jul 10, 2026
Copilot AI requested a review from MarcelBolten July 10, 2026 13:47
@MarcelBolten

Copy link
Copy Markdown
Owner

@copilot I don't like the mix of static methods for some attributes and the concrete class implementation for others.
Make it consistent with only concrete classes.

Copilot AI commented Jul 10, 2026

Copy link
Copy Markdown
Author

@copilot I don't like the mix of static methods for some attributes and the concrete class implementation for others.
Make it consistent with only concrete classes.

Done. All static factory methods have been removed from Attribute. Each concrete class now has a constructor:

  • new AttrString(AttrType $type, string $value) — for Language, Family, FontFeatures
  • new AttrInt(AttrType $type, int $value) — for Style, Weight, Underline, Rise, etc.
  • new AttrFloat(AttrType $type, float $value) — for Scale, LineHeight
  • new AttrColor(AttrType $type, int $red, int $green, int $blue) — for Foreground, Background, UnderlineColor, etc.
  • new AttrSize(int $value, bool $absolute = false) — covers both Size and AbsoluteSize
  • new AttrFontDesc(FontDescription $desc) — for FontDesc

Committed in 1e2acc4.

…e where possible

Renamed:
- ColorAttribute -> Color
- SizeAttribute -> Size
- FontDescriptionAttribute -> FontDescription

StringAttribute, IntAttribute, and FloatAttribute cannot be renamed as
String, Int, and Float are reserved PHP type names.
…asses

Remove StringAttribute, IntAttribute, FloatAttribute, Color, Size, and
FontDescription generic classes. Add one concrete class per Type enum case:
Language, Family, Style, Weight, Variant, Stretch, Size, AbsoluteSize,
FontDesc, Foreground, Background, Underline, Strikethrough, Rise, Shape,
Scale, Fallback, LetterSpacing, UnderlineColor, StrikethroughColor,
Gravity, GravityHint, FontFeatures, ForegroundAlpha, BackgroundAlpha,
AllowBreaks, Show, InsertHyphens, Overline, OverlineColor, LineHeight,
AbsoluteLineHeight, TextTransform, Word, Sentence, BaselineShift, FontScale.
@MarcelBolten

Copy link
Copy Markdown
Owner

@copilot the Language attribute should not use a string in its constructor but a Language class that has yet to be implemented under the Pango namespace. only change the attribute and don't create the language class.

Copilot AI commented Jul 11, 2026

Copy link
Copy Markdown
Author

@copilot the Language attribute should not use a string in its constructor but a Language class that has yet to be implemented under the Pango namespace. only change the attribute and don't create the...

Done. The Language attribute now accepts \Pango\Language instead of string in its constructor and property — the Language class itself is not created. Committed in the latest push.

@MarcelBolten

Copy link
Copy Markdown
Owner

@copilot the variant attribute should only accept the variant enum already in the codebase

Copilot AI commented Jul 11, 2026

Copy link
Copy Markdown
Author

@copilot the variant attribute should only accept the variant enum already in the codebase

Done. The Variant attribute now accepts \Pango\Variant (the enum from font_description.stub.php) instead of int. Committed in the latest push.

@MarcelBolten

Copy link
Copy Markdown
Owner

@copilot the style attributeshould only accept the style enum

Copilot AI commented Jul 11, 2026

Copy link
Copy Markdown
Author

@copilot the style attributeshould only accept the style enum

Done. The Style attribute now accepts \Pango\Style (the enum from font_description.stub.php) instead of int. Committed in the latest push.

@MarcelBolten

Copy link
Copy Markdown
Owner

@copilot review all attributes and find all where a php enum could be used instead of int. show me a list before actually making any changes

Copilot AI commented Jul 11, 2026

Copy link
Copy Markdown
Author

@copilot review all attributes and find all where a php enum could be used instead of int. show me a list before actually making any changes

Here are all attributes currently using int where an existing PHP enum could be used instead:

Attribute class Suggested enum Defined in
Weight \Pango\Weight font_description.stub.php
Stretch \Pango\Stretch font_description.stub.php
Underline \Pango\Underline attribute.stub.php (Pango namespace)
Gravity \Pango\Gravity context.stub.php
GravityHint \Pango\GravityHint context.stub.php
Overline \Pango\Overline attribute.stub.php (Pango namespace)
TextTransform \Pango\TextTransform attribute.stub.php (Pango namespace)
BaselineShift \Pango\BaselineShift attribute.stub.php (Pango namespace)
FontScale \Pango\FontScale attribute.stub.php (Pango namespace)

Remaining int attributes where no matching enum exists:

  • Boolean semantics (Strikethrough, Fallback, AllowBreaks, InsertHyphens) — documented as "non-zero to enable"
  • Raw numeric/measurement values (Size, AbsoluteSize, Rise, LetterSpacing, ForegroundAlpha, BackgroundAlpha, AbsoluteLineHeight)
  • Flags combination (Show) — uses ShowFlags abstract constants class, not an enum

Shall I go ahead and apply the enum substitutions for all 9 in the table above?

@MarcelBolten

Copy link
Copy Markdown
Owner

@copilot yes, go ahead and do the changes proposed in the last comment

Copilot AI commented Jul 11, 2026

Copy link
Copy Markdown
Author

@copilot yes, go ahead and do the changes proposed in the last comment

Done. All 9 attribute classes now use their corresponding enums: Weight (\Pango\Weight), Stretch (\Pango\Stretch), Underline (\Pango\Underline), Gravity (\Pango\Gravity), GravityHint (\Pango\GravityHint), Overline (\Pango\Overline), TextTransform (\Pango\TextTransform), BaselineShift (\Pango\BaselineShift), FontScale (\Pango\FontScale).

@MarcelBolten

Copy link
Copy Markdown
Owner

@copilot now also convert the boolen like attributes to accept only boolean values in the php api

Copilot AI commented Jul 11, 2026

Copy link
Copy Markdown
Author

@copilot now also convert the boolen like attributes to accept only boolean values in the php api

Done. Strikethrough, Fallback, AllowBreaks, and InsertHyphens now use bool for both their $value property and constructor parameter. Committed in the latest push.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implementation of Attributes

2 participants